Sync: respect 'replace' policy on vars...

...with rule policy being set to 'merge'

fixes #864
This commit is contained in:
Thomas Gelf 2017-08-03 10:51:13 +02:00
parent 7c47b5a8d1
commit 496be4fbc8
2 changed files with 29 additions and 12 deletions

View File

@ -51,7 +51,7 @@ class Sync
/**
* Objects to work with
*
* @var array
* @var IcingaObject[]
*/
protected $objects;
@ -72,6 +72,8 @@ class Sync
protected $syncProperties;
protected $replaceVars = false;
/**
* @var SyncRun
*/
@ -181,6 +183,10 @@ class Sync
{
$this->syncProperties = $this->rule->fetchSyncProperties();
foreach ($this->syncProperties as $key => $prop) {
if ($prop->destination_field === 'vars' && $prop->merge_policy === 'override') {
$this->replaceVars = true;
}
if (! strlen($prop->filter_expression)) {
continue;
}
@ -247,8 +253,8 @@ class Sync
/**
* Fetch latest imported data rows from all involved import sources
*
* @return self
* @return Sync
* @throws IcingaException
*/
protected function fetchImportedData()
{
@ -395,7 +401,7 @@ class Sync
// TODO: should be obsoleted by a better "loadFiltered" method
if ($this->rule->object_type === 'datalistEntry') {
$this->removeForeignListEntries($this->objects);
$this->removeForeignListEntries();
}
return $this;
@ -500,13 +506,16 @@ class Sync
return $this;
}
/**
* @param IcingaObject $object
* @return $this
*/
protected function setResolver($object)
{
if (! ($object instanceof IcingaObject)) {
if (! ($object instanceof IcingaHost || $object instanceof IcingaHostGroup)) {
return $this;
}
if ($resolver = $this->gethostGroupMembershipResolver()) {
/** @var IcingaHost|IcingaHostGroup $object */
$object->setHostGroupMembershipResolver($resolver);
}
@ -522,6 +531,9 @@ class Sync
return $this;
}
/**
* @return bool|HostGroupMembershipResolver
*/
protected function gethostGroupMembershipResolver()
{
if ($this->hostGroupMembershipResolver === null) {
@ -577,7 +589,7 @@ class Sync
case 'merge':
// TODO: re-evaluate merge settings. vars.x instead of
// just "vars" might suffice.
$this->objects[$key]->merge($object);
$this->objects[$key]->merge($object, $this->replaceVars);
break;
default:
@ -620,8 +632,9 @@ class Sync
/**
* Runs a SyncRule and applies all resulting changes
*
* @return int
* @throws Exception
* @throws IcingaException
*/
public function apply()
{

View File

@ -2412,7 +2412,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
// TODO: with rules? What if I want to override vars? Drop in favour of vars.x?
public function merge(IcingaObject $object)
public function merge(IcingaObject $object, $replaceVars = false)
{
$object = clone($object);
@ -2425,9 +2425,13 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if ($object->supportsCustomVars()) {
$myVars = $this->vars();
/** @var CustomVariables $vars */
foreach ($vars as $key => $var) {
$myVars->set($key, $var);
if ($replaceVars) {
$this->set('vars', $vars);
} else {
/** @var CustomVariables $vars */
foreach ($vars as $key => $var) {
$myVars->set($key, $var);
}
}
}