From bcef87f4c98c75ccabfef0a1bde60e2a4201cf81 Mon Sep 17 00:00:00 2001 From: Corentin Ardeois Date: Fri, 29 Jul 2016 15:58:15 -0400 Subject: [PATCH] Add support for expressions Any rendered string can contain variables encapsulated with "$$" characters. Example: Display Name declared with `Port $$host.vars.tcp_port$$ check` will be processed as `"Port " + host.vars.tcp_port + " check"` API: ```bash ./director-curl POST director/service?name=my_service '{"display_name": "Port $$host.vars.tcp_port$$ check" }' ``` Rendered config: ``` apply Service "my_service" { import "my_template" display_name = "Port " + host.vars.tcp_port + " check" } ``` refs #11976 --- .../IcingaConfig/IcingaConfigHelper.php | 6 ++++- .../IcingaConfig/IcingaConfigHelperTest.php | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 71e4a5bc..c163bfb0 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -100,7 +100,7 @@ class IcingaConfigHelper $string = preg_replace($special, $replace, $string); - return '"' . $string . '"'; + return '"' . self::renderVariablesAsExpression($string) . '"'; } public static function renderDictionaryKey($key) @@ -257,4 +257,8 @@ class IcingaConfigHelper return $seconds . 's'; } + + private static function renderVariablesAsExpression($string) { + return preg_replace('/\$\$([\w\.]+)\$\$/', '" + ${1} + "', $string); + } } diff --git a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php index c73b5bb8..80075370 100644 --- a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php +++ b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php @@ -69,4 +69,30 @@ class IcingaConfigHelperTest extends BaseTestCase { return file_get_contents(__DIR__ . '/rendered/' . $name . '.out'); } + + public function testRenderStringIsCorrectlyRendered() + { + $this->assertEquals(c::renderString('val1\\\val2'), '"val1\\\\\\\\val2"'); + $this->assertEquals(c::renderString('"val1"'), '"\"val1\""'); + $this->assertEquals(c::renderString('\$val\$'), '"\\\\$val\\\\$"'); + $this->assertEquals(c::renderString('\t'), '"\\\\t"'); + $this->assertEquals(c::renderString('\r'), '"\\\\r"'); + $this->assertEquals(c::renderString('\n'), '"\\\\n"'); + $this->assertEquals(c::renderString('\f'), '"\\\\f"'); + } + + public function testRenderStringWithVariables() + { + $this->assertEquals( + c::renderString('Before $$name$$ $$name$$ After'), + '"Before " + name + " " + name + " After"'); + $this->assertEquals( + c::renderString('Before $$var1$$ $$var2$$ After'), + '"Before " + var1 + " " + var2 + " After"'); + $this->assertEquals(c::renderString('$$host.vars.custom$$'), '"" + host.vars.custom + ""'); + $this->assertEquals(c::renderString('$$var"$$'), '"$$var\"$$"'); + $this->assertEquals( + c::renderString('\tI am\rrendering\nproperly\fand I $$support$$ "multiple" $$variables$$\$'), + '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'); + } }