Skip to content

Commit

Permalink
Restore services of service set on restoring service set
Browse files Browse the repository at this point in the history
All the services which were under the service set when it was deleted must be restored when it is restored from activity log.
  • Loading branch information
raviks789 authored and nilmerg committed Feb 7, 2024
1 parent 6351df6 commit e48ddd2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
15 changes: 14 additions & 1 deletion library/Director/Objects/DirectorActivityLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Authentication\Auth;
use Icinga\Application\Icinga;
use Icinga\Application\Logger;
use stdClass;

class DirectorActivityLog extends DbObject
{
Expand Down Expand Up @@ -176,7 +177,19 @@ public static function logRemoval(IcingaObject $object, Db $db)
{
$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,
Expand Down
32 changes: 31 additions & 1 deletion library/Director/Objects/IcingaServiceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,7 +134,12 @@ public function setServices(array $services)
protected function storeRelatedServices()
{
if ($this->services === null) {
return;
$cachedServices = $this->getCachedServices();
if ($cachedServices) {
$this->services = $cachedServices;
} else {
return;
}
}

$seen = [];
Expand Down Expand Up @@ -179,6 +208,7 @@ public function getUniqueIdentifier()

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
Expand Down
43 changes: 41 additions & 2 deletions library/Director/Web/Widget/ActivityLogInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -433,11 +436,30 @@ protected function objectToConfig(IcingaObject $object)
{
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) {
Expand Down Expand Up @@ -624,10 +646,27 @@ protected function createObject($type, $props)
$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);
}
}

0 comments on commit e48ddd2

Please sign in to comment.