[icinga-checkins] icinga.org: icingaweb2-module-director/master: Sync: fix sync & purge for datalistEntry objects

git at icinga.org git at icinga.org
Wed Oct 5 22:13:57 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: e9a570e96d77f7f083ba5f366b94ceb09582975f
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=e9a570e96d77f7f083ba5f366b94ceb09582975f

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Oct  5 17:45:25 2016 +0000

Sync: fix sync & purge for datalistEntry objects

---

 .../PurgeStrategy/ImportRunBasedPurgeStrategy.php  |    3 ++-
 library/Director/Import/Sync.php                   |    8 +++----
 library/Director/Import/SyncUtils.php              |    4 ++--
 library/Director/Objects/DirectorDatalistEntry.php |   14 +++++++++++
 library/Director/Objects/SyncRule.php              |   25 ++++++++++++++++++++
 5 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
index 4e37b8b..c48aced 100644
--- a/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
+++ b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
@@ -70,11 +70,12 @@ class ImportRunBasedPurgeStrategy extends PurgeStrategy
             return array();
         }
 
-        if ($rule->object_type === 'service') {
+        if ($rule->hasCombinedKey()) {
             $pattern = $rule->getSourceKeyPattern();
             $columns = SyncUtils::getRootVariables(
                 SyncUtils::extractVariableNames($pattern)
             );
+
             $rows = $runA->fetchRows($columns, null, $result);
             $result = array();
             foreach ($rows as $row) {
diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php
index b8eaafc..28fc482 100644
--- a/library/Director/Import/Sync.php
+++ b/library/Director/Import/Sync.php
@@ -101,7 +101,7 @@ class Sync
         foreach ($objects as $object) {
             if ($object->hasBeenModified()) {
                 $modified[] = $object;
-            } elseif ($object instanceof IcingaObject && $object->shouldBeRemoved()) {
+            } elseif ($object->shouldBeRemoved()) {
                 $modified[] = $object;
             }
         }
@@ -313,7 +313,7 @@ class Sync
 
         $no = array();
         foreach ($this->objects as $k => $o) {
-            if ($o->list_id !== $listId) {
+            if ((int) $o->list_id !== (int) $listId) {
                 $no[] = $k;
             }
         }
@@ -557,13 +557,13 @@ class Sync
                     if ($object->hasBeenModified()) {
                         throw new IcingaException(
                             'Sync is not allowed to modify template "%s"',
-                            $object->$objectKey
+                            $object->object_name
                         );
                     }
                     continue;
                 }
 
-                if ($object instanceof IcingaObject && $object->shouldBeRemoved()) {
+                if ($object->shouldBeRemoved()) {
                     $object->delete($db);
                     $deleted++;
                     continue;
diff --git a/library/Director/Import/SyncUtils.php b/library/Director/Import/SyncUtils.php
index 386f8d9..6a48ef7 100644
--- a/library/Director/Import/SyncUtils.php
+++ b/library/Director/Import/SyncUtils.php
@@ -3,7 +3,7 @@
 namespace Icinga\Module\Director\Import;
 
 use Icinga\Exception\IcingaException;
-use Icinga\Module\Director\Objects\IcingaObject;
+use Icinga\Module\Director\Data\Db\DbObject;
 
 class SyncUtils
 {
@@ -65,7 +65,7 @@ class SyncUtils
     public static function getSpecificValue($row, $var)
     {
         if (strpos($var, '.') === false) {
-            if ($row instanceof IcingaObject) {
+            if ($row instanceof DbObject) {
                 return $row->$var;
             }
             if (! property_exists($row, $var)) {
diff --git a/library/Director/Objects/DirectorDatalistEntry.php b/library/Director/Objects/DirectorDatalistEntry.php
index 339fb37..de07b06 100644
--- a/library/Director/Objects/DirectorDatalistEntry.php
+++ b/library/Director/Objects/DirectorDatalistEntry.php
@@ -19,6 +19,20 @@ class DirectorDatalistEntry extends DbObject
         'format'        => null,
     );
 
+    public function replaceWith(DirectorDatalistEntry $object)
+    {
+        $this->entry_value = $object->entry_value;
+        if ($object->format) {
+            $this->format = $object->format;
+        }
+
+        return $this;
+    }
+
+    public function merge(DirectorDatalistEntry $object)
+    {
+        return $this->replaceWith($object);
+    }
 
     public function markForRemoval($remove = true)
     {
diff --git a/library/Director/Objects/SyncRule.php b/library/Director/Objects/SyncRule.php
index 7193d80..210edac 100644
--- a/library/Director/Objects/SyncRule.php
+++ b/library/Director/Objects/SyncRule.php
@@ -253,6 +253,7 @@ class SyncRule extends DbObject
 
             $this->hasCombinedKey = false;
 
+            // TODO: Move to Objects
             if ($this->object_type === 'service') {
                 $hasHost = false;
                 $hasObjectName = false;
@@ -276,6 +277,30 @@ class SyncRule extends DbObject
 
                     $this->destinationKeyPattern = '${host}!${object_name}';
                 }
+            } elseif ($this->object_type === 'datalistEntry') {
+                $hasList = false;
+                $hasName = false;
+
+                foreach ($this->getSyncProperties() as $key => $property) {
+                    if ($property->destination_field === 'list_id') {
+                        $hasList = $property->source_expression;
+                    }
+                    if ($property->destination_field === 'entry_name') {
+                        $hasName = $property->source_expression;
+                    }
+                }
+
+                if ($hasList !== false && $hasName !== false) {
+                    $this->hasCombinedKey = true;
+                    $this->sourceKeyPattern = sprintf(
+                        '%s!%s',
+                        $hasList,
+                        $hasName
+                    );
+
+                    $this->destinationKeyPattern = '${list_id}!${entry_name}';
+                }
+
             }
         }
 



More information about the icinga-checkins mailing list