[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaService: provide assignment properties

git at icinga.org git at icinga.org
Thu Nov 3 16:36:02 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Nov  3 16:35:27 2016 +0100

IcingaService: provide assignment properties

fixes #12731
fixes #13038
fixes #12073

---

 library/Director/Data/PropertiesFilter.php |    1 +
 library/Director/Objects/IcingaService.php |   87 +++++++++++++++++++++++++++-
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/library/Director/Data/PropertiesFilter.php b/library/Director/Data/PropertiesFilter.php
index 8f103b1..a8c3906 100644
--- a/library/Director/Data/PropertiesFilter.php
+++ b/library/Director/Data/PropertiesFilter.php
@@ -6,6 +6,7 @@ class PropertiesFilter
 {
     public static $CUSTOM_PROPERTY = 'CUSTOM_PROPERTY';
     public static $HOST_PROPERTY = 'HOST_PROPERTY';
+    public static $SERVICE_PROPERTY = 'SERVICE_PROPERTY';
 
     protected $blacklist = array(
         'id',
diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php
index 228c4ee..0fea06e 100644
--- a/library/Director/Objects/IcingaService.php
+++ b/library/Director/Objects/IcingaService.php
@@ -2,8 +2,9 @@
 
 namespace Icinga\Module\Director\Objects;
 
-use Icinga\Data\Filter\Filter;
 use Icinga\Exception\ProgrammingError;
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Data\PropertiesFilter;
 use Icinga\Module\Director\IcingaConfig\IcingaConfig;
 use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
 use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
@@ -322,4 +323,88 @@ class IcingaService extends IcingaObject
         }
         return $zone;
     }
+
+    // TODO: Duplicate code, clean this up, split it into multiple methods
+    public static function enumProperties(
+        Db $connection = null,
+        $prefix = '',
+        $filter = null
+    ) {
+        $serviceProperties = array();
+        if ($filter === null) {
+            $filter = new PropertiesFilter();
+        }
+        $realProperties = static::create()->listProperties();
+        sort($realProperties);
+
+        if ($filter->match(PropertiesFilter::$SERVICE_PROPERTY, 'name')) {
+            $serviceProperties[$prefix . 'name'] = 'name';
+        }
+        foreach ($realProperties as $prop) {
+            if (!$filter->match(PropertiesFilter::$SERVICE_PROPERTY, $prop)) {
+                continue;
+            }
+
+            if (substr($prop, -3) === '_id') {
+                $prop = substr($prop, 0, -3);
+            }
+
+            $serviceProperties[$prefix . $prop] = $prop;
+        }
+
+        $serviceVars = array();
+
+        if ($connection !== null) {
+            foreach ($connection->fetchDistinctHostVars() as $var) {
+                if ($filter->match(PropertiesFilter::$CUSTOM_PROPERTY, $var->varname, $var)) {
+                    if ($var->datatype) {
+                        $serviceVars[$prefix . 'vars.' . $var->varname] = sprintf(
+                            '%s (%s)',
+                            $var->varname,
+                            $var->caption
+                        );
+                    } else {
+                        $serviceVars[$prefix . 'vars.' . $var->varname] = $var->varname;
+                    }
+                }
+            }
+        }
+
+        //$properties['vars.*'] = 'Other custom variable';
+        ksort($serviceVars);
+
+        $props = mt('director', 'Service properties');
+        $vars  = mt('director', 'Custom variables');
+
+        $properties = array();
+        if (!empty($serviceProperties)) {
+            $properties[$props] = $serviceProperties;
+            $properties[$props][$prefix . 'groups'] = 'Groups';
+        }
+
+        if (!empty($serviceVars)) {
+            $properties[$vars] = $serviceVars;
+        }
+
+        $hostProps = mt('director', 'Host properties');
+        $hostVars  = mt('director', 'Host Custom variables');
+
+        $hostProperties = IcingaHost::enumProperties($connection);
+
+        if (array_key_exists($hostProps, $hostProperties)) {
+            $p = $hostProperties[$hostProps];
+            if (!empty($p)) {
+                $properties[$hostProps] = $p;
+            }
+        }
+
+        if (array_key_exists($vars, $hostProperties)) {
+            $p = $hostProperties[$vars];
+            if (!empty($p)) {
+                $properties[$hostVars] = $p;
+            }
+        }
+
+        return $properties;
+    }
 }



More information about the icinga-checkins mailing list