diff --git a/application/forms/BasketForm.php b/application/forms/BasketForm.php index 16a68bf4..a9924580 100644 --- a/application/forms/BasketForm.php +++ b/application/forms/BasketForm.php @@ -23,6 +23,7 @@ class BasketForm extends DirectorObjectForm 'ServiceTemplate' => $this->translate('Service Templates'), 'ServiceSet' => $this->translate('Service Sets'), 'Notification' => $this->translate('Notifications'), + 'TimePeriod' => $this->translate('Time Periods'), 'Dependency' => $this->translate('Dependencies'), 'DataList' => $this->translate('Data Lists'), 'ImportSource' => $this->translate('Import Sources'), diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index bdccad40..34d41c76 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -19,6 +19,7 @@ before switching to a new version. * 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: Add TimePeriod support to Configuration Baskets (#1735) * 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 bb7fe2bd..d1b61ed2 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -9,6 +9,7 @@ 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 Icinga\Module\Director\Objects\IcingaTimePeriod; use InvalidArgumentException; use RuntimeException; @@ -16,6 +17,7 @@ class BasketSnapshot extends DbObject { protected static $typeClasses = [ 'Datafield' => '\\Icinga\\Module\\Director\\Objects\\DirectorDatafield', + 'TimePeriod' => '\\Icinga\\Module\\Director\\Objects\\IcingaTimePeriod', 'Command' => '\\Icinga\\Module\\Director\\Objects\\IcingaCommand', 'HostGroup' => '\\Icinga\\Module\\Director\\Objects\\IcingaHostGroup', 'IcingaTemplateChoiceHost' => '\\Icinga\\Module\\Director\\Objects\\IcingaTemplateChoiceHost', @@ -46,6 +48,7 @@ class BasketSnapshot extends DbObject protected $restoreOrder = [ 'Command', + 'TimePeriod', 'HostGroup', 'IcingaTemplateChoiceHost', 'HostTemplate', @@ -379,7 +382,11 @@ class BasketSnapshot extends DbObject if ($dummy instanceof IcingaCommand) { $select = $db->select()->from($dummy->getTableName()) ->where('object_type != ?', 'external_object'); - } elseif (! $dummy->isGroup() && ! $dummy instanceof IcingaDependency) { + } elseif (! $dummy->isGroup() + // TODO: this is ugly. + && ! $dummy instanceof IcingaDependency + && ! $dummy instanceof IcingaTimePeriod + ) { $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 65b47eaa..d5324682 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -93,7 +93,7 @@ class IcingaDependency extends IcingaObject implements ExportInterface $object = static::load($key, $db); } elseif (static::exists($key, $db)) { throw new DuplicateKeyException( - 'Service Template "%s" already exists', + 'Dependency "%s" already exists', $name ); } else { diff --git a/library/Director/Objects/IcingaTimePeriod.php b/library/Director/Objects/IcingaTimePeriod.php index 2c459c35..1a9e6911 100644 --- a/library/Director/Objects/IcingaTimePeriod.php +++ b/library/Director/Objects/IcingaTimePeriod.php @@ -2,9 +2,11 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; +use Icinga\Module\Director\Db; +use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; +use Icinga\Module\Director\Exception\DuplicateKeyException; -class IcingaTimePeriod extends IcingaObject +class IcingaTimePeriod extends IcingaObject implements ExportInterface { protected $table = 'icinga_timeperiod'; @@ -45,6 +47,52 @@ class IcingaTimePeriod extends IcingaObject ], ]; + 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( + 'Time Period "%s" already exists', + $name + ); + } else { + $object = static::create([], $db); + } + $object->setProperties($properties); + + return $object; + } + /** * Render update property *