From bec49d1230955219b6db2801bda11999798e2bcf Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 7 Mar 2023 14:29:34 +0100 Subject: [PATCH] library: cleanup, drop import/export methods --- .../DirectorObject/Automation/Basket.php | 38 ------ .../Automation/ExportInterface.php | 10 -- .../Automation/ImportExport.php | 7 +- library/Director/Objects/DirectorDatalist.php | 57 -------- library/Director/Objects/DirectorJob.php | 81 ------------ library/Director/Objects/IcingaCommand.php | 85 ------------ library/Director/Objects/IcingaDependency.php | 45 ------- library/Director/Objects/IcingaHost.php | 85 ------------ .../Director/Objects/IcingaNotification.php | 79 ----------- .../Director/Objects/IcingaObjectGroup.php | 42 ------ library/Director/Objects/IcingaService.php | 89 ------------- library/Director/Objects/IcingaServiceSet.php | 124 ------------------ .../Director/Objects/IcingaTemplateChoice.php | 59 --------- library/Director/Objects/IcingaTimePeriod.php | 44 ------- library/Director/Objects/IcingaUser.php | 37 ------ .../Director/Objects/ImportExportHelper.php | 68 ---------- library/Director/Objects/ImportSource.php | 62 --------- library/Director/Objects/SyncRule.php | 58 -------- .../Web/Form/CloneImportSourceForm.php | 28 ++-- .../Director/Web/Form/CloneSyncRuleForm.php | 24 ++-- 20 files changed, 22 insertions(+), 1100 deletions(-) delete mode 100644 library/Director/Objects/ImportExportHelper.php diff --git a/library/Director/DirectorObject/Automation/Basket.php b/library/Director/DirectorObject/Automation/Basket.php index 75df18ef..d00ac323 100644 --- a/library/Director/DirectorObject/Automation/Basket.php +++ b/library/Director/DirectorObject/Automation/Basket.php @@ -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)) { diff --git a/library/Director/DirectorObject/Automation/ExportInterface.php b/library/Director/DirectorObject/Automation/ExportInterface.php index 275dfed9..271824fb 100644 --- a/library/Director/DirectorObject/Automation/ExportInterface.php +++ b/library/Director/DirectorObject/Automation/ExportInterface.php @@ -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(); diff --git a/library/Director/DirectorObject/Automation/ImportExport.php b/library/Director/DirectorObject/Automation/ImportExport.php index a5e72fae..1664f5d0 100644 --- a/library/Director/DirectorObject/Automation/ImportExport.php +++ b/library/Director/DirectorObject/Automation/ImportExport.php @@ -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++; }); diff --git a/library/Director/Objects/DirectorDatalist.php b/library/Director/Objects/DirectorDatalist.php index ae5c983b..da70ae41 100644 --- a/library/Director/Objects/DirectorDatalist.php +++ b/library/Director/Objects/DirectorDatalist.php @@ -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) { diff --git a/library/Director/Objects/DirectorJob.php b/library/Director/Objects/DirectorJob.php index 82ae3b8a..2a18d523 100644 --- a/library/Director/Objects/DirectorJob.php +++ b/library/Director/Objects/DirectorJob.php @@ -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 diff --git a/library/Director/Objects/IcingaCommand.php b/library/Director/Objects/IcingaCommand.php index 35f38a46..81cac9ed 100644 --- a/library/Director/Objects/IcingaCommand.php +++ b/library/Director/Objects/IcingaCommand.php @@ -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'); diff --git a/library/Director/Objects/IcingaDependency.php b/library/Director/Objects/IcingaDependency.php index 53e1f1b4..a2b767a6 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -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; diff --git a/library/Director/Objects/IcingaHost.php b/library/Director/Objects/IcingaHost.php index 2731f4a1..4ad2334e 100644 --- a/library/Director/Objects/IcingaHost.php +++ b/library/Director/Objects/IcingaHost.php @@ -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) { diff --git a/library/Director/Objects/IcingaNotification.php b/library/Director/Objects/IcingaNotification.php index d530a3f9..47687045 100644 --- a/library/Director/Objects/IcingaNotification.php +++ b/library/Director/Objects/IcingaNotification.php @@ -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 * diff --git a/library/Director/Objects/IcingaObjectGroup.php b/library/Director/Objects/IcingaObjectGroup.php index c0bec545..bade094f 100644 --- a/library/Director/Objects/IcingaObjectGroup.php +++ b/library/Director/Objects/IcingaObjectGroup.php @@ -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; diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php index 9479ef78..ba9a9695 100644 --- a/library/Director/Objects/IcingaService.php +++ b/library/Director/Objects/IcingaService.php @@ -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 diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php index 18e82ec7..97b87404 100644 --- a/library/Director/Objects/IcingaServiceSet.php +++ b/library/Director/Objects/IcingaServiceSet.php @@ -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 diff --git a/library/Director/Objects/IcingaTemplateChoice.php b/library/Director/Objects/IcingaTemplateChoice.php index 56fd456e..572a9959 100644 --- a/library/Director/Objects/IcingaTemplateChoice.php +++ b/library/Director/Objects/IcingaTemplateChoice.php @@ -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() diff --git a/library/Director/Objects/IcingaTimePeriod.php b/library/Director/Objects/IcingaTimePeriod.php index 12323665..081cdd97 100644 --- a/library/Director/Objects/IcingaTimePeriod.php +++ b/library/Director/Objects/IcingaTimePeriod.php @@ -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 * diff --git a/library/Director/Objects/IcingaUser.php b/library/Director/Objects/IcingaUser.php index 394e8498..41002451 100644 --- a/library/Director/Objects/IcingaUser.php +++ b/library/Director/Objects/IcingaUser.php @@ -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(); diff --git a/library/Director/Objects/ImportExportHelper.php b/library/Director/Objects/ImportExportHelper.php deleted file mode 100644 index 98d34c6d..00000000 --- a/library/Director/Objects/ImportExportHelper.php +++ /dev/null @@ -1,68 +0,0 @@ -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; - } -} diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php index fd892ef3..64153521 100644 --- a/library/Director/Objects/ImportSource.php +++ b/library/Director/Objects/ImportSource.php @@ -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()) { diff --git a/library/Director/Objects/SyncRule.php b/library/Director/Objects/SyncRule.php index 8782fa23..270a8826 100644 --- a/library/Director/Objects/SyncRule.php +++ b/library/Director/Objects/SyncRule.php @@ -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 * diff --git a/library/Director/Web/Form/CloneImportSourceForm.php b/library/Director/Web/Form/CloneImportSourceForm.php index 0849dd40..87462cc3 100644 --- a/library/Director/Web/Form/CloneImportSourceForm.php +++ b/library/Director/Web/Form/CloneImportSourceForm.php @@ -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')]); } } diff --git a/library/Director/Web/Form/CloneSyncRuleForm.php b/library/Director/Web/Form/CloneSyncRuleForm.php index f90b593f..4f13deb7 100644 --- a/library/Director/Web/Form/CloneSyncRuleForm.php +++ b/library/Director/Web/Form/CloneSyncRuleForm.php @@ -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')]); } }