[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaService: support and test speciall object...

git at icinga.org git at icinga.org
Tue Oct 25 04:39:56 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 9ba6116091b121a34aeb007910b6b85470e1e894
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=9ba6116091b121a34aeb007910b6b85470e1e894

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sat Oct 22 06:54:53 2016 +0000

IcingaService: support and test speciall object...

...name variants with apply for

---

 library/Director/IcingaConfig/IcingaConfigHelper.php   |    5 +++++
 library/Director/Objects/IcingaService.php             |    9 +++++++--
 .../php/library/Director/Objects/IcingaServiceTest.php |   14 +++++++++++++-
 .../php/library/Director/Objects/rendered/service6.out |   16 ++++++++++++++++
 .../php/library/Director/Objects/rendered/service7.out |   15 +++++++++++++++
 5 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php
index f76ad16..f0fccfd 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('/(?<!\$)\$[\w\.]+\$(?!\$)/', $string);
+    }
+
     public static function renderStringWithVariables($string)
     {
         $string = preg_replace(
diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php
index ca3bcd4..3f59680 100644
--- a/library/Director/Objects/IcingaService.php
+++ b/library/Director/Objects/IcingaService.php
@@ -173,7 +173,12 @@ class IcingaService extends IcingaObject
             && $this->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 ebc4d37..bfc8101 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 0000000..d41bd85
--- /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 0000000..96c3e49
--- /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
+}
+



More information about the icinga-checkins mailing list