Add support for Host-Address for Director Register

Fixes #31
This commit is contained in:
Christian Stein 2019-12-12 15:49:56 +01:00
parent ce9257a30d
commit b978ee6574
5 changed files with 29 additions and 3 deletions

View file

@ -29,5 +29,7 @@ function Get-IcingaDirectorSelfServiceConfig()
throw 'Icinga Director Self-Service has thrown an error: ' + $JsonContent.error; 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; return $JsonContent;
} }

View file

@ -3,7 +3,8 @@ function Register-IcingaDirectorSelfServiceHost()
param( param(
$DirectorUrl, $DirectorUrl,
$Hostname, $Hostname,
$ApiKey = $null $ApiKey = $null,
[string]$Endpoint = $null
); );
if ([string]::IsNullOrEmpty($DirectorUrl)) { if ([string]::IsNullOrEmpty($DirectorUrl)) {
@ -19,10 +20,16 @@ function Register-IcingaDirectorSelfServiceHost()
} }
$ProgressPreference = "SilentlyContinue"; $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)); $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) { if ($response.StatusCode -ne 200) {
throw $response.Content; throw $response.Content;

View file

@ -20,6 +20,7 @@ function Convert-IcingaDirectorSelfServiceArguments()
AddFirewallRule = $JsonInput.agent_add_firewall_rule; AddFirewallRule = $JsonInput.agent_add_firewall_rule;
AcceptConnections = $JsonInput.agent_add_firewall_rule; AcceptConnections = $JsonInput.agent_add_firewall_rule;
ServiceUser = $JsonInput.icinga_service_user; ServiceUser = $JsonInput.icinga_service_user;
IcingaMaster = $JsonInput.IcingaMaster;
UpdateAgent = $TRUE; UpdateAgent = $TRUE;
AddDirectorGlobal = $FALSE; AddDirectorGlobal = $FALSE;
AddGlobalTemplates = $FALSE; AddGlobalTemplates = $FALSE;

View file

@ -76,7 +76,7 @@ function Start-IcingaAgentDirectorWizard()
if ($HostKnown -eq $FALSE) { if ($HostKnown -eq $FALSE) {
while ($TRUE) { while ($TRUE) {
try { 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; break;
} catch { } 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; $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;

View file

@ -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;
}