IcingaObject: special treatment for groups

refs #1146
This commit is contained in:
Thomas Gelf 2017-08-31 22:19:27 +02:00
parent 168369e738
commit 19e51e15e4
3 changed files with 56 additions and 4 deletions

View File

@ -2445,8 +2445,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$object->set('vars', []);
}
if ($object->supportsGroups()) {
$groups = $object->getGroups();
$object->set('groups', []);
}
$plain = (array) $object->toPlainObject(null, false);
unset($plain['vars']);
unset($plain['groups']);
foreach ($plain as $p => $v) {
if ($v === null) {
// We want default values, but no null values
@ -2468,6 +2474,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
if ($object->supportsGroups()) {
if (! empty($groups)) {
$this->set('groups', $groups);
}
}
return $this;
}

View File

@ -164,16 +164,20 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
return $this;
}
if (array_key_exists($group, $this->groups)) {
return $this;
}
/** @var IcingaObjectGroup $class */
$class = $this->getGroupClass();
if ($group instanceof $class) {
if (array_key_exists($group->getObjectName(), $this->groups)) {
return $this;
}
$this->groups[$group->object_name] = $group;
} elseif (is_string($group)) {
if (array_key_exists($group, $this->groups)) {
return $this;
}
$connection = $this->object->getConnection();
try {

View File

@ -7,6 +7,7 @@ use Icinga\Module\Director\Data\PropertiesFilter\CustomVariablesFilter;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaHostGroup;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Test\BaseTestCase;
use Icinga\Exception\IcingaException;
@ -621,6 +622,41 @@ class IcingaHostTest extends BaseTestCase
);
}
public function testMergingObjectKeepsGroupsIfNotGiven()
{
$one = IcingaHostGroup::create([
'object_name' => 'one',
'object_type' => 'object',
]);
$two = IcingaHostGroup::create([
'object_name' => 'two',
'object_type' => 'object',
]);
$a = IcingaHost::create([
'object_name' => 'one',
'object_type' => 'object',
'imports' => [],
'address' => '127.0.0.2',
'groups' => [$one, $two]
]);
$b = IcingaHost::create([
'object_name' => 'one',
'object_type' => 'object',
'imports' => [],
'address' => '127.0.0.42',
]);
$a->merge($b);
$this->assertEquals(
'127.0.0.42',
$a->get('address')
);
$this->assertEquals(
['one', 'two'],
$a->getGroups()
);
}
protected function getDummyRelatedProperties()
{