IcingaServiceSet: virtual services property...

...should be handled. We can now store a set with it's services
This commit is contained in:
Thomas Gelf 2023-03-07 14:03:09 +01:00
parent c42fc4498a
commit c07758b5d9

View File

@ -15,6 +15,7 @@ use Icinga\Module\Director\Resolver\HostServiceBlacklist;
use InvalidArgumentException; use InvalidArgumentException;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use RuntimeException; use RuntimeException;
use stdClass;
class IcingaServiceSet extends IcingaObject implements ExportInterface class IcingaServiceSet extends IcingaObject implements ExportInterface
{ {
@ -46,6 +47,9 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
'host' => 'IcingaHost', 'host' => 'IcingaHost',
); );
/** @var IcingaService[]|null */
private $services;
public function isDisabled() public function isDisabled()
{ {
return false; return false;
@ -79,6 +83,56 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
} }
/** /**
* @param stdClass[] $services
* @return void
*/
public function setServices(array $services)
{
$existing = $this->getServices();
$uuidMap = [];
foreach ($existing as $service) {
$uuidMap[$service->getUniqueId()->getBytes()] = $service;
}
$this->services = [];
foreach ($services as $service) {
if (isset($service->uuid)) {
$uuid = Uuid::fromString($service->uuid)->getBytes();
$current = $uuidMap[$uuid] ?? IcingaService::create([], $this->connection);
} else {
if (! is_object($service)) {
var_dump($service);
exit;
}
$current = $existing[$service->object_name] ?? IcingaService::create([], $this->connection);
}
$current->setProperties((array) $service);
$this->services[] = $current;
}
}
protected function storeRelatedServices()
{
if ($this->services === null) {
return;
}
$seen = [];
/** @var IcingaService $service */
foreach ($this->services as $service) {
$seen[$service->getUniqueId()->getBytes()] = true;
$service->set('service_set_id', $this->get('id'));
$service->store();
}
foreach ($this->fetchServices() as $service) {
if (!isset($seen[$service->getUniqueId()->getBytes()])) {
$service->delete();
}
}
}
/**
* @deprecated
* @return IcingaService[] * @return IcingaService[]
* @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Exception\NotFoundError
*/ */
@ -516,7 +570,23 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
/** /**
* @return IcingaService[] * @return IcingaService[]
*/ */
public function fetchServices() public function getServices(): array
{
if ($this->services !== null) {
return $this->services;
}
if ($this->hasBeenLoadedFromDb()) {
return $this->fetchServices();
}
return [];
}
/**
* @return IcingaService[]
*/
public function fetchServices(): array
{ {
if ($store = self::$dbObjectStore) { if ($store = self::$dbObjectStore) {
$uuid = $store->getBranch()->getUuid(); $uuid = $store->getBranch()->getUuid();
@ -580,6 +650,24 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
} }
} }
public function onStore()
{
$this->storeRelatedServices();
}
public function hasBeenModified()
{
if ($this->services !== null) {
foreach ($this->services as $service) {
if ($service->hasBeenModified()) {
return true;
}
}
}
return parent::hasBeenModified();
}
public function toSingleIcingaConfig() public function toSingleIcingaConfig()
{ {
$config = parent::toSingleIcingaConfig(); $config = parent::toSingleIcingaConfig();