IcingaService: support and test speciall object...

...name variants with apply for
This commit is contained in:
Thomas Gelf 2016-10-22 06:54:53 +00:00
parent 4c9de0fa34
commit 9ba6116091
5 changed files with 56 additions and 3 deletions

View File

@ -258,6 +258,11 @@ class IcingaConfigHelper
return $seconds . 's'; return $seconds . 's';
} }
public static function stringHasMacro($string)
{
return preg_match('/(?<!\$)\$[\w\.]+\$(?!\$)/', $string);
}
public static function renderStringWithVariables($string) public static function renderStringWithVariables($string)
{ {
$string = preg_replace( $string = preg_replace(

View File

@ -173,7 +173,12 @@ class IcingaService extends IcingaObject
&& $this->get('apply_for') !== null) { && $this->get('apply_for') !== null) {
$name = $this->getObjectName(); $name = $this->getObjectName();
if ($name !== '') { $extraName = '';
if (c::stringHasMacro($name)) {
$extraName = c::renderKeyValue('name', c::renderStringWithVariables($name));
$name = '';
} elseif ($name !== '') {
$name = ' ' . c::renderString($name); $name = ' ' . c::renderString($name);
} }
@ -183,7 +188,7 @@ class IcingaService extends IcingaObject
$this->getType(), $this->getType(),
$name, $name,
$this->get('apply_for') $this->get('apply_for')
); ) . $extraName;
} }
return parent::renderObjectHeader(); return parent::renderObjectHeader();
} }

View File

@ -279,7 +279,7 @@ class IcingaServiceTest extends BaseTestCase
); );
} }
public function testApplyFor() public function testApplyForRendersInVariousModes()
{ {
if ($this->skipForMissingDb()) { if ($this->skipForMissingDb()) {
return; return;
@ -297,6 +297,18 @@ class IcingaServiceTest extends BaseTestCase
$this->loadRendered('service5'), $this->loadRendered('service5'),
(string) $service (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() protected function host()

View File

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

View File

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