SyncRule: cleanup, remove magic method access

This commit is contained in:
Thomas Gelf 2018-11-24 10:17:18 +01:00
parent 01f82b2d48
commit a3a2b5425b
1 changed files with 44 additions and 43 deletions

View File

@ -18,8 +18,8 @@ use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\SyncProperty; use Icinga\Module\Director\Objects\SyncProperty;
use Icinga\Module\Director\Objects\SyncRule; use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Objects\SyncRun; use Icinga\Module\Director\Objects\SyncRun;
use Icinga\Module\Director\Util;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
use InvalidArgumentException;
class Sync class Sync
{ {
@ -168,20 +168,21 @@ class Sync
{ {
$this->syncProperties = $this->rule->getSyncProperties(); $this->syncProperties = $this->rule->getSyncProperties();
foreach ($this->syncProperties as $key => $prop) { foreach ($this->syncProperties as $key => $prop) {
if ($prop->destination_field === 'vars' && $prop->merge_policy === 'override') { $destinationField = $prop->get('destination_field');
if ($destinationField === 'vars' && $prop->get('merge_policy') === 'override') {
$this->replaceVars = true; $this->replaceVars = true;
} }
if ($prop->destination_field === 'disabled') { if ($destinationField === 'disabled') {
$this->hasPropertyDisabled = true; $this->hasPropertyDisabled = true;
} }
if (! strlen($prop->filter_expression)) { if (! strlen($prop->get('filter_expression'))) {
continue; continue;
} }
$this->columnFilters[$key] = Filter::fromQueryString( $this->columnFilters[$key] = Filter::fromQueryString(
$prop->filter_expression $prop->get('filter_expression')
); );
} }
@ -208,7 +209,7 @@ class Sync
{ {
$this->sources = []; $this->sources = [];
foreach ($this->syncProperties as $p) { foreach ($this->syncProperties as $p) {
$id = $p->source_id; $id = $p->get('source_id');
if (! array_key_exists($id, $this->sources)) { if (! array_key_exists($id, $this->sources)) {
$this->sources[$id] = ImportSource::loadWithAutoIncId( $this->sources[$id] = ImportSource::loadWithAutoIncId(
(int) $id, (int) $id,
@ -231,12 +232,12 @@ class Sync
$this->sourceColumns = []; $this->sourceColumns = [];
foreach ($this->syncProperties as $p) { foreach ($this->syncProperties as $p) {
$sourceId = $p->source_id; $sourceId = $p->get('source_id');
if (! array_key_exists($sourceId, $this->sourceColumns)) { if (! array_key_exists($sourceId, $this->sourceColumns)) {
$this->sourceColumns[$sourceId] = []; $this->sourceColumns[$sourceId] = [];
} }
foreach (SyncUtils::extractVariableNames($p->source_expression) as $varname) { foreach (SyncUtils::extractVariableNames($p->get('source_expression')) as $varname) {
$this->sourceColumns[$sourceId][$varname] = $varname; $this->sourceColumns[$sourceId][$varname] = $varname;
// -> ? $fieldMap[ // -> ? $fieldMap[
} }
@ -253,7 +254,7 @@ class Sync
protected function fetchImportedData() protected function fetchImportedData()
{ {
Benchmark::measure('Begin loading imported data'); Benchmark::measure('Begin loading imported data');
if ($this->rule->object_type === 'host') { if ($this->rule->get('object_type') === 'host') {
$this->serviceOverrideKeyName = $this->db->settings()->override_services_varname; $this->serviceOverrideKeyName = $this->db->settings()->override_services_varname;
} }
@ -264,7 +265,7 @@ class Sync
foreach ($this->sources as $source) { foreach ($this->sources as $source) {
/** @var ImportSource $source */ /** @var ImportSource $source */
$sourceId = $source->id; $sourceId = $source->get('id');
// Provide an alias column for our key. TODO: double-check this! // Provide an alias column for our key. TODO: double-check this!
$key = $source->key_column; $key = $source->key_column;
@ -301,22 +302,22 @@ class Sync
$key = SyncUtils::fillVariables($sourceKeyPattern, $row); $key = SyncUtils::fillVariables($sourceKeyPattern, $row);
if (array_key_exists($key, $this->imported[$sourceId])) { if (array_key_exists($key, $this->imported[$sourceId])) {
throw new IcingaException( throw new InvalidArgumentException(sprintf(
'Trying to import row "%s" (%s) twice: %s VS %s', 'Trying to import row "%s" (%s) twice: %s VS %s',
$key, $key,
$sourceKeyPattern, $sourceKeyPattern,
json_encode($this->imported[$sourceId][$key]), json_encode($this->imported[$sourceId][$key]),
json_encode($row) json_encode($row)
); ));
} }
} else { } else {
if (! property_exists($row, $key)) { if (! property_exists($row, $key)) {
throw new IcingaException( throw new InvalidArgumentException(sprintf(
'There is no key column "%s" in this row from "%s": %s', 'There is no key column "%s" in this row from "%s": %s',
$key, $key,
$source->source_name, $source->source_name,
json_encode($row) json_encode($row)
); ));
} }
} }
@ -341,27 +342,25 @@ class Sync
/** /**
* TODO: This is rubbish, we need to filter at fetch time * TODO: This is rubbish, we need to filter at fetch time
*
* @throws IcingaException
*/ */
protected function removeForeignListEntries() protected function removeForeignListEntries()
{ {
$listId = null; $listId = null;
foreach ($this->syncProperties as $prop) { foreach ($this->syncProperties as $prop) {
if ($prop->destination_field === 'list_id') { if ($prop->get('destination_field') === 'list_id') {
$listId = (int) $prop->source_expression; $listId = (int) $prop->get('source_expression');
} }
} }
if ($listId === null) { if ($listId === null) {
throw new IcingaException( throw new InvalidArgumentException(
'Cannot sync datalist entry without list_ist' 'Cannot sync datalist entry without list_ist'
); );
} }
$no = []; $no = [];
foreach ($this->objects as $k => $o) { foreach ($this->objects as $k => $o) {
if ((int) $o->list_id !== (int) $listId) { if ((int) $o->get('list_id') !== (int) $listId) {
$no[] = $k; $no[] = $k;
} }
} }
@ -373,25 +372,29 @@ class Sync
/** /**
* @return $this * @return $this
* @throws IcingaException
*/ */
protected function loadExistingObjects() protected function loadExistingObjects()
{ {
Benchmark::measure('Begin loading existing objects'); Benchmark::measure('Begin loading existing objects');
$ruleObjectType = $this->rule->get('object_type');
// TODO: Make object_type (template, object...) and object_name mandatory? // TODO: Make object_type (template, object...) and object_name mandatory?
if ($this->rule->hasCombinedKey()) { if ($this->rule->hasCombinedKey()) {
$this->objects = []; $this->objects = [];
$destinationKeyPattern = $this->rule->getDestinationKeyPattern(); $destinationKeyPattern = $this->rule->getDestinationKeyPattern();
foreach (IcingaObject::loadAllByType( foreach (IcingaObject::loadAllByType(
$this->rule->object_type, $ruleObjectType,
$this->db $this->db
) as $object) { ) as $object) {
if ($object instanceof IcingaService) { if ($object instanceof IcingaService) {
if (strstr($destinationKeyPattern, '${host}') && $object->host_id === null) { if (strstr($destinationKeyPattern, '${host}')
&& $object->get('host_id') === null
) {
continue; continue;
} elseif (strstr($destinationKeyPattern, '${service_set}') && $object->service_set_id === null) { } elseif (strstr($destinationKeyPattern, '${service_set}')
&& $object->get('service_set_id') === null
) {
continue; continue;
} }
} }
@ -402,24 +405,24 @@ class Sync
); );
if (array_key_exists($key, $this->objects)) { if (array_key_exists($key, $this->objects)) {
throw new IcingaException( throw new InvalidArgumentException(sprintf(
'Combined destination key "%s" is not unique, got "%s" twice', 'Combined destination key "%s" is not unique, got "%s" twice',
$destinationKeyPattern, $destinationKeyPattern,
$key $key
); ));
} }
$this->objects[$key] = $object; $this->objects[$key] = $object;
} }
} else { } else {
$this->objects = IcingaObject::loadAllByType( $this->objects = IcingaObject::loadAllByType(
$this->rule->object_type, $ruleObjectType,
$this->db $this->db
); );
} }
// 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 ($ruleObjectType === 'datalistEntry') {
$this->removeForeignListEntries(); $this->removeForeignListEntries();
} }
@ -437,6 +440,7 @@ class Sync
protected function prepareNewObjects() protected function prepareNewObjects()
{ {
$objects = []; $objects = [];
$ruleObjectType = $this->rule->get('object_type');
foreach ($this->sources as $source) { foreach ($this->sources as $source) {
$sourceId = $source->id; $sourceId = $source->id;
@ -444,7 +448,7 @@ class Sync
foreach ($this->imported[$sourceId] as $key => $row) { foreach ($this->imported[$sourceId] as $key => $row) {
if (! array_key_exists($key, $objects)) { if (! array_key_exists($key, $objects)) {
// Safe default values for object_type and object_name // Safe default values for object_type and object_name
if ($this->rule->object_type === 'datalistEntry') { if ($ruleObjectType === 'datalistEntry') {
$props = []; $props = [];
} else { } else {
$props = [ $props = [
@ -454,7 +458,7 @@ class Sync
} }
$objects[$key] = IcingaObject::createByType( $objects[$key] = IcingaObject::createByType(
$this->rule->object_type, $ruleObjectType,
$props, $props,
$this->db $this->db
); );
@ -479,7 +483,7 @@ class Sync
protected function prepareNewObject($row, DbObject $object, $sourceId) protected function prepareNewObject($row, DbObject $object, $sourceId)
{ {
foreach ($this->syncProperties as $propertyKey => $p) { foreach ($this->syncProperties as $propertyKey => $p) {
if ($p->source_id !== $sourceId) { if ($p->get('source_id') !== $sourceId) {
continue; continue;
} }
@ -487,9 +491,9 @@ class Sync
continue; continue;
} }
$prop = $p->destination_field; $prop = $p->get('destination_field');
$val = SyncUtils::fillVariables($p->source_expression, $row); $val = SyncUtils::fillVariables($p->get('source_expression'), $row);
if ($object instanceof IcingaObject) { if ($object instanceof IcingaObject) {
if ($prop === 'import') { if ($prop === 'import') {
@ -527,7 +531,6 @@ class Sync
/** /**
* @return $this * @return $this
* @throws IcingaException
*/ */
protected function deferResolvers() protected function deferResolvers()
{ {
@ -542,7 +545,6 @@ class Sync
/** /**
* @param DbObject $object * @param DbObject $object
* @return $this * @return $this
* @throws IcingaException
*/ */
protected function setResolver($object) protected function setResolver($object)
{ {
@ -558,7 +560,7 @@ class Sync
/** /**
* @return $this * @return $this
* @throws IcingaException * @throws \Zend_Db_Adapter_Exception
*/ */
protected function notifyResolvers() protected function notifyResolvers()
{ {
@ -571,7 +573,6 @@ class Sync
/** /**
* @return bool|HostGroupMembershipResolver * @return bool|HostGroupMembershipResolver
* @throws IcingaException
*/ */
protected function getHostGroupMembershipResolver() protected function getHostGroupMembershipResolver()
{ {
@ -663,7 +664,6 @@ class Sync
* @param $key * @param $key
* @param DbObject|IcingaObject $object * @param DbObject|IcingaObject $object
* @throws IcingaException * @throws IcingaException
* @throws \Icinga\Exception\ProgrammingError
*/ */
protected function processObject($key, $object) protected function processObject($key, $object)
{ {
@ -678,7 +678,6 @@ class Sync
* @param $key * @param $key
* @param DbObject|IcingaObject $object * @param DbObject|IcingaObject $object
* @throws IcingaException * @throws IcingaException
* @throws \Icinga\Exception\ProgrammingError
*/ */
protected function refreshObject($key, $object) protected function refreshObject($key, $object)
{ {
@ -745,7 +744,8 @@ class Sync
$created = 0; $created = 0;
$modified = 0; $modified = 0;
$deleted = 0; $deleted = 0;
$failed = 0; // TODO: Count also failed ones, once we allow such
// $failed = 0;
foreach ($objects as $object) { foreach ($objects as $object) {
$this->setResolver($object); $this->setResolver($object);
if ($object->shouldBeRemoved()) { if ($object->shouldBeRemoved()) {
@ -806,16 +806,17 @@ class Sync
} }
} }
return $this->run->id; return $this->run->get('id');
} }
protected function prepareCache() protected function prepareCache()
{ {
PrefetchCache::initialize($this->db); PrefetchCache::initialize($this->db);
$ruleObjectType = $this->rule->get('object_type');
$dummy = IcingaObject::createByType($this->rule->object_type); $dummy = IcingaObject::createByType($ruleObjectType);
if ($dummy instanceof IcingaObject) { if ($dummy instanceof IcingaObject) {
IcingaObject::prefetchAllRelationsByType($this->rule->object_type, $this->db); IcingaObject::prefetchAllRelationsByType($ruleObjectType, $this->db);
} }
return $this; return $this;