diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index f76ad167..f0fccfd9 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -258,6 +258,11 @@ class IcingaConfigHelper return $seconds . 's'; } + public static function stringHasMacro($string) + { + return preg_match('/(?get('apply_for') !== null) { $name = $this->getObjectName(); - if ($name !== '') { + $extraName = ''; + + if (c::stringHasMacro($name)) { + $extraName = c::renderKeyValue('name', c::renderStringWithVariables($name)); + $name = ''; + } elseif ($name !== '') { $name = ' ' . c::renderString($name); } @@ -183,7 +188,7 @@ class IcingaService extends IcingaObject $this->getType(), $name, $this->get('apply_for') - ); + ) . $extraName; } return parent::renderObjectHeader(); } diff --git a/test/php/library/Director/Objects/IcingaServiceTest.php b/test/php/library/Director/Objects/IcingaServiceTest.php index ebc4d378..bfc8101c 100644 --- a/test/php/library/Director/Objects/IcingaServiceTest.php +++ b/test/php/library/Director/Objects/IcingaServiceTest.php @@ -279,7 +279,7 @@ class IcingaServiceTest extends BaseTestCase ); } - public function testApplyFor() + public function testApplyForRendersInVariousModes() { if ($this->skipForMissingDb()) { return; @@ -297,6 +297,18 @@ class IcingaServiceTest extends BaseTestCase $this->loadRendered('service5'), (string) $service ); + + $service->object_name = '___TEST$config$___service $host.var.bla$'; + $this->assertEquals( + $this->loadRendered('service6'), + (string) $service + ); + + $service->object_name = ''; + $this->assertEquals( + $this->loadRendered('service7'), + (string) $service + ); } protected function host() diff --git a/test/php/library/Director/Objects/rendered/service6.out b/test/php/library/Director/Objects/rendered/service6.out new file mode 100644 index 00000000..d41bd852 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/service6.out @@ -0,0 +1,16 @@ +apply Service for (config in host.vars.test1) { + name = "___TEST" + config + "___service " + host.var.bla + display_name = "Whatever service" + vars.test1 = "string" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + a = [ "dict", "ionary" ] + @this = "is" + } + + assign where host.vars.env == "test" + + import DirectorOverrideTemplate +} + diff --git a/test/php/library/Director/Objects/rendered/service7.out b/test/php/library/Director/Objects/rendered/service7.out new file mode 100644 index 00000000..96c3e498 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/service7.out @@ -0,0 +1,15 @@ +apply Service for (config in host.vars.test1) { + display_name = "Whatever service" + vars.test1 = "string" + vars.test2 = 17 + vars.test3 = false + vars.test4 = { + a = [ "dict", "ionary" ] + @this = "is" + } + + assign where host.vars.env == "test" + + import DirectorOverrideTemplate +} +