From 70c4d06a42ad02e0d386fbc0c7e7e4d9cc323bff Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 13 Jul 2016 19:59:04 +0200 Subject: [PATCH] Sync: move key-related logic to SyncRule --- library/Director/Import/Sync.php | 64 ++++------------------ library/Director/Objects/SyncRule.php | 79 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 53 deletions(-) diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php index c3d39785..e8b8c52e 100644 --- a/library/Director/Import/Sync.php +++ b/library/Director/Import/Sync.php @@ -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 ); } diff --git a/library/Director/Objects/SyncRule.php b/library/Director/Objects/SyncRule.php index 658ec06b..ca315f10 100644 --- a/library/Director/Objects/SyncRule.php +++ b/library/Director/Objects/SyncRule.php @@ -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();