[icinga-checkins] icinga.org: icingaweb2-module-director/feature/icinga-legacy-config-13049: IcingaHostGroup(legacy): Render assign to a hostgroup_member list

git at icinga.org git at icinga.org
Mon Nov 7 16:46:59 CET 2016


Module: icingaweb2-module-director
Branch: feature/icinga-legacy-config-13049
Commit: e61306725fe51131403e3fa27f209beb16531c42
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=e61306725fe51131403e3fa27f209beb16531c42

Author: Markus Frosch <markus.frosch at icinga.com>
Date:   Mon Nov  7 16:41:00 2016 +0100

IcingaHostGroup(legacy): Render assign to a hostgroup_member list

Supporting active-passive and masterless, by writing multiple versions of the object.

refs #12891

---

 library/Director/Objects/IcingaHostGroup.php |   99 ++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)

diff --git a/library/Director/Objects/IcingaHostGroup.php b/library/Director/Objects/IcingaHostGroup.php
index 4570c31..5c7959c 100644
--- a/library/Director/Objects/IcingaHostGroup.php
+++ b/library/Director/Objects/IcingaHostGroup.php
@@ -2,6 +2,11 @@
 
 namespace Icinga\Module\Director\Objects;
 
+use Icinga\Data\Filter\Filter;
+use Icinga\Exception\ProgrammingError;
+use Icinga\Module\Director\IcingaConfig\IcingaConfig;
+use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
+
 class IcingaHostGroup extends IcingaObjectGroup
 {
     protected $table = 'icinga_hostgroup';
@@ -10,4 +15,98 @@ class IcingaHostGroup extends IcingaObjectGroup
     {
         return true;
     }
+
+    public function renderToLegacyConfig(IcingaConfig $config)
+    {
+        if ($this->get('assign_filter') !== null) {
+            $this->renderLegacyApplyToConfig($config);
+        } else {
+            parent::renderToLegacyConfig($config);
+        }
+    }
+
+    /**
+     * @param  IcingaConfig  $config
+     *
+     * @throws ProgrammingError  When IcingaConfig deployment mode is not supported
+     */
+    protected function renderLegacyApplyToConfig(IcingaConfig $config)
+    {
+        $conn = $this->getConnection();
+
+        $filter = Filter::fromQueryString($this->get('assign_filter'));
+        $hosts = HostApplyMatches::forFilter($filter, $conn);
+        $this->set('object_type', 'object');
+
+        $zoneMap = array();
+
+        foreach ($hosts as $hostname) {
+            $host = IcingaHost::load($hostname, $this->connection);
+
+            if (($zoneId = $host->getResolvedProperty('zone_id')) !== null) {
+                $zoneMap[$zoneId][] = $hostname;
+            } else {
+                $zoneMap[0][] = $hostname;
+            }
+        }
+
+        if (empty($zoneMap)) {
+            // no hosts matched
+            $file = $this->legacyZoneHostgroupFile($config);
+            $this->properties['hostgroup_members'] = array();
+            $file->addLegacyObject($this);
+
+        } else {
+            $allMembers = array();
+
+            foreach ($zoneMap as $zoneId => $members) {
+                $file = $this->legacyZoneHostgroupFile($config, $zoneId);
+                $this->properties['hostgroup_members'] = $members;
+                $file->addLegacyObject($this);
+
+                $allMembers = array_merge($allMembers, $members);
+            }
+
+            $deploymentMode = $config->getDeploymentMode();
+            if ($deploymentMode === 'active-passive') {
+                $this->properties['hostgroup_members'] = $allMembers;
+                $this->legacyZoneHostgroupFile($config, 0)
+                    ->addLegacyObject($this);
+            } else if ($deploymentMode == 'masterless') {
+                // nothing to add
+            } else {
+                throw new ProgrammingError('Unsupported deployment mode: %s' . $deploymentMode);
+            }
+        }
+    }
+
+    protected function legacyZoneHostgroupFile(IcingaConfig $config, $zoneId = null)
+    {
+        if ($zoneId !== null) {
+            $zone = IcingaZone::load($zoneId, $this->getConnection())->getObjectName();
+        } else {
+            $zone = $this->connection->getDefaultGlobalZoneName();
+        }
+        return $config->configFile(
+            'director/' . $zone . '/hostgroups'
+        );
+    }
+
+    protected function renderLegacyHostgroup_members()
+    {
+        if (empty($this->properties['hostgroup_members'])) {
+            return '';
+        }
+        return c1::renderKeyValue('hostgroup_members', join(',', $this->properties['hostgroup_members']));
+    }
+
+    /**
+     * Note: rendered with renderLegacyHostgroup_members()
+     *
+     * @return string
+     */
+    protected function renderLegacyAssign_filter()
+    {
+        return '';
+    }
 }



More information about the icinga-checkins mailing list