From d4d309a704c4b077966f6ff4e5a67bb6b65440c0 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 26 Apr 2018 13:42:42 +0200 Subject: [PATCH] legacy/IcingaService/ServiceSet: Improve apply rendering This now renders one object per zone with a list of hosts. --- library/Director/Objects/IcingaObject.php | 39 +++++++++++++--- library/Director/Objects/IcingaService.php | 40 +++------------- library/Director/Objects/IcingaServiceSet.php | 46 +++++-------------- 3 files changed, 52 insertions(+), 73 deletions(-) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 622af8d6..386faeb8 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -1847,13 +1847,17 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer /** * @codingStandardsIgnoreStart */ - protected function renderLegacyHost_id() + protected function renderLegacyHost_id($value) { - return $this->renderLegacyRelationProperty( - 'host', - $this->get('host_id'), - 'host_name' - ); + if (is_array($value)) { + return c1::renderKeyValue('host_name', c1::renderArray($value)); + } else { + return $this->renderLegacyRelationProperty( + 'host', + $this->get('host_id'), + 'host_name' + ); + } } /** @@ -2365,6 +2369,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer // @codingStandardsIgnoreEnd if ($this instanceof IcingaHostGroup) { $c = " # resolved memberships are set via the individual object\n"; + } elseif ($this instanceof IcingaService) { + $c = " # resolved objects are listed here\n"; } else { $c = " # assign is not supported for " . $this->type . "\n"; } @@ -2947,6 +2953,27 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer } } + protected function mapHostsToZones($names) + { + $map = array(); + + foreach ($names as $hostname) { + /** @var IcingaHost $host */ + $host = IcingaHost::load($hostname, $this->connection); + + $zone = $host->getRenderingZone(); + if (! array_key_exists($zone, $map)) { + $map[$zone] = array(); + } + + $map[$zone][] = $hostname; + } + + ksort($map); + + return $map; + } + public function getUrlParams() { $params = array(); diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php index a57a4d0e..f3cedbc3 100644 --- a/library/Director/Objects/IcingaService.php +++ b/library/Director/Objects/IcingaService.php @@ -198,7 +198,6 @@ class IcingaService extends IcingaObject public function renderHost_id() { // @codingStandardsIgnoreEnd - if ($this->hasBeenAssignedToHostTemplate()) { return ''; } @@ -231,37 +230,16 @@ class IcingaService extends IcingaObject $assign_filter = $this->get('assign_filter'); $filter = Filter::fromQueryString($assign_filter); - $hosts = HostApplyMatches::forFilter($filter, $conn); + $hostnames = HostApplyMatches::forFilter($filter, $conn); + $this->set('object_type', 'object'); - $this->set('assign_filter', null); - foreach ($hosts as $hostname) { - $file = $this->legacyHostnameServicesFile($hostname, $config); - $this->set('host', $hostname); - $file->addLegacyObject($this); + foreach ($this->mapHostsToZones($hostnames) as $zone => $names) { + $this->set('host_id', $names); + + $config->configFile('director/' . $zone . '/service_apply', '.cfg') + ->addLegacyObject($this); } - - $this->set('host', null); - $this->set('object_type', 'apply'); - $this->set('assign_filter', $assign_filter); - } - - /** - * @param string $hostname - * @param IcingaConfig $config - * @return \Icinga\Module\Director\IcingaConfig\IcingaConfigFile - * @throws \Icinga\Exception\NotFoundError - */ - protected function legacyHostnameServicesFile($hostname, IcingaConfig $config) - { - return $config->configFile( - sprintf( - 'director/%s/service_apply', - IcingaHost::load($hostname, $this->getConnection()) - ->getRenderingZone($config) - ), - '.cfg' - ); } /** @@ -273,10 +251,6 @@ class IcingaService extends IcingaObject return ''; } - if ($this->isApplyRule()) { - throw new InvalidArgumentException('Apply Services can not be rendered directly.'); - } - $str = parent::toLegacyConfigString(); if (! $this->isDisabled() diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php index 21988e19..6253f0da 100644 --- a/library/Director/Objects/IcingaServiceSet.php +++ b/library/Director/Objects/IcingaServiceSet.php @@ -223,49 +223,27 @@ class IcingaServiceSet extends IcingaObject // Delegating this to the service would look, but this way it's faster if ($filter = $this->get('assign_filter')) { $filter = Filter::fromQueryString($filter); - $hosts = HostApplyMatches::forFilter($filter, $conn); + + $hostnames = HostApplyMatches::forFilter($filter, $conn); + } else { + $hostnames = array($this->getRelated('host')->object_name); + } + + foreach ($this->mapHostsToZones($hostnames) as $zone => $names) { + $file = $config->configFile('director/' . $zone . '/servicesets', '.cfg'); + $file->addContent($this->getConfigHeaderComment($config)); + foreach ($this->getServiceObjects() as $service) { $service->set('object_type', 'object'); + $service->set('host_id', $names); + $this->copyVarsToService($service); - foreach ($hosts as $hostname) { - $file = $this->legacyHostnameServicesFile($hostname, $config); - $file->addContent($this->getConfigHeaderComment($config)); - $service->set('host', $hostname); - $file->addLegacyObject($service); - } - } - } else { - foreach ($this->getServiceObjects() as $service) { - $service->set('object_type', 'object'); - $service->set('host_id', $this->get('host_id')); - foreach ($this->vars() as $k => $var) { - $service->$k = $var; - } - $file = $this->legacyRelatedHostFile($service, $config); - $file->addContent($this->getConfigHeaderComment($config)); $file->addLegacyObject($service); } } } - protected function legacyHostnameServicesFile($hostname, IcingaConfig $config) - { - $host = IcingaHost::load($hostname, $this->getConnection()); - return $config->configFile( - 'director/' . $host->getRenderingZone($config) . '/servicesets', - '.cfg' - ); - } - - protected function legacyRelatedHostFile(IcingaService $service, IcingaConfig $config) - { - return $config->configFile( - 'director/' . $service->getRelated('host')->getRenderingZone($config) . '/servicesets', - '.cfg' - ); - } - public function getRenderingZone(IcingaConfig $config = null) { if ($this->get('host_id') === null) {