Basket: related fixes

This commit is contained in:
Thomas Gelf 2018-11-15 11:14:14 +01:00
parent 4352264b73
commit bb0422c327
5 changed files with 114 additions and 11 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Director\DirectorObject\Automation;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Exception\DuplicateKeyException;
/**
* Class Basket
@ -75,6 +76,34 @@ class Basket extends DbObject implements ExportInterface
return (object) $result;
}
/**
* @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['basket_name'];
if ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::exists($name, $db)) {
throw new DuplicateKeyException(
'Basket "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
return $object;
}
public function supportsCustomSelectionFor($type)
{
if (! array_key_exists($type, $this->chosenObjects)) {
@ -113,7 +142,7 @@ class Basket extends DbObject implements ExportInterface
$objects = true;
} elseif ($objects === null || $objects === 'IGNORE') {
return;
} elseif ($objects === '[]') {
} elseif ($objects === '[]' || is_array($objects)) {
if (isset($this->chosenObjects[$type])) {
if (! is_array($this->chosenObjects[$type])) {
$this->chosenObjects[$type] = [];

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\DirectorObject\Automation;
use Icinga\Module\Director\Db;
interface ExportInterface
{
/**
@ -9,6 +11,8 @@ interface ExportInterface
*/
public function export();
public static function import($plain, Db $db, $replace = false);
// TODO:
// public function getXyzChecksum();
public function getUniqueIdentifier();

View File

@ -2,7 +2,9 @@
namespace Icinga\Module\Director\Objects;
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 RuntimeException;
@ -120,6 +122,10 @@ class IcingaNotification extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @return \stdClass
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
// TODO: ksort in toPlainObject?
@ -130,6 +136,38 @@ class IcingaNotification extends IcingaObject implements ExportInterface
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(
'Notification "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
// $object->newFields = $properties['fields'];
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
protected function loadFieldReferences()
{
$db = $this->getDb();

View File

@ -51,7 +51,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
protected function setKey($key)
{
if (is_int($key)) {
$this->id = $key;
$this->set('id', $key);
} elseif (is_string($key)) {
$keyComponents = preg_split('~!~', $key);
if (count($keyComponents) === 1) {
@ -72,6 +72,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
/**
* @return IcingaService[]
* @throws \Icinga\Exception\NotFoundError
*/
public function getServiceObjects()
{
@ -86,6 +87,11 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
}
}
/**
* @param IcingaServiceSet $set
* @return array
* @throws \Icinga\Exception\NotFoundError
*/
protected function getServiceObjectsForSet(IcingaServiceSet $set)
{
if ($set->get('id') === null) {
@ -226,6 +232,9 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
return $object;
}
/**
* @throws \Icinga\Exception\NotFoundError
*/
public function onDelete()
{
$hostId = $this->get('host_id');
@ -250,6 +259,10 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
parent::onDelete();
}
/**
* @param IcingaConfig $config
* @throws \Icinga\Exception\NotFoundError
*/
public function renderToConfig(IcingaConfig $config)
{
if ($this->get('assign_filter') === null && $this->isTemplate()) {
@ -325,6 +338,10 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
return $this;
}
/**
* @param IcingaConfig $config
* @throws \Icinga\Exception\NotFoundError
*/
public function renderToLegacyConfig(IcingaConfig $config)
{
if ($this->get('assign_filter') === null && $this->isTemplate()) {
@ -343,7 +360,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
$hostnames = HostApplyMatches::forFilter($filter, $conn);
} else {
$hostnames = array($this->getRelated('host')->object_name);
$hostnames = array($this->getRelated('host')->getObjectName());
}
$blacklists = [];
@ -353,7 +370,7 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
$file->addContent($this->getConfigHeaderComment($config));
foreach ($this->getServiceObjects() as $service) {
$object_name = $service->object_name;
$object_name = $service->getObjectName();
if (! array_key_exists($object_name, $blacklists)) {
$blacklists[$object_name] = $service->getBlacklistedHostnames();
@ -422,7 +439,8 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
*/
public function fetchHostSets()
{
if ($this->id === null) {
$id = $this->get('id');
if ($id === null) {
return [];
}
@ -435,12 +453,16 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
[]
)->where(
'ssi.parent_service_set_id = ?',
$this->id
$id
);
return static::loadAll($this->connection, $query);
}
/**
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
protected function beforeStore()
{
parent::beforeStore();

View File

@ -48,10 +48,7 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
{
$properties = (array) $plain;
if (isset($properties['originalId'])) {
$id = $properties['originalId'];
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties['object_name'];
$key = $name;
@ -107,6 +104,13 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
return substr($this->table, 0, -16);
}
/**
* @param QuickForm $form
* @param array $imports
* @param string $namePrefix
* @return \Zend_Form_Element
* @throws \Zend_Form_Exception
*/
public function createFormElement(QuickForm $form, $imports = [], $namePrefix = 'choice')
{
$required = $this->isRequired() && !$this->isTemplate();
@ -141,12 +145,12 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
public function isRequired()
{
return (int) $this->min_required > 0;
return (int) $this->get('min_required') > 0;
}
public function allowsMultipleChoices()
{
return (int) $this->max_allowed > 1;
return (int) $this->get('max_allowed') > 1;
}
public function hasBeenModified()
@ -217,6 +221,9 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
return array_combine($choices, $choices);
}
/**
* @throws \Zend_Db_Adapter_Exception
*/
public function onStore()
{
parent::onStore();
@ -225,6 +232,9 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
}
}
/**
* @throws \Zend_Db_Adapter_Exception
*/
protected function storeChoices()
{
$id = $this->getProperty('id');