[icinga-checkins] icinga.org: icingaweb2-module-director/master: Sync: start to modularize combined key handling

git at icinga.org git at icinga.org
Tue Feb 23 11:22:45 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Feb 23 11:10:37 2016 +0100

Sync: start to modularize combined key handling

---

 library/Director/Import/Sync.php |   66 +++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 23 deletions(-)

diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php
index 6c5d090..6e35a06 100644
--- a/library/Director/Import/Sync.php
+++ b/library/Director/Import/Sync.php
@@ -9,6 +9,8 @@ use Icinga\Exception\IcingaException;
 
 class Sync
 {
+    protected $rule;
+
     protected $modify = array();
 
     protected $remove = array();
@@ -17,12 +19,19 @@ class Sync
 
     protected $errors = array();
 
+    protected $hasCombinedKey;
+
+    protected $sourceKeyPattern;
+
+    protected $destinationKeyPattern;
+
     /**
      * Constructor. No direct initialization allowed right now. Please use one
      * of the available static factory methods
      */
-    protected function __construct()
+    protected function __construct(SyncRule $rule)
     {
+        $this->rule = $rule;
     }
 
     /**
@@ -30,7 +39,7 @@ class Sync
      */
     public static function run(SyncRule $rule)
     {
-        $sync = new static;
+        $sync = new static($rule);
 
         // Raise limits. TODO: do this in a failsafe way, and only if necessary
         ini_set('memory_limit', '768M');
@@ -61,7 +70,7 @@ class Sync
     public static function getExpectedModifications(SyncRule $rule)
     {
         $modified = array();
-        $sync = new static;
+        $sync = new static($rule);
         $objects = $sync->prepareSyncForRule($rule);
         foreach ($objects as $object) {
             if ($object->hasBeenModified()) {
@@ -225,30 +234,41 @@ class Sync
         return $columns;
     }
 
-    protected function fetchImportedData($sources, $properties, SyncRule $rule, $db)
+    protected function hasCombinedKey()
     {
-        $imported = array();
+        if ($this->hasCombinedKey === null) {
 
-        $sourceColumns = $this->prepareSourceColumns($properties);
+            $this->hasCombinedKey = false;
 
-        $keyPattern = null;
-        if ($rule->object_type === 'service') {
-            $hasHost = false;
-            $hasObjectName = false;
-            foreach ($properties as $key => $property) {
-                if ($property->destination_field === 'host') {
-                    $hasHost = $property->source_expression;
-                }
-                if ($property->destination_field === 'object_name') {
-                    $hasObjectName = $property->source_expression;
+            if ($this->rule->object_type === 'service') {
+                $hasHost = false;
+                $hasObjectName = false;
+                foreach ($properties as $key => $property) {
+                    if ($property->destination_field === 'host') {
+                        $hasHost = $property->source_expression;
+                    }
+                    if ($property->destination_field === 'object_name') {
+                        $hasObjectName = $property->source_expression;
+                    }
                 }
-            }
 
-            if ($hasHost !== false && $hasObjectName !== false) {
-                $keyPattern = sprintf('%s!%s', $hasHost, $hasObjectName);
+                if ($hasHost !== false && $hasObjectName !== false) {
+                    $this->hasCombinedKey = true;
+                    $this->sourceKeyPattern = sprintf('%s!%s', $hasHost, $hasObjectName);
+                    $this->destinationKeyPattern = 'host!object_name';
+                }
             }
         }
 
+        return $this->hasCombinedKey;
+    }
+
+    protected function fetchImportedData($sources, $properties, SyncRule $rule, $db)
+    {
+        $imported = array();
+
+        $sourceColumns = $this->prepareSourceColumns($properties);
+
         foreach ($sources as $source) {
             $sourceId = $source->id;
             $key = $source->key_column;
@@ -257,13 +277,13 @@ class Sync
 
             $imported[$sourceId] = array();
             foreach ($rows as $row) {
-                if ($keyPattern) {
-                    $key = $this->fillVariables($keyPattern, $row);
+                if ($this->hasCombinedKey()) {
+                    $key = $this->fillVariables($this->sourceKeyPattern, $row);
                     if (array_key_exists($key, $imported[$sourceId])) {
                         throw new IcingaException(
                             'Trying to import row "%s" (%s) twice: %s VS %s',
                             $key,
-                            $keyPattern,
+                            $this->sourceKeyPattern,
                             json_encode($imported[$sourceId][$key]),
                             json_encode($row)
                         );
@@ -286,7 +306,7 @@ class Sync
                     continue;
                 }
 
-                if ($keyPattern) {
+                if ($this->hasCombinedKey()) {
                     $imported[$sourceId][$key] = $row;
                 } else {
                     $imported[$sourceId][$row->$key] = $row;



More information about the icinga-checkins mailing list