Restore services of service set on restoring service set

All the services which were under the service set when it was deleted must be restored when it is restored from activity log.
This commit is contained in:
raviks789 2024-02-02 12:52:54 +01:00 committed by Johannes Meyer
parent 6351df68ea
commit e48ddd2f35
3 changed files with 86 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use Icinga\Module\Director\Db;
use Icinga\Authentication\Auth;
use Icinga\Application\Icinga;
use Icinga\Application\Logger;
use stdClass;
class DirectorActivityLog extends DbObject
{
@ -176,7 +177,19 @@ class DirectorActivityLog extends DbObject
{
$name = $object->getObjectName();
$type = $object->getTableName();
$oldProps = json_encode($object->getPlainUnmodifiedObject());
/** @var stdClass $plainUnmodifiedObject */
$plainUnmodifiedObject = $object->getPlainUnmodifiedObject();
if ($object instanceof IcingaServiceSet) {
$services = [];
foreach ($object->getCachedServices() as $service) {
$services[$service->getObjectName()] = $service->toPlainObject();
}
$plainUnmodifiedObject->services = $services;
}
$oldProps = json_encode($plainUnmodifiedObject);
$data = [
'object_name' => $name,

View File

@ -44,9 +44,33 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
'host' => 'IcingaHost',
);
/** @var IcingaService[] Cached services */
protected $cachedServices = [];
/** @var IcingaService[]|null */
private $services;
/**
* Set the services to be cached
*
* @param $services IcingaService[]
* @return void
*/
public function setCachedServices($services)
{
$this->cachedServices = $services;
}
/**
* Get the cached services
*
* @return IcingaService[]
*/
public function getCachedServices()
{
return $this->cachedServices;
}
public function isDisabled()
{
return false;
@ -110,7 +134,12 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
protected function storeRelatedServices()
{
if ($this->services === null) {
return;
$cachedServices = $this->getCachedServices();
if ($cachedServices) {
$this->services = $cachedServices;
} else {
return;
}
}
$seen = [];
@ -179,6 +208,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
public function beforeDelete()
{
$this->setCachedServices($this->getServices());
// check if this is a template, or directly assigned to a host
if ($this->get('host_id') === null) {
// find all host sets and delete them

View File

@ -3,7 +3,10 @@
namespace Icinga\Module\Director\Web\Widget;
use gipfl\Json\JsonString;
use Icinga\Module\Director\Data\FieldReferenceLoader;
use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshotFieldResolver;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use Icinga\Date\DateFormatter;
@ -433,11 +436,30 @@ class ActivityLogInfo extends HtmlDocument
{
if ($object instanceof IcingaService) {
return $this->previewService($object);
} elseif ($object instanceof IcingaServiceSet) {
return $this->previewServiceSet($object);
} else {
return $object->toSingleIcingaConfig();
}
}
/**
* Render service set to be previewed
*
* @param IcingaServiceSet $object
*
* @return IcingaConfig
*/
protected function previewServiceSet(IcingaServiceSet $object)
{
$config = $object->toSingleIcingaConfig();
foreach ($object->getCachedServices() as $service) {
$service->renderToConfig($config);
}
return $config;
}
protected function previewService(IcingaService $service)
{
if (($set = $service->get('service_set')) !== null) {
@ -624,10 +646,27 @@ class ActivityLogInfo extends HtmlDocument
$newProps['object_type'] = $props->object_type;
}
return IcingaObject::createByType(
$object = IcingaObject::createByType(
$type,
$newProps,
$this->db
)->setProperties((array) $props);
);
if ($type === 'icinga_service_set' && isset($props->services)) {
$services = [];
foreach ($props->services as $service) {
$services[$service->object_name] = IcingaObject::createByType(
'icinga_service',
(array) $service,
$this->db
);
}
/** @var IcingaServiceSet $object */
$object->setCachedServices($services);
unset($props->services);
}
return $object->setProperties((array) $props);
}
}