mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Added support to manage framework config
This commit is contained in:
parent
59be230567
commit
7150fd1236
8 changed files with 177 additions and 0 deletions
25
lib/config/Get-IcingaConfigTreeCount.psm1
Normal file
25
lib/config/Get-IcingaConfigTreeCount.psm1
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function Get-IcingaConfigTreeCount()
|
||||
{
|
||||
param(
|
||||
$Path = ''
|
||||
);
|
||||
|
||||
$Config = Read-IcingaPowerShellConfig;
|
||||
$PathArray = $Path.Split('.');
|
||||
$ConfigObject = $Config;
|
||||
[int]$Count = 0;
|
||||
|
||||
foreach ($entry in $PathArray) {
|
||||
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ConfigObject = $ConfigObject.$entry;
|
||||
}
|
||||
|
||||
foreach ($config in $ConfigObject.PSObject.Properties) {
|
||||
$Count += 1;
|
||||
}
|
||||
|
||||
return $Count;
|
||||
}
|
||||
20
lib/config/Get-IcingaPowerShellConfig.psm1
Normal file
20
lib/config/Get-IcingaPowerShellConfig.psm1
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function Get-IcingaPowerShellConfig()
|
||||
{
|
||||
param(
|
||||
$Path = ''
|
||||
);
|
||||
|
||||
$Config = Read-IcingaPowerShellConfig;
|
||||
$PathArray = $Path.Split('.');
|
||||
$ConfigObject = $Config;
|
||||
|
||||
foreach ($entry in $PathArray) {
|
||||
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
|
||||
return $null;
|
||||
}
|
||||
|
||||
$ConfigObject = $ConfigObject.$entry;
|
||||
}
|
||||
|
||||
return $ConfigObject;
|
||||
}
|
||||
14
lib/config/New-IcingaPowerShellConfigItem.psm1
Normal file
14
lib/config/New-IcingaPowerShellConfigItem.psm1
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function New-IcingaPowerShellConfigItem()
|
||||
{
|
||||
param(
|
||||
$ConfigObject,
|
||||
[string]$ConfigKey,
|
||||
$ConfigValue = $null
|
||||
);
|
||||
|
||||
if ($null -eq $ConfigValue) {
|
||||
$ConfigValue = (New-Object -TypeName PSOBject);
|
||||
}
|
||||
|
||||
$ConfigObject | Add-Member -MemberType NoteProperty -Name $ConfigKey -Value $ConfigValue;
|
||||
}
|
||||
23
lib/config/Read-IcingaPowerShellConfig.psm1
Normal file
23
lib/config/Read-IcingaPowerShellConfig.psm1
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function Read-IcingaPowerShellConfig()
|
||||
{
|
||||
$ConfigDir = Get-IcingaPowerShellConfigDir;
|
||||
$ConfigFile = Join-Path -Path $ConfigDir -ChildPath 'config.json';
|
||||
|
||||
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) {
|
||||
if ($global:IcingaDaemonData.ContainsKey('Config')) {
|
||||
return $global:IcingaDaemonData.Config;
|
||||
}
|
||||
}
|
||||
|
||||
if (-Not (Test-Path $ConfigFile)) {
|
||||
return (New-Object -TypeName PSOBject);
|
||||
}
|
||||
|
||||
[string]$Content = Get-Content -Path $ConfigFile;
|
||||
|
||||
if ([string]::IsNullOrEmpty($Content)) {
|
||||
return (New-Object -TypeName PSOBject);
|
||||
}
|
||||
|
||||
return (ConvertFrom-Json -InputObject $Content);
|
||||
}
|
||||
32
lib/config/Remove-IcingaPowerShellConfig.psm1
Normal file
32
lib/config/Remove-IcingaPowerShellConfig.psm1
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
function Remove-IcingaPowerShellConfig()
|
||||
{
|
||||
param(
|
||||
$Path = ''
|
||||
);
|
||||
|
||||
if ([string]::IsNullOrEmpty($Path)) {
|
||||
throw 'Please specify a valid path to an object';
|
||||
}
|
||||
|
||||
$Config = Read-IcingaPowerShellConfig;
|
||||
$PathArray = $Path.Split('.');
|
||||
$ConfigObject = $Config;
|
||||
[int]$Index = $PathArray.Count;
|
||||
|
||||
foreach ($entry in $PathArray) {
|
||||
|
||||
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
|
||||
return $null;
|
||||
}
|
||||
|
||||
if ($index -eq 1) {
|
||||
$ConfigObject.PSObject.Properties.Remove($entry);
|
||||
break;
|
||||
}
|
||||
|
||||
$ConfigObject = $ConfigObject.$entry;
|
||||
$Index -= 1;
|
||||
}
|
||||
|
||||
Write-IcingaPowerShellConfig $Config;
|
||||
}
|
||||
31
lib/config/Set-IcingaPowerShellConfig.psm1
Normal file
31
lib/config/Set-IcingaPowerShellConfig.psm1
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
function Set-IcingaPowerShellConfig()
|
||||
{
|
||||
param(
|
||||
$Path = '',
|
||||
$Value = $null
|
||||
);
|
||||
|
||||
$Config = Read-IcingaPowerShellConfig;
|
||||
$PathArray = $Path.Split('.');
|
||||
$ConfigObject = $Config;
|
||||
[int]$Index = $PathArray.Count;
|
||||
$InputValue = $null;
|
||||
foreach ($entry in $PathArray) {
|
||||
if ($index -eq 1) {
|
||||
$InputValue = $Value;
|
||||
}
|
||||
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
|
||||
New-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry -ConfigValue $InputValue;
|
||||
}
|
||||
|
||||
if ($index -eq 1) {
|
||||
$ConfigObject.$entry = $Value;
|
||||
break;
|
||||
}
|
||||
|
||||
$ConfigObject = $ConfigObject.$entry;
|
||||
$index -= 1;
|
||||
}
|
||||
|
||||
Write-IcingaPowerShellConfig $Config;
|
||||
}
|
||||
9
lib/config/Test-IcingaPowerShellConfigItem.psm1
Normal file
9
lib/config/Test-IcingaPowerShellConfigItem.psm1
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function Test-IcingaPowerShellConfigItem()
|
||||
{
|
||||
param(
|
||||
$ConfigObject,
|
||||
$ConfigKey
|
||||
);
|
||||
|
||||
return ([bool]($ConfigObject.PSobject.Properties.Name -eq $ConfigKey) -eq $TRUE);
|
||||
}
|
||||
23
lib/config/Write-IcingaPowerShellConfig.psm1
Normal file
23
lib/config/Write-IcingaPowerShellConfig.psm1
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function Write-IcingaPowerShellConfig()
|
||||
{
|
||||
param(
|
||||
$Config
|
||||
);
|
||||
|
||||
$ConfigDir = Get-IcingaPowerShellConfigDir;
|
||||
$ConfigFile = Join-Path -Path $ConfigDir -ChildPath 'config.json';
|
||||
|
||||
if (-Not (Test-Path $ConfigDir)) {
|
||||
New-Item -Path $ConfigDir -ItemType Directory | Out-Null;
|
||||
}
|
||||
|
||||
$Content = ConvertTo-Json -InputObject $Config -Depth 100;
|
||||
|
||||
Set-Content -Path $ConfigFile -Value $Content;
|
||||
|
||||
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) {
|
||||
if ($global:IcingaDaemonData.ContainsKey('Config')) {
|
||||
$global:IcingaDaemonData.Config = $Config;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue