Sync: fix sync & purge for datalistEntry objects

This commit is contained in:
Thomas Gelf 2016-10-05 17:45:25 +00:00
parent c859055221
commit e9a570e96d
5 changed files with 47 additions and 7 deletions

View File

@ -70,11 +70,12 @@ class ImportRunBasedPurgeStrategy extends PurgeStrategy
return array(); return array();
} }
if ($rule->object_type === 'service') { if ($rule->hasCombinedKey()) {
$pattern = $rule->getSourceKeyPattern(); $pattern = $rule->getSourceKeyPattern();
$columns = SyncUtils::getRootVariables( $columns = SyncUtils::getRootVariables(
SyncUtils::extractVariableNames($pattern) SyncUtils::extractVariableNames($pattern)
); );
$rows = $runA->fetchRows($columns, null, $result); $rows = $runA->fetchRows($columns, null, $result);
$result = array(); $result = array();
foreach ($rows as $row) { foreach ($rows as $row) {

View File

@ -101,7 +101,7 @@ class Sync
foreach ($objects as $object) { foreach ($objects as $object) {
if ($object->hasBeenModified()) { if ($object->hasBeenModified()) {
$modified[] = $object; $modified[] = $object;
} elseif ($object instanceof IcingaObject && $object->shouldBeRemoved()) { } elseif ($object->shouldBeRemoved()) {
$modified[] = $object; $modified[] = $object;
} }
} }
@ -313,7 +313,7 @@ class Sync
$no = array(); $no = array();
foreach ($this->objects as $k => $o) { foreach ($this->objects as $k => $o) {
if ($o->list_id !== $listId) { if ((int) $o->list_id !== (int) $listId) {
$no[] = $k; $no[] = $k;
} }
} }
@ -557,13 +557,13 @@ class Sync
if ($object->hasBeenModified()) { if ($object->hasBeenModified()) {
throw new IcingaException( throw new IcingaException(
'Sync is not allowed to modify template "%s"', 'Sync is not allowed to modify template "%s"',
$object->$objectKey $object->object_name
); );
} }
continue; continue;
} }
if ($object instanceof IcingaObject && $object->shouldBeRemoved()) { if ($object->shouldBeRemoved()) {
$object->delete($db); $object->delete($db);
$deleted++; $deleted++;
continue; continue;

View File

@ -3,7 +3,7 @@
namespace Icinga\Module\Director\Import; namespace Icinga\Module\Director\Import;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Data\Db\DbObject;
class SyncUtils class SyncUtils
{ {
@ -65,7 +65,7 @@ class SyncUtils
public static function getSpecificValue($row, $var) public static function getSpecificValue($row, $var)
{ {
if (strpos($var, '.') === false) { if (strpos($var, '.') === false) {
if ($row instanceof IcingaObject) { if ($row instanceof DbObject) {
return $row->$var; return $row->$var;
} }
if (! property_exists($row, $var)) { if (! property_exists($row, $var)) {

View File

@ -19,6 +19,20 @@ class DirectorDatalistEntry extends DbObject
'format' => null, 'format' => null,
); );
public function replaceWith(DirectorDatalistEntry $object)
{
$this->entry_value = $object->entry_value;
if ($object->format) {
$this->format = $object->format;
}
return $this;
}
public function merge(DirectorDatalistEntry $object)
{
return $this->replaceWith($object);
}
public function markForRemoval($remove = true) public function markForRemoval($remove = true)
{ {

View File

@ -253,6 +253,7 @@ class SyncRule extends DbObject
$this->hasCombinedKey = false; $this->hasCombinedKey = false;
// TODO: Move to Objects
if ($this->object_type === 'service') { if ($this->object_type === 'service') {
$hasHost = false; $hasHost = false;
$hasObjectName = false; $hasObjectName = false;
@ -276,6 +277,30 @@ class SyncRule extends DbObject
$this->destinationKeyPattern = '${host}!${object_name}'; $this->destinationKeyPattern = '${host}!${object_name}';
} }
} elseif ($this->object_type === 'datalistEntry') {
$hasList = false;
$hasName = false;
foreach ($this->getSyncProperties() as $key => $property) {
if ($property->destination_field === 'list_id') {
$hasList = $property->source_expression;
}
if ($property->destination_field === 'entry_name') {
$hasName = $property->source_expression;
}
}
if ($hasList !== false && $hasName !== false) {
$this->hasCombinedKey = true;
$this->sourceKeyPattern = sprintf(
'%s!%s',
$hasList,
$hasName
);
$this->destinationKeyPattern = '${list_id}!${entry_name}';
}
} }
} }