diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index fb54d0ea..bdccad40 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -18,6 +18,7 @@ before switching to a new version. * FIX: Activity Log used to ignore Host filters (#1613) * FIX: Basket failed to restore depending on PHP version (#1782) * FIX: Loop detection works again (#1631) +* FIX: Snapshots for Baskets with Dependencies are now possible (#1739) * FEATURE: RO users could want to see where a configured service originated (#1785) ### REST API diff --git a/library/Director/DirectorObject/Automation/BasketSnapshot.php b/library/Director/DirectorObject/Automation/BasketSnapshot.php index 3cf6dc77..bb7fe2bd 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -7,6 +7,7 @@ use Icinga\Module\Director\Db; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Objects\DirectorDatafield; use Icinga\Module\Director\Objects\IcingaCommand; +use Icinga\Module\Director\Objects\IcingaDependency; use Icinga\Module\Director\Objects\IcingaObject; use InvalidArgumentException; use RuntimeException; @@ -378,7 +379,7 @@ class BasketSnapshot extends DbObject if ($dummy instanceof IcingaCommand) { $select = $db->select()->from($dummy->getTableName()) ->where('object_type != ?', 'external_object'); - } elseif (! $dummy->isGroup()) { + } elseif (! $dummy->isGroup() && ! $dummy instanceof IcingaDependency) { $select = $db->select()->from($dummy->getTableName()) ->where('object_type = ?', 'template'); } else { diff --git a/library/Director/Objects/IcingaDependency.php b/library/Director/Objects/IcingaDependency.php index efd481d5..65b47eaa 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -3,11 +3,14 @@ namespace Icinga\Module\Director\Objects; use Icinga\Exception\ConfigurationError; +use Icinga\Module\Director\Db; +use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; +use Icinga\Module\Director\Exception\DuplicateKeyException; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Exception\NotFoundError; use Icinga\Data\Filter\Filter; -class IcingaDependency extends IcingaObject +class IcingaDependency extends IcingaObject implements ExportInterface { protected $table = 'icinga_dependency'; @@ -55,6 +58,53 @@ class IcingaDependency extends IcingaObject 'ignore_soft_states' => 'ignore_soft_states' ]; + public function getUniqueIdentifier() + { + return $this->getObjectName(); + } + + /** + * @return object + * @throws \Icinga\Exception\NotFoundError + */ + public function export() + { + $props = (array) $this->toPlainObject(); + ksort($props); + + return (object) $props; + } + + /** + * @param $plain + * @param Db $db + * @param bool $replace + * @return static + * @throws DuplicateKeyException + * @throws \Icinga\Exception\NotFoundError + */ + public static function import($plain, Db $db, $replace = false) + { + $properties = (array) $plain; + $name = $properties['object_name']; + $key = $name; + + if ($replace && static::exists($key, $db)) { + $object = static::load($key, $db); + } elseif (static::exists($key, $db)) { + throw new DuplicateKeyException( + 'Service Template "%s" already exists', + $name + ); + } else { + $object = static::create([], $db); + } + + $object->setProperties($properties); + + return $object; + } + /** * Do not render internal property apply_to *