IcingaServiceSet: use query builder to retrieve...

...services, this is required for branches
This commit is contained in:
Thomas Gelf 2022-09-20 12:55:55 +02:00
parent c56b190469
commit b19dd5f62d
1 changed files with 23 additions and 31 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Objects;
use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Data\Db\ServiceSetQueryBuilder;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\Db\DbUtil;
@ -104,30 +105,20 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
/**
* @param IcingaServiceSet $set
* @return array
* @return IcingaService[]
* @throws \Icinga\Exception\NotFoundError
*/
protected function getServiceObjectsForSet(IcingaServiceSet $set)
{
if ($set->get('id') === null) {
return array();
}
$connection = $this->getConnection();
$db = $this->getDb();
$uuids = $db->fetchCol(
$db->select()->from('icinga_service', 'uuid')
->where('service_set_id = ?', $set->get('id'))
);
$services = array();
foreach ($uuids as $uuid) {
$service = IcingaService::loadWithUniqueId(Uuid::fromBytes(DbUtil::binaryResult($uuid)), $connection);
$service->set('service_set', null);
$services[$service->getObjectName()] = $service;
if (self::$dbObjectStore !== null) {
$branchUuid = self::$dbObjectStore->getBranch()->getUuid();
} else {
$branchUuid = null;
}
return $services;
$builder = new ServiceSetQueryBuilder($connection, $branchUuid);
return $builder->fetchServicesWithQuery($builder->selectServicesForSet($set));
}
public function getUniqueIdentifier()
@ -487,11 +478,16 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
public function getRenderingZone(IcingaConfig $config = null)
{
if ($this->get('host_id') === null) {
return $this->connection->getDefaultGlobalZoneName();
if ($hostname = $this->get('host')) {
$host = IcingaHost::load($hostname, $this->getConnection());
} else {
return $this->connection->getDefaultGlobalZoneName();
}
} else {
$host = $this->getRelatedObject('host', $this->get('host_id'));
return $host->getRenderingZone($config);
}
return $host->getRenderingZone($config);
}
public function createWhere()
@ -511,17 +507,13 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
*/
public function fetchServices()
{
$connection = $this->getConnection();
$db = $connection->getDbAdapter();
/** @var IcingaService[] $services */
$services = IcingaService::loadAll(
$connection,
$db->select()->from('icinga_service')
->where('service_set_id = ?', $this->get('id'))
);
return $services;
if ($store = self::$dbObjectStore) {
$uuid = $store->getBranch()->getUuid();
} else {
$uuid = null;
}
$builder = new ServiceSetQueryBuilder($this->getConnection(), $uuid);
return $builder->fetchServicesWithQuery($builder->selectServicesForSet($this));
}
/**
@ -561,7 +553,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
$name = $this->getObjectName();
if ($this->isObject() && $this->get('host_id') === null) {
if ($this->isObject() && $this->get('host_id') === null && $this->get('host') === null) {
throw new InvalidArgumentException(
'A Service Set cannot be an object with no related host'
);