IcingaTimePeriod: support Basket (ExportInterface)

fixes #1735
This commit is contained in:
Thomas Gelf 2019-02-14 22:42:21 +01:00
parent e961f0eb2a
commit 738e4eeaf1
5 changed files with 61 additions and 4 deletions

View File

@ -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'),

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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
*