[icinga-checkins] icinga.org: icingaweb2-module-director/feature/nested-apply-rules-12033: IcingaObjectLegacyAssignments: rendering for old...

git at icinga.org git at icinga.org
Tue Oct 25 02:00:47 CEST 2016


Module: icingaweb2-module-director
Branch: feature/nested-apply-rules-12033
Commit: 39be7b2cb0404443f5bd1ef420f62794006c53c6
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=39be7b2cb0404443f5bd1ef420f62794006c53c6

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Oct 25 00:00:16 2016 +0000

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(+)

diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index 7e64c60..7c68d14 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 0000000..f30b06b
--- /dev/null
+++ b/library/Director/Objects/IcingaObjectLegacyAssignments.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Icinga\Module\Director\Objects;
+
+use Icinga\Exception\ProgrammingError;
+
+/**
+ * This class is required for historical reasons
+ *
+ * Objects with assignments in your activity log would otherwise not be able
+ * to render themselves
+ */
+class IcingaObjectLegacyAssignments
+{
+    public static function applyToObject(IcingaObject $object, $values)
+    {
+        if (! $object->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'
+        );
+    }
+}



More information about the icinga-checkins mailing list