From 39be7b2cb0404443f5bd1ef420f62794006c53c6 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 25 Oct 2016 00:00:16 +0000 Subject: [PATCH] IcingaObjectLegacyAssignments: rendering for old... ...assignments still to be found in our activity log --- library/Director/Objects/IcingaObject.php | 5 ++ .../Objects/IcingaObjectLegacyAssignments.php | 79 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 library/Director/Objects/IcingaObjectLegacyAssignments.php diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 7e64c608..7c68d14f 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -386,6 +386,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this->supportsSets; } + public function setAssignments($value) + { + return IcingaObjectLegacyAssignments::applyToObject($this, $value); + } + /** * @codingStandardsIgnoreStart */ diff --git a/library/Director/Objects/IcingaObjectLegacyAssignments.php b/library/Director/Objects/IcingaObjectLegacyAssignments.php new file mode 100644 index 00000000..f30b06bb --- /dev/null +++ b/library/Director/Objects/IcingaObjectLegacyAssignments.php @@ -0,0 +1,79 @@ +supportsAssignments()) { + throw new ProgrammingError( + 'I can only assign for applied objects, got %s', + $object->object_type + ); + } + + if ($values === null) { + return $object; + } + + if (! is_array($values)) { + static::throwCompatError(); + } + + if (empty($values)) { + return $object; + } + + $assigns = array(); + $ignores = array(); + foreach ($values as $type => $value) { + if (strpos($value, '|') !== false || strpos($value, '&' !== false)) { + $value = '(' . $value . ')'; + } + + if ($type === 'assign') { + $assigns[] = $value; + } elseif ($type === 'ignore') { + $ignores[] = $value; + } else { + static::throwCompatError(); + } + } + + $assign = implode('|', $assigns); + $ignore = implode('&', $ignores); + if (empty($assign)) { + $filter = $ignore; + } elseif (empty($ignore)) { + $filter = $assign; + } else { + if (count($assigns) === 1) { + $filter = $assign . '&' . $ignore; + } else { + $filter = '(' . $assign . ')&(' . $ignore . ')'; + } + } + + $object->assign_filter = $filter; + + return $object; + } + + protected static function throwCompatError() + { + throw new ProgrammingError( + 'You ran into an unexpected compatibility issue. Please report' + . ' this with details helping us to reproduce this to the' + . ' Icinga project' + ); + } +}