2020-04-28 09:24:57 -04:00
<#
. SYNOPSIS
Will fetch the current host configuration or general configuration depending
if a host or template key is specified from the Icinga Director Self-Service API
. DESCRIPTION
Use the Self-Service API of the Icinga Director to connect to it and fetch the
configuration to apply for this host . The configuration itself is differentiated
if a template or the specific host key is used
. FUNCTIONALITY
Fetches host or general configuration form the Icinga Director Self-Service API
. EXAMPLE
PS > Get-IcingaDirectorSelfServiceConfig -DirectorUrl ' https : / / example . com / icingaweb2 / director -ApiKey 457g6b98054v76vb5490ß276bv0457v6054b76 ;
. PARAMETER DirectorUrl
The URL pointing directly to the Icinga Web 2 Director module
. PARAMETER ApiKey
Either the template or host key to authenticate against the Self-Service API
. INPUTS
System . String
. OUTPUTS
System . Object
. LINK
https : / / github . com / Icinga / icinga-powershell -framework
#>
2019-10-06 10:56:59 -04:00
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 ) ) {
2020-03-11 12:53:59 -04:00
throw 'Please enter either a template or your host key. If this message persists, ensure your host is not having a template key assigned already. If so, you can try dropping it within the Icinga Director.' ;
2019-10-06 10:56:59 -04:00
}
2020-07-31 04:20:53 -04:00
Set-IcingaTLSVersion ;
2019-10-06 10:56:59 -04:00
$ProgressPreference = " SilentlyContinue " ;
2019-11-02 12:42:39 -04:00
$EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ( [ string ] :: Format ( '/self-service/powershell-parameters?key={0}' , $ApiKey ) ) ;
2019-10-06 10:56:59 -04:00
$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 ;
}
2019-12-12 09:49:56 -05:00
$JsonContent = Add-PSCustomObjectMember -Object $JsonContent -Key 'IcingaMaster' -Value $response . BaseResponse . ResponseUri . Host ;
2019-10-06 10:56:59 -04:00
return $JsonContent ;
}