From 4f4b6eb63a621f367ff33a191bd8ba719e2a5710 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 8 Sep 2016 10:45:02 +0000 Subject: [PATCH] IcingaService: allow host vars to override vars... ...for applied services refs #12546 --- .../Director/IcingaConfig/IcingaConfig.php | 22 +++++++++++++++++++ library/Director/Objects/IcingaService.php | 14 ++++++++++++ .../Director/Objects/rendered/service1.out | 2 ++ .../Director/Objects/rendered/service2.out | 2 ++ 4 files changed, 40 insertions(+) diff --git a/library/Director/IcingaConfig/IcingaConfig.php b/library/Director/IcingaConfig/IcingaConfig.php index a12e4455..f22d67ab 100644 --- a/library/Director/IcingaConfig/IcingaConfig.php +++ b/library/Director/IcingaConfig/IcingaConfig.php @@ -457,12 +457,34 @@ class IcingaConfig ) )->prepend( "\nconst DirectorStageDir = dirname(dirname(current_filename))\n" + . $this->renderHostOverridableVars() . $this->renderMagicApplyFor() ); return $this; } + protected function renderHostOverridableVars() + { + $settings = $this->connection->settings(); + + return sprintf( + ' +const DirectorVarsOverride = "%s" + +template Service "%s" { + if (vars) { + vars += host.vars[DirectorVarsOverride][name] + } else { + vars = host.vars[DirectorVarsOverride][name] + } +} +', + $settings->override_services_varname, + $settings->override_services_templatename + ); + } + protected function renderMagicApplyFor() { if (! $this->usesMagicApplyFor()) { diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php index 5427ff08..adcef4c8 100644 --- a/library/Director/Objects/IcingaService.php +++ b/library/Director/Objects/IcingaService.php @@ -156,6 +156,20 @@ class IcingaService extends IcingaObject )->object_type === 'template'; } + protected function renderSuffix() + { + if ($this->isApplyRule()) { + return $this->renderImportHostVarOverrides() . parent::renderSuffix(); + } else { + return parent::renderSuffix(); + } + } + + protected function renderImportHostVarOverrides() + { + return "\n" . ' import "host var overrides (Director)"' . "\n"; + } + protected function renderCustomExtensions() { // A hand-crafted command endpoint overrides use_agent diff --git a/test/php/library/Director/Objects/rendered/service1.out b/test/php/library/Director/Objects/rendered/service1.out index 01f70251..275f3a8f 100644 --- a/test/php/library/Director/Objects/rendered/service1.out +++ b/test/php/library/Director/Objects/rendered/service1.out @@ -10,5 +10,7 @@ apply Service "___TEST___service" { assign where match("127.*", host.address) assign where host.vars.env == "test" + + import "host var overrides (Director)" } diff --git a/test/php/library/Director/Objects/rendered/service2.out b/test/php/library/Director/Objects/rendered/service2.out index 922a728f..d019fa9d 100644 --- a/test/php/library/Director/Objects/rendered/service2.out +++ b/test/php/library/Director/Objects/rendered/service2.out @@ -10,5 +10,7 @@ apply Service "___TEST___service" { assign where match("128.*", host.address) ignore where host.name == "localhost" + + import "host var overrides (Director)" }