Skip to content

Commit

Permalink
Datalist: fix snapshot diff
Browse files Browse the repository at this point in the history
fixes #2791
  • Loading branch information
Thomas-Gelf committed Aug 25, 2023
1 parent 5298243 commit fc7bfe7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
4 changes: 4 additions & 0 deletions doc/82-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 1 addition & 10 deletions library/Director/Data/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions library/Director/DirectorObject/Automation/BasketDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
53 changes: 26 additions & 27 deletions library/Director/Objects/DirectorDatalist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = [];
Expand Down Expand Up @@ -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.",
Expand All @@ -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()
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}

0 comments on commit fc7bfe7

Please sign in to comment.