[icinga-checkins] icinga.org: icingaweb2-module-director/master: Sync: simplify code, prepare for various fixes

git at icinga.org git at icinga.org
Tue Feb 23 03:15:19 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Feb 19 15:23:44 2016 +0100

Sync: simplify code, prepare for various fixes

---

 library/Director/Import/Sync.php |  190 ++++++++++++++++++--------------------
 1 file changed, 88 insertions(+), 102 deletions(-)

diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php
index 5fd21c0..8796e47 100644
--- a/library/Director/Import/Sync.php
+++ b/library/Director/Import/Sync.php
@@ -285,29 +285,10 @@ class Sync
         }
     }
 
-    /**
-     * Evaluates a SyncRule and returns a list of modified objects
-     *
-     * TODO: This needs to be splitted into smaller methods
-     *
-     * @param  SyncRule $rule The synchronization rule that should be used
-     *
-     * @return array          List of modified IcingaObjects
-     */
-    protected function prepareSyncForRule(SyncRule $rule)
+    protected function prepareNewObjects($rule, & $properties, & $sources, & $imported)
     {
         $db = $rule->getConnection();
-        $properties = $rule->fetchSyncProperties();
-        $sources    = $this->perpareImportSources($properties, $db);
-        $imported   = $this->fetchImportedData($sources, $properties, $rule, $db);
-
-        // TODO: Make object_type (template, object...) and object_name mandatory?
-        $objects = IcingaObject::loadAllByType($rule->object_type, $db);
-
-        if ($rule->object_type === 'datalistEntry') {
-            $this->removeForeignListEntries($objects, $properties);
-        }
-        $objectKey = $rule->object_type === 'datalistEntry' ? 'entry_name' : 'object_name';
+        $newObjects = array();
 
         foreach ($sources as $source) {
             $sourceId = $source->id;
@@ -339,106 +320,111 @@ class Sync
                         }
                     }
                 }
-                if (array_key_exists($key, $objects)) {
-
-                    switch ($rule->update_policy) {
-                        case 'override':
-                            $object = IcingaObject::createByType(
-                                $rule->object_type,
-                                $newProps,
-                                $db
-                            );
-
-                            foreach ($newVars as $prop => $var) {
-                                $object->vars()->$prop = $var;
-                            }
-
-                            if (! empty($imports)) {
-                                $object->imports()->set($imports);
-                            }
-
-                            $objects[$key]->replaceWith($object);
-                            break;
-
-                        case 'merge':
-                            $object = $objects[$key];
-                            foreach ($newProps as $prop => $value) {
-                                // TODO: data type? 
-                                $object->set($prop, $value);
-
-                            }
-
-                            foreach ($newVars as $prop => $var) {
-                                // TODO: property merge policy
-                                $object->vars()->$prop = $var;
-                            }
-
-                            if (! empty($imports)) {
-                                // TODO: merge imports ?!
-                                $objects[$key]->imports()->set($imports);
-                            }
-                            break;
-
-                        default:
-                            // policy 'ignore', no action
-                    }
-                } else {
-                    // New object
-                    if ($rule->object_type !== 'datalistEntry') {
-                        if (! array_key_exists('object_type', $newProps) || $newProps['object_type'] === null) {
-                            $newProps['object_type'] = 'object';
-                        }
-
-                        if (! array_key_exists('object_name', $newProps) || $newProps['object_name'] === null) {
-                            $newProps['object_name'] = $key;
-                        }
-                    }
-
-                    $objects[$key] = IcingaObject::createByType(
+                if (! array_key_exists($key, $newObjects)) {
+                    $newObjects[$key] = IcingaObject::createByType(
                         $rule->object_type,
-                        $newProps,
+                        array(),
                         $db
                     );
+                }
+
+                $object = $newObjects[$key];
 
-                    foreach ($newVars as $prop => $var) {
-                        $objects[$key]->vars()->$prop = $var;
+                // Safe default values for object_type and object_name
+                if ($rule->object_type !== 'datalistEntry') {
+                    if (! array_key_exists('object_type', $newProps)
+                        || $newProps['object_type'] === null
+                    ) {
+                        $newProps['object_type'] = 'object';
                     }
 
-                    if (! empty($imports)) {
-                        $objects[$key]->imports()->set($imports);
+                    if (! array_key_exists('object_name', $newProps)
+                        || $newProps['object_name'] === null
+                    ) {
+                        $newProps['object_name'] = $key;
                     }
                 }
+
+                foreach ($newProps as $prop => $value) {
+                    // TODO: data type? 
+                    $object->set($prop, $value);
+                }
+
+                foreach ($newVars as $prop => $var) {
+                    $object->vars()->$prop = $var;
+                }
+
+                if (! empty($imports)) {
+                    // TODO: merge imports!!!
+                    $object->imports()->set($imports);
+                }
             }
         }
 
-        $ignore = array();
+        return $newObjects;
+    }
 
-        foreach ($objects as $key => $object) {
+    /**
+     * Evaluates a SyncRule and returns a list of modified objects
+     *
+     * TODO: This needs to be splitted into smaller methods
+     *
+     * @param  SyncRule $rule The synchronization rule that should be used
+     *
+     * @return array          List of modified IcingaObjects
+     */
+    protected function prepareSyncForRule(SyncRule $rule)
+    {
+        $db = $rule->getConnection();
+        $properties = $rule->fetchSyncProperties();
+        $sources    = $this->perpareImportSources($properties, $db);
+        $imported   = $this->fetchImportedData($sources, $properties, $rule, $db);
 
-            if ($object->hasBeenLoadedFromDb() && $rule->purge_existing === 'y') {
-                $found = false;
-                foreach ($sources as $source) {
-                    if (array_key_exists($object->$objectKey, $imported[$source->id])) {
-                        $found = true;
+        // TODO: Make object_type (template, object...) and object_name mandatory?
+        $objects = IcingaObject::loadAllByType($rule->object_type, $db);
+
+        // TODO: should be obsoleted by a better "loadFiltered" method
+        if ($rule->object_type === 'datalistEntry') {
+            $this->removeForeignListEntries($objects, $properties);
+        }
+
+        $newObjects = $this->prepareNewObjects(
+            $rule,
+            $properties,
+            $sources,
+            $imported
+        );
+
+        foreach ($newObjects as $key => $object) {
+            if (array_key_exists($key, $objects)) {
+                switch ($rule->update_policy) {
+                    case 'override':
+                        $objects[$key]->replaceWith($object);
                         break;
-                    }
-                }
 
-                if (! $found) {
-                    $object->markForRemoval();
-                    $this->remove[] = $object;
-                }
-            }
+                    case 'merge':
+                        $objects[$key]->merge($object);
+                        break;
 
-            // TODO: This should be noticed or removed:
-            if (! $object->$objectKey) {
-                $this->errors[] = $object;
-                $ignore[] = $key;
+                    default:
+                        // policy 'ignore', no action
+                }
+            } else {
+                $objects[$key] = $object;
             }
         }
 
-        foreach ($ignore as $key) {
-            unset($objects[$key]);
+        $objectKey = $rule->object_type === 'datalistEntry' ? 'entry_name' : 'object_name';
+        foreach ($objects as $key => $object) {
+
+            if ($object->hasBeenLoadedFromDb() && $rule->purge_existing === 'y') {
+                if (! array_key_exists($key, $newObjects)) {
+                    $object->markForRemoval();
+
+                    // TODO: this is for stats, preview, summary:
+                    // $this->remove[] = $object;
+                }
+            }
         }
 
         return $objects;



More information about the icinga-checkins mailing list