From b6a1d91d782e5e255225c2fb18a989d88d65b0fa Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Sun, 6 Oct 2019 16:56:59 +0200 Subject: [PATCH] Added Cmdlets to talk to Icinga Director Self-Service API --- .../Get-IcingaDirectorSelfServiceConfig.psm1 | 33 ++++++++++++++++ ...egister-IcingaDirectorSelfServiceHost.psm1 | 38 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 create mode 100644 lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 diff --git a/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 b/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 new file mode 100644 index 0000000..7e48f9c --- /dev/null +++ b/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 @@ -0,0 +1,33 @@ +function Get-IcingaDirectorSelfServiceConfig() +{ + param( + $DirectorUrl, + $ApiKey = $null + ); + + if ([string]::IsNullOrEmpty($DirectorUrl)) { + throw 'Please enter a valid Url to your Icinga Director'; + } + + if ([string]::IsNullOrEmpty($ApiKey)) { + throw 'Please enter either a template or your host key'; + } + + $ProgressPreference = "SilentlyContinue"; + + $EndpointUrl = [string]::Format('{0}/self-service/powershell-parameters?key={1}', $DirectorUrl, $ApiKey); + + $response = Invoke-WebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST'; + + if ($response.StatusCode -ne 200) { + throw $response.Content; + } + + $JsonContent = ConvertFrom-Json -InputObject $response.Content; + + if (Test-PSCustomObjectMember -PSObject $JsonContent -Name 'error') { + throw 'Icinga Director Self-Service has thrown an error: ' + $JsonContent.error; + } + + return $JsonContent; +} diff --git a/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 b/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 new file mode 100644 index 0000000..da2250d --- /dev/null +++ b/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 @@ -0,0 +1,38 @@ +function Register-IcingaDirectorSelfServiceHost() +{ + param( + $DirectorUrl, + $Hostname, + $ApiKey = $null + ); + + if ([string]::IsNullOrEmpty($DirectorUrl)) { + throw 'Please enter a valid Url to your Icinga Director'; + } + + if ([string]::IsNullOrEmpty($Hostname)) { + throw 'Please enter the hostname to use'; + } + + if ([string]::IsNullOrEmpty($ApiKey)) { + throw 'Please enter the API key of the template you wish to use'; + } + + $ProgressPreference = "SilentlyContinue"; + + $EndpointUrl = [string]::Format('{0}/self-service/register-host?name={1}&key={2}', $DirectorUrl, $Hostname, $ApiKey); + + $response = Invoke-WebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST'; + + if ($response.StatusCode -ne 200) { + throw $response.Content; + } + + $JsonContent = ConvertFrom-Json -InputObject $response.Content; + + if (Test-PSCustomObjectMember -PSObject $JsonContent -Name 'error') { + throw 'Icinga Director Self-Service has thrown an error: ' + $JsonContent.error; + } + + return $JsonContent; +}