[icinga-checkins] icinga.org: icingaweb2-module-director/master: Sync: implement purge with nested keys

git at icinga.org git at icinga.org
Thu Jul 14 12:54:00 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Jul 14 12:11:27 2016 +0200

Sync: implement purge with nested keys

---

 .../PurgeStrategy/ImportRunBasedPurgeStrategy.php    |    4 +++-
 library/Director/Import/Sync.php                     |    4 +++-
 library/Director/Import/SyncUtils.php                |   18 ++++++++++++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
index 4c64a8d..0e46e83 100644
--- a/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
+++ b/library/Director/Import/PurgeStrategy/ImportRunBasedPurgeStrategy.php
@@ -68,7 +68,9 @@ class ImportRunBasedPurgeStrategy extends PurgeStrategy
 
         if ($rule->object_type === 'service') {
             $pattern = $rule->getSourceKeyPattern();
-            $columns = SyncUtils::extractVariableNames($pattern);
+            $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 e8b8c52..b8eaafc 100644
--- a/library/Director/Import/Sync.php
+++ b/library/Director/Import/Sync.php
@@ -246,7 +246,9 @@ class Sync
             $key = $source->key_column;
             $this->sourceColumns[$sourceId][$key] = $key;
             $run = $source->fetchLastRun(true);
-            $rows = $run->fetchRows($this->sourceColumns[$sourceId]);
+            $rows = $run->fetchRows(
+                SyncUtils::getRootVariables($this->sourceColumns[$sourceId])
+            );
 
             $this->imported[$sourceId] = array();
             foreach ($rows as $row) {
diff --git a/library/Director/Import/SyncUtils.php b/library/Director/Import/SyncUtils.php
index 0c89615..924548e 100644
--- a/library/Director/Import/SyncUtils.php
+++ b/library/Director/Import/SyncUtils.php
@@ -75,6 +75,10 @@ class SyncUtils
         } else {
             $parts = explode('.', $var);
             $main = array_shift($parts);
+            if (! property_exists($row, $main)) {
+                return null;
+            }
+
             if (! is_object($row->$main)) {
                 throw new IcingaException('Data is not nested, cannot access %s: %s', $var, var_export($row, 1));
             }
@@ -108,4 +112,18 @@ class SyncUtils
 
         return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
     }
+
+    public static function getRootVariables($vars)
+    {
+        $res = array();
+        foreach ($vars as $p) {
+            if (false === ($pos = strpos($p, '.'))) {
+                $res[] = $p;
+            } else {
+                $res[] = substr($p, 0, $pos);
+            }
+        }
+
+        return array_combine($res, $res);
+    }
 }



More information about the icinga-checkins mailing list