From fc7bfe7db9b1c3729c0003e9a3ba96a18c45909f Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 25 Aug 2023 17:15:08 +0200 Subject: [PATCH] Datalist: fix snapshot diff fixes #2791 --- doc/82-Changelog.md | 4 ++ library/Director/Data/Exporter.php | 11 +--- .../DirectorObject/Automation/BasketDiff.php | 1 + library/Director/Objects/DirectorDatalist.php | 53 +++++++++---------- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 44556f993..ddc932b6d 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -34,6 +34,10 @@ This version hasn't been released yet * FIX: synchronizing Service (and -Set) Templates has been fixed (#2745, #2217) * FIX: null properties with Sync policy "ignore" are now being ignored (#2657) +# Configuration Baskets +* FEATURE: it's now possible to upload snapshots for existing baskets (#1952) +* FIX: basket now shows where to expect changes for lists from snapshots (#2791) + ### REST API * FIX: Commands give 304 w/o ghost changes for same properties (#2660) diff --git a/library/Director/Data/Exporter.php b/library/Director/Data/Exporter.php index 1618b8082..06122a3a1 100644 --- a/library/Director/Data/Exporter.php +++ b/library/Director/Data/Exporter.php @@ -304,16 +304,7 @@ protected function exportIcingaObject(IcingaObject $object) protected function exportDatalistEntries(DirectorDatalist $list) { $entries = []; - $id = $list->get('id'); - if ($id === null) { - return $entries; - } - - $dbEntries = DirectorDatalistEntry::loadAllForList($list); - // Hint: they are loaded with entry_name key - ksort($dbEntries); - - foreach ($dbEntries as $entry) { + foreach ($list->getEntries() as $name => $entry) { if ($entry->shouldBeRemoved()) { continue; } diff --git a/library/Director/DirectorObject/Automation/BasketDiff.php b/library/Director/DirectorObject/Automation/BasketDiff.php index 08873781b..8dbb42362 100644 --- a/library/Director/DirectorObject/Automation/BasketDiff.php +++ b/library/Director/DirectorObject/Automation/BasketDiff.php @@ -6,6 +6,7 @@ use Icinga\Module\Director\Data\Exporter; use Icinga\Module\Director\Data\ObjectImporter; use Icinga\Module\Director\Db; +use Icinga\Module\Director\Objects\DirectorDatalist; use Ramsey\Uuid\UuidInterface; use stdClass; diff --git a/library/Director/Objects/DirectorDatalist.php b/library/Director/Objects/DirectorDatalist.php index ba0e8acec..1bb821b41 100644 --- a/library/Director/Objects/DirectorDatalist.php +++ b/library/Director/Objects/DirectorDatalist.php @@ -4,7 +4,7 @@ use Exception; use Icinga\Module\Director\Data\Db\DbObject; -use Icinga\Module\Director\Db; +use Icinga\Module\Director\DataType\DataTypeDatalist; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; use Icinga\Module\Director\Exception\DuplicateKeyException; @@ -23,16 +23,16 @@ class DirectorDatalist extends DbObject implements ExportInterface ]; /** @var DirectorDatalistEntry[] */ - protected $storedEntries; + protected $entries; public function getUniqueIdentifier() { return $this->get('list_name'); } - public function setEntries($entries) + public function setEntries($entries): void { - $existing = $this->getStoredEntries(); + $existing = $this->getEntries(); $new = []; $seen = []; @@ -68,15 +68,13 @@ public function setEntries($entries) $this->hasBeenModified = true; } - $this->storedEntries = $existing; - ksort($this->storedEntries); - - return $this; + $this->entries = $existing; + ksort($this->entries); } protected function beforeDelete() { - if ($this->hasBeenUsed()) { + if ($this->isInUse()) { throw new Exception( sprintf( "Cannot delete '%s', as the datalist '%s' is currently being used.", @@ -87,9 +85,13 @@ protected function beforeDelete() } } - protected function hasBeenUsed() + protected function isInUse(): bool { - $datalistType = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist'; + $id = $this->get('id'); + if ($id === null) { + return false; + } + $db = $this->getDb(); $dataFieldsCheck = $db->select() @@ -104,8 +106,8 @@ protected function hasBeenUsed() 'l.id = dfs.setting_value', [] ) - ->where('datatype = ?', $datalistType) - ->where('setting_value = ?', $this->get('id')); + ->where('datatype = ?', DataTypeDatalist::class) + ->where('setting_value = ?', $id); if ($db->fetchOne($dataFieldsCheck)) { return true; @@ -114,7 +116,7 @@ protected function hasBeenUsed() $syncCheck = $db->select() ->from(['sp' =>'sync_property'], ['source_expression']) ->where('sp.destination_field = ?', 'list_id') - ->where('sp.source_expression = ?', $this->get('id')); + ->where('sp.source_expression = ?', $id); if ($db->fetchOne($syncCheck)) { return true; @@ -128,41 +130,38 @@ protected function hasBeenUsed() */ public function onStore() { - if ($this->storedEntries) { + if ($this->entries) { $db = $this->getConnection(); $removedKeys = []; $myId = $this->get('id'); - foreach ($this->storedEntries as $key => $entry) { + foreach ($this->entries as $key => $entry) { if ($entry->shouldBeRemoved()) { $entry->delete(); $removedKeys[] = $key; } else { - if (! $entry->hasBeenLoadedFromDb()) { - $entry->set('list_id', $myId); - } $entry->set('list_id', $myId); $entry->store($db); } } foreach ($removedKeys as $key) { - unset($this->storedEntries[$key]); + unset($this->entries[$key]); } } } - protected function getStoredEntries() + public function getEntries(): array { - if ($this->storedEntries === null) { - if ($id = $this->get('id')) { - $this->storedEntries = DirectorDatalistEntry::loadAllForList($this); - ksort($this->storedEntries); + if ($this->entries === null) { + if ($this->get('id')) { + $this->entries = DirectorDatalistEntry::loadAllForList($this); + ksort($this->entries); } else { - $this->storedEntries = []; + $this->entries = []; } } - return $this->storedEntries; + return $this->entries; } }