diff --git a/lib/config/Get-IcingaConfigTreeCount.psm1 b/lib/config/Get-IcingaConfigTreeCount.psm1 new file mode 100644 index 0000000..a295d52 --- /dev/null +++ b/lib/config/Get-IcingaConfigTreeCount.psm1 @@ -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; +} diff --git a/lib/config/Get-IcingaPowerShellConfig.psm1 b/lib/config/Get-IcingaPowerShellConfig.psm1 new file mode 100644 index 0000000..65abd70 --- /dev/null +++ b/lib/config/Get-IcingaPowerShellConfig.psm1 @@ -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; +} diff --git a/lib/config/New-IcingaPowerShellConfigItem.psm1 b/lib/config/New-IcingaPowerShellConfigItem.psm1 new file mode 100644 index 0000000..ddff776 --- /dev/null +++ b/lib/config/New-IcingaPowerShellConfigItem.psm1 @@ -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; +} diff --git a/lib/config/Read-IcingaPowerShellConfig.psm1 b/lib/config/Read-IcingaPowerShellConfig.psm1 new file mode 100644 index 0000000..cf989f2 --- /dev/null +++ b/lib/config/Read-IcingaPowerShellConfig.psm1 @@ -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); +} diff --git a/lib/config/Remove-IcingaPowerShellConfig.psm1 b/lib/config/Remove-IcingaPowerShellConfig.psm1 new file mode 100644 index 0000000..a6c0dfd --- /dev/null +++ b/lib/config/Remove-IcingaPowerShellConfig.psm1 @@ -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; +} diff --git a/lib/config/Set-IcingaPowerShellConfig.psm1 b/lib/config/Set-IcingaPowerShellConfig.psm1 new file mode 100644 index 0000000..195d6e1 --- /dev/null +++ b/lib/config/Set-IcingaPowerShellConfig.psm1 @@ -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; +} diff --git a/lib/config/Test-IcingaPowerShellConfigItem.psm1 b/lib/config/Test-IcingaPowerShellConfigItem.psm1 new file mode 100644 index 0000000..95c3fe4 --- /dev/null +++ b/lib/config/Test-IcingaPowerShellConfigItem.psm1 @@ -0,0 +1,9 @@ +function Test-IcingaPowerShellConfigItem() +{ + param( + $ConfigObject, + $ConfigKey + ); + + return ([bool]($ConfigObject.PSobject.Properties.Name -eq $ConfigKey) -eq $TRUE); +} diff --git a/lib/config/Write-IcingaPowerShellConfig.psm1 b/lib/config/Write-IcingaPowerShellConfig.psm1 new file mode 100644 index 0000000..3dfbdda --- /dev/null +++ b/lib/config/Write-IcingaPowerShellConfig.psm1 @@ -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; + } + } +}