From 092bb8c3f6ea3f3f5707dd8abc0dc3b63bdc66ce Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sat, 5 Mar 2016 10:39:15 +0100 Subject: [PATCH] IcingaObjectGroups: simplify setting groups --- library/Director/Objects/IcingaObject.php | 17 +++++++++---- .../Director/Objects/IcingaObjectGroups.php | 24 ++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index e05206b9..af346f37 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -222,11 +222,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public function set($key, $value) { - if ($key === 'groups') { - $this->groups()->set($value); - return $this; - - } elseif ($key === 'imports') { + if ($key === 'imports') { $this->imports()->set($value); return $this; @@ -488,6 +484,17 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $vars; } + public function getGroups() + { + return $this->groups()->listGroupNames(); + } + + public function setGroups($groups) + { + $this->groups()->set($groups); + return $this; + } + protected function getResolved($what) { $func = 'resolve' . $what; diff --git a/library/Director/Objects/IcingaObjectGroups.php b/library/Director/Objects/IcingaObjectGroups.php index 479cd2c5..76bd884a 100644 --- a/library/Director/Objects/IcingaObjectGroups.php +++ b/library/Director/Objects/IcingaObjectGroups.php @@ -77,17 +77,33 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer public function set($group) { + if (! is_array($group)) { + $group = array($group); + } + $existing = array_keys($this->groups); $new = array(); $class = $this->getGroupClass(); - foreach ($group as $g) { + $unset = array(); + + foreach ($group as $k => $g) { if ($g instanceof $class) { $new[] = $g->object_name; } else { + if (empty($g)) { + $unset[] = $k; + continue; + } + $new[] = $g; } } + + foreach ($unset as $k) { + unset($group[$k]); + } + sort($existing); sort($new); if ($existing === $new) { @@ -95,6 +111,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer } $this->groups = array(); + if (empty($group)) { + $this->modified = true; + $this->refreshIndex(); + return $this; + } + return $this->add($group); }