BasketSnapshot: add Datafields to the mix
This commit is contained in:
parent
a58efd2d25
commit
2e37758336
|
@ -5,6 +5,7 @@ namespace Icinga\Module\Director\DirectorObject\Automation;
|
||||||
use Icinga\Module\Director\Core\Json;
|
use Icinga\Module\Director\Core\Json;
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
|
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||||
use Icinga\Module\Director\Objects\IcingaCommand;
|
use Icinga\Module\Director\Objects\IcingaCommand;
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
@ -23,6 +24,7 @@ class BasketSnapshot extends DbObject
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $restoreOrder = [
|
protected $restoreOrder = [
|
||||||
|
'Datafield',
|
||||||
'Command',
|
'Command',
|
||||||
'HostGroup',
|
'HostGroup',
|
||||||
'IcingaTemplateChoiceHost',
|
'IcingaTemplateChoiceHost',
|
||||||
|
@ -48,6 +50,7 @@ class BasketSnapshot extends DbObject
|
||||||
public static function getClassForType($type)
|
public static function getClassForType($type)
|
||||||
{
|
{
|
||||||
$types = [
|
$types = [
|
||||||
|
'Datafield' => '\\Icinga\\Module\\Director\\Objects\\DirectorDatafield',
|
||||||
'Command' => '\\Icinga\\Module\\Director\\Objects\\IcingaCommand',
|
'Command' => '\\Icinga\\Module\\Director\\Objects\\IcingaCommand',
|
||||||
'HostGroup' => '\\Icinga\\Module\\Director\\Objects\\IcingaHostGroup',
|
'HostGroup' => '\\Icinga\\Module\\Director\\Objects\\IcingaHostGroup',
|
||||||
'IcingaTemplateChoiceHost' => '\\Icinga\\Module\\Director\\Objects\\IcingaTemplateChoiceHost',
|
'IcingaTemplateChoiceHost' => '\\Icinga\\Module\\Director\\Objects\\IcingaTemplateChoiceHost',
|
||||||
|
@ -73,10 +76,39 @@ class BasketSnapshot extends DbObject
|
||||||
'basket_uuid' => $basket->get('uuid')
|
'basket_uuid' => $basket->get('uuid')
|
||||||
], $db);
|
], $db);
|
||||||
$snapshot->addObjectsChosenByBasket($basket);
|
$snapshot->addObjectsChosenByBasket($basket);
|
||||||
|
$snapshot->resolveRequiredFields();
|
||||||
|
|
||||||
return $snapshot;
|
return $snapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Icinga\Exception\NotFoundError
|
||||||
|
*/
|
||||||
|
protected function resolveRequiredFields()
|
||||||
|
{
|
||||||
|
$requiredIds = [];
|
||||||
|
foreach ($this->objects as $typeName => $objects) {
|
||||||
|
foreach ($objects as $key => $object) {
|
||||||
|
if (isset($object->fields)) {
|
||||||
|
foreach ($object->fields as $field) {
|
||||||
|
$requiredIds[$field->datafield_id] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
if (! isset($this->objects['Datafield'])) {
|
||||||
|
$this->objects['Datafield'] = [];
|
||||||
|
}
|
||||||
|
$fields = & $this->objects['Datafield'];
|
||||||
|
foreach ($requiredIds as $id) {
|
||||||
|
if (! isset($fields[$id])) {
|
||||||
|
$fields[$id] = DirectorDatafield::loadWithAutoIncId((int) $id, $connection)->export();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function addObjectsChosenByBasket(Basket $basket)
|
protected function addObjectsChosenByBasket(Basket $basket)
|
||||||
{
|
{
|
||||||
foreach ($basket->getChosenObjects() as $typeName => $selection) {
|
foreach ($basket->getChosenObjects() as $typeName => $selection) {
|
||||||
|
|
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Director\Objects;
|
namespace Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\Core\Json;
|
||||||
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
||||||
use Icinga\Module\Director\Hook\DataTypeHook;
|
use Icinga\Module\Director\Hook\DataTypeHook;
|
||||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||||
|
use InvalidArgumentException;
|
||||||
use Zend_Form_Element as ZfElement;
|
use Zend_Form_Element as ZfElement;
|
||||||
|
|
||||||
class DirectorDatafield extends DbObjectWithSettings
|
class DirectorDatafield extends DbObjectWithSettings
|
||||||
|
@ -50,6 +53,10 @@ class DirectorDatafield extends DbObjectWithSettings
|
||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return object
|
||||||
|
* @throws \Icinga\Exception\NotFoundError
|
||||||
|
*/
|
||||||
public function export()
|
public function export()
|
||||||
{
|
{
|
||||||
$plain = (object) $this->getProperties();
|
$plain = (object) $this->getProperties();
|
||||||
|
@ -68,6 +75,38 @@ class DirectorDatafield extends DbObjectWithSettings
|
||||||
return $plain;
|
return $plain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $plain
|
||||||
|
* @param Db $db
|
||||||
|
* @param bool $replace
|
||||||
|
* @return DirectorDatafield
|
||||||
|
* @throws DuplicateKeyException
|
||||||
|
* @throws \Icinga\Exception\NotFoundError
|
||||||
|
*/
|
||||||
|
public static function import($plain, Db $db, $replace = false)
|
||||||
|
{
|
||||||
|
$properties = (array) $plain;
|
||||||
|
if (isset($properties['originalId'])) {
|
||||||
|
$id = $properties['originalId'];
|
||||||
|
unset($properties['originalId']);
|
||||||
|
} else {
|
||||||
|
$id = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($id) {
|
||||||
|
if (static::exists($id, $db)) {
|
||||||
|
$existing = static::loadWithAutoIncId($id, $db);
|
||||||
|
$existingProperties = (array) $existing->export();
|
||||||
|
unset($existingProperties['originalId']);
|
||||||
|
if (Json::encode($properties) === Json::encode($existingProperties)) {
|
||||||
|
return $existing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::create($properties, $db);
|
||||||
|
}
|
||||||
|
|
||||||
protected function setObject(IcingaObject $object)
|
protected function setObject(IcingaObject $object)
|
||||||
{
|
{
|
||||||
$this->object = $object;
|
$this->object = $object;
|
||||||
|
|
Loading…
Reference in New Issue