[icinga-checkins] icinga.org: icingaweb2-module-director/feature/nested-apply-rules-12033: Add support for expressions

git at icinga.org git at icinga.org
Sun Oct 23 06:25:31 CEST 2016


Module: icingaweb2-module-director
Branch: feature/nested-apply-rules-12033
Commit: bcef87f4c98c75ccabfef0a1bde60e2a4201cf81
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=bcef87f4c98c75ccabfef0a1bde60e2a4201cf81

Author: Corentin Ardeois <cardeois at iweb.com>
Date:   Fri Jul 29 15:58:15 2016 -0400

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

---

 .../Director/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 71e4a5b..c163bfb 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 c73b5bb..8007537 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 + "\\\\$"');
+    }
 }



More information about the icinga-checkins mailing list