Sync: move key-related logic to SyncRule

This commit is contained in:
Thomas Gelf 2016-07-13 19:59:04 +02:00
parent 465740c37c
commit 70c4d06a42
2 changed files with 90 additions and 53 deletions

View File

@ -61,12 +61,6 @@ class Sync
protected $errors = array();
protected $hasCombinedKey;
protected $sourceKeyPattern;
protected $destinationKeyPattern;
protected $syncProperties;
protected $run;
@ -233,46 +227,6 @@ class Sync
return $this;
}
/**
* Whether we have a combined key (e.g. services on hosts)
*
* @return bool
*/
protected function hasCombinedKey()
{
if ($this->hasCombinedKey === null) {
$this->hasCombinedKey = false;
if ($this->rule->object_type === 'service') {
$hasHost = false;
$hasObjectName = false;
foreach ($this->syncProperties as $key => $property) {
if ($property->destination_field === 'host') {
$hasHost = $property->source_expression;
}
if ($property->destination_field === 'object_name') {
$hasObjectName = $property->source_expression;
}
}
if ($hasHost !== false && $hasObjectName !== false) {
$this->hasCombinedKey = true;
$this->sourceKeyPattern = sprintf(
'%s!%s',
$hasHost,
$hasObjectName
);
$this->destinationKeyPattern = '${host}!${object_name}';
}
}
}
return $this->hasCombinedKey;
}
/**
* Fetch latest imported data rows from all involved import sources
*
@ -282,6 +236,9 @@ class Sync
{
$this->imported = array();
$sourceKeyPattern = $this->rule->getSourceKeyPattern();
$combinedKey = $this->rule->hasCombinedKey();
foreach ($this->sources as $source) {
$sourceId = $source->id;
@ -293,14 +250,14 @@ class Sync
$this->imported[$sourceId] = array();
foreach ($rows as $row) {
if ($this->hasCombinedKey()) {
$key = SyncUtils::fillVariables($this->sourceKeyPattern, $row);
if ($combinedKey) {
$key = SyncUtils::fillVariables($sourceKeyPattern, $row);
if (array_key_exists($key, $this->imported[$sourceId])) {
throw new IcingaException(
'Trying to import row "%s" (%s) twice: %s VS %s',
$key,
$this->sourceKeyPattern,
$sourceKeyPattern,
json_encode($this->imported[$sourceId][$key]),
json_encode($row)
);
@ -323,7 +280,7 @@ class Sync
continue;
}
if ($this->hasCombinedKey()) {
if ($combinedKey) {
$this->imported[$sourceId][$key] = $row;
} else {
$this->imported[$sourceId][$row->$key] = $row;
@ -367,9 +324,10 @@ class Sync
protected function loadExistingObjects()
{
// TODO: Make object_type (template, object...) and object_name mandatory?
if ($this->hasCombinedKey()) {
if ($this->rule->hasCombinedKey()) {
$this->objects = array();
$destinationKeyPattern = $this->rule->getDestinationKeyPattern();
foreach (IcingaObject::loadAllByType(
$this->rule->object_type,
@ -383,14 +341,14 @@ class Sync
}
$key = SyncUtils::fillVariables(
$this->destinationKeyPattern,
$destinationKeyPattern,
$object
);
if (array_key_exists($key, $this->objects)) {
throw new IcingaException(
'Combined destination key "%s" is not unique, got "%s" twice',
$this->destinationKeyPattern,
$destinationKeyPattern,
$key
);
}

View File

@ -37,6 +37,14 @@ class SyncRule extends DbObject
private $filter;
private $hasCombinedKey;
private $syncProperties;
private $sourceKeyPattern;
private $destinationKeyPattern;
public function listInvolvedSourceIds()
{
if (! $this->hasBeenLoadedFromDb()) {
@ -179,6 +187,24 @@ class SyncRule extends DbObject
return $this->currentSyncRunId;
}
public function getSourceKeyPattern()
{
if ($this->hasCombinedKey()) {
return $this->sourceKeyPattern;
} else {
return null; // ??
}
}
public function getDestinationKeyPattern()
{
if ($this->hasCombinedKey()) {
return $this->destinationKeyPattern;
} else {
return null; // ??
}
}
protected function sync()
{
if ($this->sync === null) {
@ -216,6 +242,59 @@ class SyncRule extends DbObject
}
}
/**
* Whether we have a combined key (e.g. services on hosts)
*
* @return bool
*/
public function hasCombinedKey()
{
if ($this->hasCombinedKey === null) {
$this->hasCombinedKey = false;
if ($this->object_type === 'service') {
$hasHost = false;
$hasObjectName = false;
foreach ($this->getSyncProperties() as $key => $property) {
if ($property->destination_field === 'host') {
$hasHost = $property->source_expression;
}
if ($property->destination_field === 'object_name') {
$hasObjectName = $property->source_expression;
}
}
if ($hasHost !== false && $hasObjectName !== false) {
$this->hasCombinedKey = true;
$this->sourceKeyPattern = sprintf(
'%s!%s',
$hasHost,
$hasObjectName
);
$this->destinationKeyPattern = '${host}!${object_name}';
}
}
}
return $this->hasCombinedKey;
}
public function getSyncProperties()
{
if (! $this->hasBeenLoadedFromDb()) {
return array();
}
if ($this->syncProperties === null) {
$this->syncProperties = $this->fetchSyncProperties();
}
return $this->syncProperties;
}
public function fetchSyncProperties()
{
$db = $this->getDb();