[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: IcingaNotificationAssignmentForm: new form

git at icinga.org git at icinga.org
Thu May 19 16:46:17 CEST 2016


Module: icingaweb2-module-director
Branch: fiddle/mfrosch
Commit: a9e3cb767ef3d56304ab3f45fc5f400be9c1bbe0
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=a9e3cb767ef3d56304ab3f45fc5f400be9c1bbe0

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu May 19 15:06:56 2016 +0200

IcingaNotificationAssignmentForm: new form

---

 .../forms/IcingaNotificationAssignmentForm.php     |  151 ++++++++++++++++++++
 1 file changed, 151 insertions(+)

diff --git a/application/forms/IcingaNotificationAssignmentForm.php b/application/forms/IcingaNotificationAssignmentForm.php
new file mode 100644
index 0000000..3d49803
--- /dev/null
+++ b/application/forms/IcingaNotificationAssignmentForm.php
@@ -0,0 +1,151 @@
+<?php
+
+namespace Icinga\Module\Director\Forms;
+
+use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
+use Icinga\Module\Director\Objects\IcingaObject;
+use Icinga\Module\Director\Objects\IcingaHost;
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+
+class IcingaNotificationAssignmentForm extends DirectorObjectForm
+{
+    /**
+     *
+     * Please note that $object would conflict with logic in parent class
+     */
+    private $icingaObject;
+
+    protected $db;
+
+    public function setDb($db)
+    {
+        $this->db = $db;
+        return $this;
+    }
+
+    public function setIcingaObject($object)
+    {
+        $this->icingaObject = $object;
+        return $this;
+    }
+
+    public function setup()
+    {
+        $this->addHidden('service_id', $this->icingaObject->id);
+
+        if ($this->icingaObject->isTemplate()) {
+            $this->addHtmlHint(
+                'Assign all services importing this service template to one or'
+                . ' more hosts'
+            );
+        } else {
+            $this->addHtmlHint(
+                'Assign this service to one or more hosts'
+            );
+        }
+
+        $this->addElement('select', 'object_type', array(
+            'label'        => 'Assign',
+            'required'     => true,
+            'ignore'       => true,
+            'multiOptions' => $this->optionalEnum(
+                array(
+                    'host_group'    => $this->translate('to a host group'),
+                    'host_property' => $this->translate('by host property'),
+                    'host_group_property' => $this->translate('by host group property'),
+                )
+            ),
+            'class' => 'autosubmit'
+
+        ));
+
+        if ($this->hasObject()) {// immer da??
+            $this->setElementValue('object_type', 'host_property');
+        }
+
+        switch ($this->getSentValue('object_type', $this->getValue('object_type'))) {
+            case 'host_group':
+                $this->addHostGroupElements();
+                break;
+            case 'host_property':
+                $this->addHostPropertyElements();
+                break;
+            case 'host_property':
+                $this->addHostFilterElements();
+                break;
+        }
+
+        if ($this->object) {
+            $this->setElementValue('host_property', 'host.address');
+        }
+
+        $this->setSubmitLabel(
+            $this->translate('Assign')
+        );
+        $this->setButtons();
+    }
+
+    protected function addHostGroupElements()
+    {
+        $this->addElement('select', 'host_id', array(
+            'label'        => 'Hostgroup',
+            'ignore'       => true,
+            'required'     => true,
+            'multiOptions' => $this->optionalEnum($this->db->enumHostgroups())
+        ));
+    }
+
+    protected function addHostPropertyElements()
+    {
+        $this->addElement('select', 'host_property', array(
+            'label'        => 'Host property',
+            'ignore'       => true,
+            'required'     => true,
+            'multiOptions' => $this->optionalEnum(IcingaHost::enumProperties($this->db))
+        ));
+        $this->addElement('text', 'filter_expression', array(
+            'label'        => 'Filter expression',
+            'ignore'       => true,
+            'required'     => true,
+        ));
+    }
+
+    protected function addHostFilterElements()
+    {
+        $this->addElement('text', 'host_filter', array(
+            'label'        => 'Host filter string',
+            'required'     => true,
+        ));
+    }
+
+    public function onSuccess()
+    {
+        switch ($this->getValue('object_type')) {
+            case 'host_group':
+                $this->db->insert('icinga_service_assignment', array(
+                    'service_id'    => $this->getValue('service_id'),
+                    // TODO: in?
+                    'filter_string' => 'groups=' . $this->getValue('host_group'),
+                ));
+                break;
+            case 'host_property':
+                $filter = sprintf(
+                    'host.%s=%s',
+                    $this->getValue('host_property'),
+                    c::renderString($this->getValue('filter_expression'))
+                );
+
+                $this->setElementValue('filter_string', $filter);
+                $this->object()->filter_string = $filter;
+                break;
+            case 'host_filter':
+                $this->db->insert('icinga_service_assignment', array(
+                    'service_id'    => $this->getValue('service_id'),
+                    'filter_string' => $this->getValue('filter_string'),
+                ));
+                break;
+        }
+
+        return parent::onSuccess();
+    }
+}



More information about the icinga-checkins mailing list