diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/DirectorObject/Lookup/AppliedServiceInfo.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/Director/DirectorObject/Lookup/AppliedServiceInfo.php | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/library/Director/DirectorObject/Lookup/AppliedServiceInfo.php b/library/Director/DirectorObject/Lookup/AppliedServiceInfo.php new file mode 100644 index 0000000..abda497 --- /dev/null +++ b/library/Director/DirectorObject/Lookup/AppliedServiceInfo.php @@ -0,0 +1,109 @@ +<?php + +namespace Icinga\Module\Director\DirectorObject\Lookup; + +use gipfl\IcingaWeb2\Url; +use Icinga\Data\Filter\Filter; +use Icinga\Module\Director\Db; +use Icinga\Module\Director\Objects\HostApplyMatches; +use Icinga\Module\Director\Objects\IcingaHost; +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; + +/** + * A Service Apply Rule matching this Host, generating a Service with the given + * name + */ +class AppliedServiceInfo implements ServiceInfo +{ + /** @var string */ + protected $hostName; + + /** @var string */ + protected $serviceName; + + /** @var int */ + protected $serviceApplyRuleId; + + /** @var UuidInterface */ + protected $uuid; + + public function __construct($hostName, $serviceName, $serviceApplyRuleId, UuidInterface $uuid) + { + $this->hostName = $hostName; + $this->serviceName= $serviceName; + $this->serviceApplyRuleId = $serviceApplyRuleId; + $this->uuid = $uuid; + } + + public static function find(IcingaHost $host, $serviceName) + { + $matcher = HostApplyMatches::prepare($host); + $connection = $host->getConnection(); + foreach (static::fetchApplyRulesByServiceName($connection, $serviceName) as $rule) { + if ($matcher->matchesFilter($rule->filter)) { + return new static($host->getObjectName(), $serviceName, (int) $rule->id, $rule->uuid); + } + } + + return null; + } + + public function getHostName() + { + return $this->hostName; + } + + /** + * @return int + */ + public function getServiceApplyRuleId() + { + return $this->serviceApplyRuleId; + } + + public function getName() + { + return $this->serviceName; + } + + public function getUuid() + { + return $this->uuid; + } + + public function getUrl() + { + return Url::fromPath('director/host/appliedservice', [ + 'name' => $this->hostName, + 'service_id' => $this->serviceApplyRuleId, + ]); + } + + public function requiresOverrides() + { + return true; + } + + protected static function fetchApplyRulesByServiceName(Db $connection, $serviceName) + { + $db = $connection->getDbAdapter(); + $query = $db->select() + ->from(['s' => 'icinga_service'], [ + 'id' => 's.id', + 'uuid' => 's.uuid', + 'name' => 's.object_name', + 'assign_filter' => 's.assign_filter', + ]) + ->where('object_name = ?', $serviceName) + ->where('object_type = ? AND assign_filter IS NOT NULL', 'apply'); + + $allRules = $db->fetchAll($query); + foreach ($allRules as $rule) { + $rule->uuid = Uuid::fromBytes(Db\DbUtil::binaryResult($rule->uuid)); + $rule->filter = Filter::fromQueryString($rule->assign_filter); + } + + return $allRules; + } +} |