[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaObjectGroups: provide autocreate and ignore...

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


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

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

IcingaObjectGroups: provide autocreate and ignore...

...failure options for objects assigned to missing groups by name

---

 library/Director/Objects/IcingaObjectGroups.php |   38 ++++++++++++++++++-----
 1 file changed, 30 insertions(+), 8 deletions(-)

diff --git a/library/Director/Objects/IcingaObjectGroups.php b/library/Director/Objects/IcingaObjectGroups.php
index 6a6b4cf..7d08e6f 100644
--- a/library/Director/Objects/IcingaObjectGroups.php
+++ b/library/Director/Objects/IcingaObjectGroups.php
@@ -124,12 +124,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
         $this->idx = array_keys($this->groups);
     }
 
-    public function add($group)
+    public function add($group, $onError = 'fail')
     {
         // TODO: only one query when adding array
         if (is_array($group)) {
             foreach ($group as $g) {
-                $this->add($g);
+                $this->add($g, $onError);
             }
             return $this;
         }
@@ -139,25 +139,47 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
         }
 
         $class = $this->getGroupClass();
-        $connection = $this->object->getConnection();
 
         if ($group instanceof $class) {
             $this->groups[$group->object_name] = $group;
+
         } elseif (is_string($group)) {
+
+            $connection = $this->object->getConnection();
+
+            // TODO: fix this, prefetch or whatever - this is expensive
             $query = $this->object->getDb()->select()->from(
                 $this->getGroupTableName()
             )->where('object_name = ?', $group);
             $groups = $class::loadAll($connection, $query, 'object_name');
-        }
-        if (! array_key_exists($group, $groups)) {
+
+            if (! array_key_exists($group, $groups)) {
+                switch ($onError) {
+                    case 'autocreate':
+                        $groups[$group] = $class::create(array(
+                            'object_type' => 'object',
+                            'object_name' => $group
+                        ));
+                        $groups[$group]->store($connection);
+                        // TODO
+                    case 'fail':
+                        throw new ProgrammingError(
+                            'The group "%s" doesn\'t exists.',
+                            $group
+                        );
+                    break;
+                    case 'ignore':
+                        return $this;
+                }
+            }
+        } else {
             throw new ProgrammingError(
-                'The group "%s" doesn\'t exists.',
-                $group
+                'Invalid group object: %s',
+                var_export($group, 1)
             );
         }
 
         $this->groups[$group] = $groups[$group];
-
         $this->modified = true;
         $this->refreshIndex();
 



More information about the icinga-checkins mailing list