diff --git a/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 b/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 index 1d263b6..59d57bc 100644 --- a/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 +++ b/lib/apis/Get-IcingaDirectorSelfServiceConfig.psm1 @@ -29,5 +29,7 @@ function Get-IcingaDirectorSelfServiceConfig() throw 'Icinga Director Self-Service has thrown an error: ' + $JsonContent.error; } + $JsonContent = Add-PSCustomObjectMember -Object $JsonContent -Key 'IcingaMaster' -Value $response.BaseResponse.ResponseUri.Host; + return $JsonContent; } diff --git a/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 b/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 index 46fa385..f5503d5 100644 --- a/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 +++ b/lib/apis/Register-IcingaDirectorSelfServiceHost.psm1 @@ -3,7 +3,8 @@ function Register-IcingaDirectorSelfServiceHost() param( $DirectorUrl, $Hostname, - $ApiKey = $null + $ApiKey = $null, + [string]$Endpoint = $null ); if ([string]::IsNullOrEmpty($DirectorUrl)) { @@ -19,10 +20,16 @@ function Register-IcingaDirectorSelfServiceHost() } $ProgressPreference = "SilentlyContinue"; + $DirectorConfigJson = $null; + + if ([string]::IsNullOrEmpty($Endpoint) -eq $FALSE) { + $Interface = Get-IcingaNetworkInterface $Endpoint; + $DirectorConfigJson = [string]::Format('{0} "address":"{2}" {1}', '{', '}', $Interface); + } $EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/register-host?name={0}&key={1}', $Hostname, $ApiKey)); - $response = Invoke-WebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST'; + $response = Invoke-WebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -Body $DirectorConfigJson; if ($response.StatusCode -ne 200) { throw $response.Content; diff --git a/lib/core/icingaagent/misc/Convert-IcingaDirectorSelfServiceArguments.psm1 b/lib/core/icingaagent/misc/Convert-IcingaDirectorSelfServiceArguments.psm1 index d9ac0d4..b57a427 100644 --- a/lib/core/icingaagent/misc/Convert-IcingaDirectorSelfServiceArguments.psm1 +++ b/lib/core/icingaagent/misc/Convert-IcingaDirectorSelfServiceArguments.psm1 @@ -20,6 +20,7 @@ function Convert-IcingaDirectorSelfServiceArguments() AddFirewallRule = $JsonInput.agent_add_firewall_rule; AcceptConnections = $JsonInput.agent_add_firewall_rule; ServiceUser = $JsonInput.icinga_service_user; + IcingaMaster = $JsonInput.IcingaMaster; UpdateAgent = $TRUE; AddDirectorGlobal = $FALSE; AddGlobalTemplates = $FALSE; diff --git a/lib/core/icingaagent/misc/Start-IcingaAgentDirectorWizard.psm1 b/lib/core/icingaagent/misc/Start-IcingaAgentDirectorWizard.psm1 index a501437..02e83a0 100644 --- a/lib/core/icingaagent/misc/Start-IcingaAgentDirectorWizard.psm1 +++ b/lib/core/icingaagent/misc/Start-IcingaAgentDirectorWizard.psm1 @@ -76,7 +76,7 @@ function Start-IcingaAgentDirectorWizard() if ($HostKnown -eq $FALSE) { while ($TRUE) { try { - $SelfServiceAPIKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey -Hostname (Get-IcingaHostname @Arguments); + $SelfServiceAPIKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey -Hostname (Get-IcingaHostname @Arguments) -Endpoint $Arguments.IcingaMaster; break; } catch { $SelfServiceAPIKey = (Get-IcingaAgentInstallerAnswerInput -Prompt ([string]::Format('Failed to register host within Icinga Director. Please re-enter your SelfService API Key. If this prompt continues, drop your host key at "Hosts -> {0} -> Agent"', (Get-IcingaHostname @Arguments))) -Default 'v' -DefaultInput $SelfServiceAPIKey).answer; diff --git a/lib/core/tools/Add-PSCustomObjectMember.psm1 b/lib/core/tools/Add-PSCustomObjectMember.psm1 new file mode 100644 index 0000000..b7d0921 --- /dev/null +++ b/lib/core/tools/Add-PSCustomObjectMember.psm1 @@ -0,0 +1,16 @@ +function Add-PSCustomObjectMember() +{ + param ( + $Object, + $Key, + $Value + ); + + if ($null -eq $Object) { + return $Object; + } + + $Object | Add-Member -MemberType NoteProperty -Name $Key -Value $Value; + + return $Object; +}