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