From fca804e74ec209bd06507c2d74511e4b77c1f3ba Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 21 Jul 2022 07:49:12 +0200 Subject: [PATCH] ObjectCommand: --all-services fixes #2571 --- library/Director/Cli/ObjectCommand.php | 8 ++-- library/Director/Data/Exporter.php | 59 +++++++++++++++++++++----- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/library/Director/Cli/ObjectCommand.php b/library/Director/Cli/ObjectCommand.php index 58df3b00..26de5e4a 100644 --- a/library/Director/Cli/ObjectCommand.php +++ b/library/Director/Cli/ObjectCommand.php @@ -47,7 +47,7 @@ class ObjectCommand extends Command * --no-defaults Per default JSON output ships null or default * values. This flag skips those properties * --with-services For hosts only, also shows attached services - * --resolve-services For hosts only, show applied and inherited services + * --all-services For hosts only, show applied and inherited services * too */ public function showAction() @@ -57,16 +57,16 @@ class ObjectCommand extends Command $exporter = new Exporter($db); $resolve = (bool) $this->params->shift('resolved'); $withServices = (bool) $this->params->get('with-services'); - $resolveServices = (bool) $this->params->get('resolve-services'); + $allServices = (bool) $this->params->get('all-services'); if ($withServices) { if (!$object instanceof IcingaHost) { $this->fail('--with-services is available for Hosts only'); } $exporter->enableHostServices(); } - if ($resolveServices) { + if ($allServices) { if (!$object instanceof IcingaHost) { - $this->fail('--resolve-services is available for Hosts only'); + $this->fail('--all-services is available for Hosts only'); } $exporter->resolveHostServices(); } diff --git a/library/Director/Data/Exporter.php b/library/Director/Data/Exporter.php index 125676f3..341f8513 100644 --- a/library/Director/Data/Exporter.php +++ b/library/Director/Data/Exporter.php @@ -65,7 +65,7 @@ class Exporter protected $fieldReferenceLoader; protected $exportHostServices = false; - protected $resolveHostServices = false; + protected $fetchAllHostServices = false; protected $showDefaults = false; protected $showIds = false; protected $resolveObjects = false; @@ -115,7 +115,7 @@ class Exporter public function resolveHostServices($enable = true) { - $this->resolveHostServices = $enable; + $this->fetchAllHostServices = $enable; return $this; } @@ -208,8 +208,8 @@ class Exporter { $table = (new ObjectsTableService($this->connection))->setHost($host); $services = $this->fetchServicesForTable($table); - if ($this->resolveHostServices) { - foreach ($this->fetchRelatedServicesForHost($host) as $service) { + if ($this->fetchAllHostServices) { + foreach ($this->fetchAllServicesForHost($host) as $service) { $services[] = $service; } } @@ -240,7 +240,7 @@ class Exporter return $services; } - protected function fetchRelatedServicesForHost(IcingaHost $host) + protected function fetchAllServicesForHost(IcingaHost $host) { $services = []; /** @var IcingaHost[] $parents */ @@ -253,12 +253,16 @@ class Exporter $services[] = $service; } } -/* - $this->addHostServiceSetTables($host); - foreach ($parents as $parent) { - $this->addHostServiceSetTables($parent, $host); + + foreach ($this->getHostServiceSetTables($host) as $service) { + $services[] = $service; } -*/ + foreach ($parents as $parent) { + foreach ($this->getHostServiceSetTables($parent, $host) as $service) { + $services[] = $service; + } + } + $appliedSets = AppliedServiceSetLoader::fetchForHost($host); foreach ($appliedSets as $set) { $table = IcingaServiceSetServiceTable::load($set) @@ -277,6 +281,41 @@ class Exporter return $services; } + /** + * Duplicates Logic in HostController + * + * @param IcingaHost $host + * @param IcingaHost|null $affectedHost + * @return IcingaServiceSetServiceTable[] + */ + protected function getHostServiceSetTables(IcingaHost $host, IcingaHost $affectedHost = null) + { + $tables = []; + $db = $this->connection; + if ($affectedHost === null) { + $affectedHost = $host; + } + if ($host->get('id') === null) { + return $tables; + } + + $query = $db->getDbAdapter()->select() + ->from(['ss' => 'icinga_service_set'], 'ss.*') + ->join(['hsi' => 'icinga_service_set_inheritance'], 'hsi.parent_service_set_id = ss.id', []) + ->join(['hs' => 'icinga_service_set'], 'hs.id = hsi.service_set_id', []) + ->where('hs.host_id = ?', $host->get('id')); + + $sets = IcingaServiceSet::loadAll($db, $query, 'object_name'); + /** @var IcingaServiceSet $set*/ + foreach ($sets as $name => $set) { + $tables[] = IcingaServiceSetServiceTable::load($set) + ->setHost($host) + ->setAffectedHost($affectedHost); + } + + return $tables; + } + protected function loadTemplateName($table, $id) { $db = $this->db;