library: cleanup, drop import/export methods

This commit is contained in:
Thomas Gelf 2023-03-07 14:29:34 +01:00
parent c07758b5d9
commit bec49d1230
20 changed files with 22 additions and 1100 deletions

View File

@ -68,44 +68,6 @@ class Basket extends DbObject implements ExportInterface
return $this->get('basket_name');
}
public function export()
{
$result = $this->getProperties();
unset($result['uuid']);
$result['objects'] = Json::decode($result['objects']);
ksort($result);
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)) {

View File

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

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\DirectorObject\Automation;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Data\ObjectImporter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\DirectorDatalist;
@ -125,8 +126,9 @@ class ImportExport
{
$count = 0;
$this->connection->runFailSafeTransaction(function () use ($objects, &$count) {
$importer = new ObjectImporter($this->connection);
foreach ($objects as $object) {
ImportSource::import($object, $this->connection)->store();
$importer->import(ImportSource::class, $object)->store();
$count++;
}
});
@ -138,8 +140,9 @@ class ImportExport
{
$count = 0;
$this->connection->runFailSafeTransaction(function () use ($objects, &$count) {
$importer = new ObjectImporter($this->connection);
foreach ($objects as $object) {
SyncRule::import($object, $this->connection)->store();
$importer->import(SyncRule::class, $object)->store();
}
$count++;
});

View File

@ -30,39 +30,6 @@ class DirectorDatalist extends DbObject implements ExportInterface
return $this->get('list_name');
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return static
* @throws \Icinga\Exception\NotFoundError
* @throws DuplicateKeyException
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
if (isset($properties['originalId'])) {
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties['list_name'];
if ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::exists($name, $db)) {
throw new DuplicateKeyException(
'Data List %s already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
return $object;
}
public function setEntries($entries)
{
$existing = $this->getStoredEntries();
@ -185,30 +152,6 @@ class DirectorDatalist extends DbObject implements ExportInterface
}
}
/**
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @return object
*/
public function export()
{
$plain = (object) $this->getProperties();
$plain->originalId = $plain->id;
unset($plain->id);
$plain->entries = [];
foreach ($this->getStoredEntries() as $key => $entry) {
if ($entry->shouldBeRemoved()) {
continue;
}
$plainEntry = (object) $entry->getProperties();
unset($plainEntry->list_id);
$plain->entries[] = $plainEntry;
}
return $plain;
}
protected function getStoredEntries()
{
if ($this->storedEntries === null) {

View File

@ -197,87 +197,6 @@ class DirectorJob extends DbObjectWithSettings implements ExportInterface, Insta
return $this;
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
$plain = (object) $this->getProperties();
$plain->originalId = $plain->id;
unset($plain->id);
unset($plain->timeperiod_id);
if ($this->hasTimeperiod()) {
$plain->timeperiod = $this->timeperiod()->getObjectName();
}
foreach ($this->stateProperties as $key) {
unset($plain->$key);
}
$plain->settings = $this->getInstance()->exportSettings();
return $plain;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return DirectorJob
* @throws DuplicateKeyException
* @throws NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$dummy = new static;
$idCol = $dummy->autoincKeyName;
$keyCol = $dummy->keyName;
$properties = (array) $plain;
if (isset($properties['originalId'])) {
$id = $properties['originalId'];
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties[$keyCol];
if ($replace && $id && static::existsWithNameAndId($name, $id, $db)) {
$object = static::loadWithAutoIncId($id, $db);
} elseif ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::exists($name, $db)) {
throw new DuplicateKeyException(
'Director Job "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$settings = (array) $properties['settings'];
if (array_key_exists('source', $settings) && ! (array_key_exists('source_id', $settings))) {
$val = ImportSource::load($settings['source'], $db)->get('id');
$settings['source_id'] = $val;
unset($settings['source']);
}
if (array_key_exists('rule', $settings) && ! (array_key_exists('rule_id', $settings))) {
$val = SyncRule::load($settings['rule'], $db)->get('id');
$settings['rule_id'] = $val;
unset($settings['rule']);
}
$properties['settings'] = (object) $settings;
$object->setProperties($properties);
if ($id !== null) {
$object->reallySet($idCol, $id);
}
return $object;
}
/**
* @param string $name
* @param int $id

View File

@ -2,9 +2,7 @@
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 Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\Objects\Extension\Arguments;
@ -212,89 +210,6 @@ class IcingaCommand extends IcingaObject implements ObjectWithArguments, ExportI
return $this->getObjectName();
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
$props = (array) $this->toPlainObject();
if (isset($props['arguments'])) {
foreach ($props['arguments'] as $key => $argument) {
if (property_exists($argument, 'command_id')) {
unset($props['arguments'][$key]->command_id);
}
}
}
$props['fields'] = $this->loadFieldReferences();
ksort($props);
return (object) $props;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaCommand
* @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(
'Command "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return array
*/
protected function loadFieldReferences()
{
$db = $this->getDb();
$res = $db->fetchAll(
$db->select()->from([
'cf' => 'icinga_command_field'
], [
'cf.datafield_id',
'cf.is_required',
'cf.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = cf.datafield_id', [])
->where('command_id = ?', $this->get('id'))
->order('varname ASC')
);
if (empty($res)) {
return [];
} else {
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}
protected function renderCommand()
{
$command = $this->get('command');

View File

@ -3,9 +3,7 @@
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;
@ -80,49 +78,6 @@ class IcingaDependency extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @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(
'Dependency "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
return $object;
}
public function parentHostIsVar()
{
return $this->get('parent_host_var') !== null;

View File

@ -7,7 +7,6 @@ use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Data\PropertiesFilter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
@ -310,90 +309,6 @@ class IcingaHost extends IcingaObject implements ExportInterface
}
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
// TODO: ksort in toPlainObject?
$props = (array) $this->toPlainObject();
$props['fields'] = $this->loadFieldReferences();
ksort($props);
return (object) $props;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaHost
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
$name = $properties['object_name'];
if ($properties['object_type'] !== 'template') {
throw new InvalidArgumentException(sprintf(
'Can import only Templates, got "%s" for "%s"',
$properties['object_type'],
$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->newFields = $properties['fields'];
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return array
*/
protected function loadFieldReferences()
{
$db = $this->getDb();
$res = $db->fetchAll(
$db->select()->from([
'hf' => 'icinga_host_field'
], [
'hf.datafield_id',
'hf.is_required',
'hf.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = hf.datafield_id', [])
->where('host_id = ?', $this->get('id'))
->order('varname ASC')
);
if (empty($res)) {
return [];
} else {
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}
public function beforeDelete()
{
foreach ($this->fetchServices() as $service) {

View File

@ -3,9 +3,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\CustomVariable\CustomVariables;
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;
@ -160,83 +158,6 @@ class IcingaNotification extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @return \stdClass
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
// TODO: ksort in toPlainObject?
$props = (array) $this->toPlainObject();
$props['fields'] = $this->loadFieldReferences();
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(
'Notification "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
// $object->newFields = $properties['fields'];
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return array
*/
protected function loadFieldReferences()
{
$db = $this->getDb();
$res = $db->fetchAll(
$db->select()->from([
'nf' => 'icinga_notification_field'
], [
'nf.datafield_id',
'nf.is_required',
'nf.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = nf.datafield_id', [])
->where('notification_id = ?', $this->get('id'))
->order('varname ASC')
);
if (empty($res)) {
return [];
} else {
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}
/**
* Do not render internal property apply_to
*

View File

@ -2,9 +2,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
abstract class IcingaObjectGroup extends IcingaObject implements ExportInterface
{
@ -29,46 +27,6 @@ abstract class IcingaObjectGroup extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
return $this->toPlainObject();
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaObjectGroup
* @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(
'Group "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
return $object;
}
protected function prefersGlobalZone()
{
return true;

View File

@ -8,7 +8,6 @@ use Icinga\Module\Director\Data\PropertiesFilter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
@ -169,94 +168,6 @@ class IcingaService extends IcingaObject implements ExportInterface
}
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
// TODO: ksort in toPlainObject?
$props = (array) $this->toPlainObject();
$props['fields'] = $this->loadFieldReferences();
ksort($props);
return (object) $props;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaService
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
$name = $properties['object_name'];
if ($properties['object_type'] !== 'template') {
throw new InvalidArgumentException(sprintf(
'Can import only Templates, got "%s" for "%s"',
$properties['object_type'],
$name
));
}
$key = [
'object_type' => 'template',
'object_name' => $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->newFields = $properties['fields'];
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return array
*/
protected function loadFieldReferences()
{
$db = $this->getDb();
$res = $db->fetchAll(
$db->select()->from([
'sf' => 'icinga_service_field'
], [
'sf.datafield_id',
'sf.is_required',
'sf.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = sf.datafield_id', [])
->where('service_id = ?', $this->get('id'))
->order('varname ASC')
);
if (empty($res)) {
return [];
} else {
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}
/**
* @param string $key
* @return $this

View File

@ -5,16 +5,13 @@ namespace Icinga\Module\Director\Objects;
use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Data\Db\ServiceSetQueryBuilder;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Resolver\HostServiceBlacklist;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use RuntimeException;
use stdClass;
class IcingaServiceSet extends IcingaObject implements ExportInterface
@ -180,127 +177,6 @@ class IcingaServiceSet extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @return object
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @throws \Icinga\Exception\NotFoundError
*/
public function export()
{
if ($this->get('host_id')) {
$result = $this->exportSetOnHost();
} else {
$result = $this->exportTemplate();
}
unset($result->uuid);
return $result;
}
protected function exportSetOnHost()
{
// TODO.
throw new RuntimeException('Not yet');
}
/**
* @return object
* @deprecated
* @throws \Icinga\Exception\NotFoundError
*/
protected function exportTemplate()
{
$props = $this->getProperties();
unset($props['id'], $props['host_id']);
$props['services'] = [];
foreach ($this->getServiceObjects() as $serviceObject) {
$props['services'][$serviceObject->getObjectName()] = $serviceObject->export();
}
ksort($props);
return (object) $props;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaServiceSet
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
$name = $properties['object_name'];
if (isset($properties['services'])) {
$services = $properties['services'];
unset($properties['services']);
} else {
$services = [];
}
if ($properties['object_type'] !== 'template') {
throw new InvalidArgumentException(sprintf(
'Can import only Templates, got "%s" for "%s"',
$properties['object_type'],
$name
));
}
if ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::exists($name, $db)) {
throw new DuplicateKeyException(
'Service Set "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
// This is not how other imports work, but here we need an ID
if (! $object->hasBeenLoadedFromDb()) {
$object->store();
}
$setId = $object->get('id');
$sQuery = $db->getDbAdapter()->select()->from(
['s' => 'icinga_service'],
's.*'
)->where('service_set_id = ?', $setId);
$existingServices = IcingaService::loadAll($db, $sQuery, 'object_name');
$serviceNames = [];
foreach ($services as $service) {
if (isset($service->fields)) {
unset($service->fields);
}
$name = $service->object_name;
$serviceNames[] = $name;
if (isset($existingServices[$name])) {
$existing = $existingServices[$name];
$existing->setProperties((array) $service);
$existing->set('service_set_id', $setId);
if ($existing->hasBeenModified()) {
$existing->store();
}
} else {
$new = IcingaService::create((array) $service, $db);
$new->set('service_set_id', $setId);
$new->store();
}
}
foreach ($existingServices as $existing) {
if (!in_array($existing->getObjectName(), $serviceNames)) {
$existing->delete();
}
}
return $object;
}
public function beforeDelete()
{
// check if this is a template, or directly assigned to a host

View File

@ -3,9 +3,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\Web\Form\QuickForm;
class IcingaTemplateChoice extends IcingaObject implements ExportInterface
@ -36,63 +34,6 @@ class IcingaTemplateChoice extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaTemplateChoice
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
if (isset($properties['originalId'])) {
unset($properties['originalId']);
}
$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(
'Template Choice "%s" already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
return $object;
}
/**
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @return array|object|\stdClass
*/
public function export()
{
$plain = (object) $this->getProperties();
$plain->originalId = $plain->id;
unset($plain->id);
$requiredId = $plain->required_template_id;
unset($plain->required_template_id);
if ($requiredId) {
$db = $this->getDb();
$query = $db->select()
->from(['o' => $this->getObjectTableName()], 'o.object_name')->where("o.object_type = 'template'")
->where('o.id = ?', $this->get('id'));
$plain->required_template = $db->fetchOne($query);
}
$plain->members = array_values($this->getMembers());
return $plain;
}
public function isMainChoice()
{
return $this->hasBeenLoadedFromDb()

View File

@ -2,9 +2,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
class IcingaTimePeriod extends IcingaObject implements ExportInterface
{
@ -55,48 +53,6 @@ class IcingaTimePeriod extends IcingaObject implements ExportInterface
return $this->getObjectName();
}
/**
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @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
*

View File

@ -48,43 +48,6 @@ class IcingaUser extends IcingaObject implements ExportInterface
'zone' => 'IcingaZone',
);
public function export()
{
return ImportExportHelper::simpleExport($this);
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return IcingaUser
* @throws DuplicateKeyException
* @throws \Icinga\Exception\NotFoundError
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
$key = $properties['object_name'];
if ($replace && static::exists($key, $db)) {
$object = static::load($key, $db);
} elseif (static::exists($key, $db)) {
throw new DuplicateKeyException(
'Cannot import, %s "%s" already exists',
static::create([])->getShortTableName(),
$key
);
} else {
$object = static::create([], $db);
}
// $object->newFields = $properties['fields'];
unset($properties['fields']);
$object->setProperties($properties);
return $object;
}
public function getUniqueIdentifier()
{
return $this->getObjectName();

View File

@ -1,68 +0,0 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Db;
/**
* Helper class, allows to reduce duplicate code. Might be moved elsewhere
* afterwards
*/
class ImportExportHelper
{
/**
* Does not support every type out of the box
*
* @param IcingaObject $object
* @return object
* @throws \Icinga\Exception\NotFoundError
*/
public static function simpleExport(IcingaObject $object)
{
$props = (array) $object->toPlainObject();
$props['fields'] = static::fetchFields($object);
ksort($props); // TODO: ksort in toPlainObject?
return (object) $props;
}
public static function fetchFields(IcingaObject $object)
{
return static::loadFieldReferences(
$object->getConnection(),
$object->getShortTableName(),
$object->get('id')
);
}
/**
* @param Db $connection
* @param string $type Warning: this will not be validated.
* @param int $id
* @return array
*/
public static function loadFieldReferences(Db $connection, $type, $id)
{
$db = $connection->getDbAdapter();
$res = $db->fetchAll(
$db->select()->from([
'f' => "icinga_${type}_field"
], [
'f.datafield_id',
'f.is_required',
'f.var_filter',
])->join(['df' => 'director_datafield'], 'df.id = f.datafield_id', [])
->where("${type}_id = ?", $id)
->order('varname ASC')
);
if (empty($res)) {
return [];
}
foreach ($res as $field) {
$field->datafield_id = (int) $field->datafield_id;
}
return $res;
}
}

View File

@ -52,68 +52,6 @@ class ImportSource extends DbObjectWithSettings implements ExportInterface
private $newRowModifiers;
/**
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
* @return \stdClass
*/
public function export()
{
$plain = $this->getProperties();
$plain['originalId'] = $plain['id'];
unset($plain['id']);
foreach ($this->stateProperties as $key) {
unset($plain[$key]);
}
$plain['settings'] = (object) $this->getSettings();
$plain['modifiers'] = $this->exportRowModifiers();
ksort($plain);
return (object) $plain;
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return ImportSource
* @throws DuplicateKeyException
* @throws 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;
}
$name = $properties['source_name'];
if ($replace && $id && static::existsWithNameAndId($name, $id, $db)) {
$object = static::loadWithAutoIncId($id, $db);
} elseif ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::existsWithName($name, $db)) {
throw new DuplicateKeyException(
'Import Source %s already exists',
$name
);
} else {
$object = static::create([], $db);
}
if (! isset($properties['modifiers'])) {
$properties['modifiers'] = [];
}
$object->setProperties($properties);
return $object;
}
public function setModifiers(array $modifiers)
{
if ($this->loadedRowModifiers === null && $this->hasBeenLoadedFromDb()) {

View File

@ -258,64 +258,6 @@ class SyncRule extends DbObject implements ExportInterface
}
}
/**
* @deprecated please use \Icinga\Module\Director\Data\Exporter
* @return object
*/
public function export()
{
$plain = $this->getProperties();
$plain['originalId'] = $plain['id'];
unset($plain['id']);
foreach ($this->stateProperties as $key) {
unset($plain[$key]);
}
$plain['properties'] = $this->exportSyncProperties();
ksort($plain);
return (object) $plain;
}
/**
* @param object $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;
if (isset($properties['originalId'])) {
$id = $properties['originalId'];
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties['rule_name'];
if ($replace && $id && static::existsWithNameAndId($name, $id, $db)) {
$object = static::loadWithAutoIncId($id, $db);
} elseif ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::existsWithName($name, $db)) {
throw new DuplicateKeyException(
'Sync Rule %s already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->newSyncProperties = $properties['properties'];
unset($properties['properties']);
$object->setProperties($properties);
return $object;
}
/**
* Flat object has 'properties', but setProperties() is not available in DbObject
*

View File

@ -4,6 +4,8 @@ namespace Icinga\Module\Director\Web\Form;
use Icinga\Module\Director\Data\Exporter;
use ipl\Html\Form;
use Icinga\Module\Director\Data\ObjectImporter;
use Icinga\Module\Director\Db;
use ipl\Html\FormDecorator\DdDtDecorator;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Url;
@ -36,37 +38,25 @@ class CloneImportSourceForm extends Form
]);
}
/**
* @return \Icinga\Module\Director\Db
*/
protected function getTargetDb()
{
return $this->source->getConnection();
}
/**
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
public function onSuccess()
{
$db = $this->getTargetDb();
$db = $this->source->getConnection();
assert($db instanceof Db);
$export = (new Exporter($db))->export($this->source);
$newName = $this->getElement('source_name')->getValue();
$export->source_name = $newName;
unset($export->originalId);
unset($export->uuid);
if (ImportSource::existsWithName($newName, $db)) {
$this->getElement('source_name')->addMessage('Name already exists');
}
$this->newSource = ImportSource::import($export, $db);
$importer = new ObjectImporter($db);
$this->newSource = $importer->import(ImportSource::class, $export);
$this->newSource->store();
}
public function getSuccessUrl()
{
if ($this->newSource === null) {
return parent::getSuccessUrl();
} else {
return Url::fromPath('director/importsource', ['id' => $this->newSource->get('id')]);
}
return Url::fromPath('director/importsource', ['id' => $this->newSource->get('id')]);
}
}

View File

@ -4,6 +4,8 @@ namespace Icinga\Module\Director\Web\Form;
use Icinga\Module\Director\Data\Exporter;
use ipl\Html\Form;
use Icinga\Module\Director\Data\ObjectImporter;
use Icinga\Module\Director\Db;
use ipl\Html\FormDecorator\DdDtDecorator;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Url;
@ -36,41 +38,31 @@ class CloneSyncRuleForm extends Form
]);
}
/**
* @return \Icinga\Module\Director\Db
*/
protected function getTargetDb()
{
return $this->rule->getConnection();
}
/**
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
public function onSuccess()
{
$db = $this->getTargetDb();
$db = $this->rule->getConnection();
assert($db instanceof Db);
$exporter = new Exporter($db);
$export = $exporter->export($this->rule);
$newName = $this->getValue('rule_name');
$export->rule_name = $newName;
unset($export->originalId);
unset($export->uuid);
if (SyncRule::existsWithName($newName, $db)) {
$this->getElement('rule_name')->addMessage('Name already exists');
}
$this->newRule = SyncRule::import($export, $db);
$importer = new ObjectImporter($db);
$this->newRule = $importer->import(SyncRule::class, $export);
$this->newRule->store();
}
public function getSuccessUrl()
{
if ($this->newRule === null) {
return parent::getSuccessUrl();
} else {
return Url::fromPath('director/syncrule', ['id' => $this->newRule->get('id')]);
}
return Url::fromPath('director/syncrule', ['id' => $this->newRule->get('id')]);
}
}