From f9f61be7aa454afda474bd61969b6bb20ca2b031 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 27 Oct 2016 15:39:39 +0200 Subject: [PATCH 1/3] IcingaObject: Allow replaceWith on non-apply Objects This broke with assign_filter changes, we should not throw an error when the filter is actually null. replaceWith() re-sets all vars. refs #12033 --- library/Director/Objects/IcingaObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 438eceb7..f0eb1b56 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -396,7 +396,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer */ public function setAssign_filter($filter) { - if (! $this->supportsAssignments()) { + if (! $this->supportsAssignments() && $filter !== null) { if ($this->hasProperty('object_type')) { $type = $this->object_type; } else { From 5199dc78266c404dad9b3b79893fdb1c794eb2e1 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 27 Oct 2016 15:43:26 +0200 Subject: [PATCH 2/3] Sync: Tell the user which object caused an Exception --- library/Director/Import/Sync.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php index 28fc4828..a8fe247e 100644 --- a/library/Director/Import/Sync.php +++ b/library/Director/Import/Sync.php @@ -4,7 +4,7 @@ namespace Icinga\Module\Director\Import; use Exception; use Icinga\Data\Filter\Filter; -use Icinga\Module\Director\Import\SyncUtils; +use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\ImportSource; use Icinga\Module\Director\Objects\IcingaService; @@ -20,6 +20,11 @@ class Sync */ protected $rule; + /** + * @var Db + */ + protected $db; + /** * Related ImportSource objects * @@ -63,6 +68,9 @@ class Sync protected $syncProperties; + /** + * @var SyncRun + */ protected $run; protected $runStartTime; @@ -72,6 +80,8 @@ class Sync /** * Constructor. No direct initialization allowed right now. Please use one * of the available static factory methods + * + * @param SyncRule $rule */ public function __construct(SyncRule $rule) { @@ -240,6 +250,7 @@ class Sync $combinedKey = $this->rule->hasCombinedKey(); foreach ($this->sources as $source) { + /** @var ImportSource $source */ $sourceId = $source->id; // Provide an alias column for our key. TODO: double-check this! @@ -544,6 +555,8 @@ class Sync $dba = $db->getDbAdapter(); $dba->beginTransaction(); + $object = null; + try { $formerActivityChecksum = Util::hex2binary( $db->getLastActivityChecksum() @@ -604,7 +617,15 @@ class Sync } catch (Exception $e) { $dba->rollBack(); - throw $e; + if ($object !== null && $object instanceof IcingaObject) { + throw new IcingaException( + 'Exception while syncing %s %s: %s', + get_class($object), $object->get('object_name'), $e->getMessage(), $e + ); + } + else { + throw $e; + } } return $this->run->id; From a6ad95848ba2626ac508d6cd07394dc303b1bc9d Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 27 Oct 2016 15:54:13 +0200 Subject: [PATCH 3/3] PlainObjectRenderer: Render assoc arrays as Hashes Previous handling completely ignored any keys. --- library/Director/PlainObjectRenderer.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/library/Director/PlainObjectRenderer.php b/library/Director/PlainObjectRenderer.php index 8267bd4b..11d0eead 100644 --- a/library/Director/PlainObjectRenderer.php +++ b/library/Director/PlainObjectRenderer.php @@ -98,14 +98,28 @@ class PlainObjectRenderer return self::renderInteger($object); } elseif (is_float($object)) { return self::renderFloat($object); + } elseif (is_object($object) || static::isAssocArray($object)) { + return self::renderHash($object, $prefix); } elseif (is_array($object)) { return self::renderArray($object, $prefix); - } elseif (is_object($object)) { - return self::renderHash($object, $prefix); } elseif (is_string($object)) { return self::renderString($object); } else { return '(UNKNOWN TYPE) ' . var_export($object, 1); } } + + /** + * Check if an array contains assoc keys + * + * @from https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential + * @param $arr + * @return bool + */ + protected static function isAssocArray($arr) + { + if (! is_array($arr)) return false; + if (array() === $arr) return false; + return array_keys($arr) !== range(0, count($arr) - 1); + } }