[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: Notifications: should be subject to apply rules

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


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

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

Notifications: should be subject to apply rules

---

 application/controllers/NotificationController.php |   76 ++++++++++++++++++++
 application/forms/IcingaNotificationForm.php       |   17 +++++
 application/tables/IcingaNotificationTable.php     |   74 +++++++++++++++++++
 3 files changed, 167 insertions(+)

diff --git a/application/controllers/NotificationController.php b/application/controllers/NotificationController.php
index e98550e..48f3650 100644
--- a/application/controllers/NotificationController.php
+++ b/application/controllers/NotificationController.php
@@ -3,7 +3,83 @@
 namespace Icinga\Module\Director\Controllers;
 
 use Icinga\Module\Director\Web\Controller\ObjectController;
+use Icinga\Module\Director\Objects\IcingaHost;
+use Icinga\Module\Director\Objects\IcingaNotification;
+use Icinga\Module\Director\Objects\IcingaService;
 
 class NotificationController extends ObjectController
 {
+    public function init()
+    {
+        parent::init();
+        if ($this->object && $this->object->object_type === 'apply') {
+            $this->getTabs()->add('assign', array(
+                'url'       => 'director/notification/assign',
+                'urlParams' => $this->object->getUrlParams(),
+                'label'     => 'Assign'
+            ));
+
+            if ($host = $this->params->get('host')) {
+                foreach ($this->getTabs()->getTabs() as $tab) {
+                    $tab->getUrl()->setParam('host', $host);
+                }
+            }
+
+            if ($service = $this->params->get('service')) {
+                foreach ($this->getTabs()->getTabs() as $tab) {
+                    $tab->getUrl()->setParam('service', $service);
+                }
+            }
+        }
+    }
+
+    public function assignAction()
+    {
+        $this->getTabs()->activate('assign');
+        $this->view->form = $form = $this->loadForm('icingaNotificationAssignment');
+        $form
+            ->setIcingaObject($this->object)
+            ->setDb($this->db());
+        if ($id = $this->params->get('rule_id')) {
+            $this->view->actionLinks = $this->view->qlink(
+                $this->translate('back'),
+                $this->getRequest()->getUrl()->without('rule_id'),
+                null,
+                array('class' => 'icon-left-big')
+            );
+            $form->loadObject($id);
+        }
+        $form->handleRequest();
+
+        $this->view->table = $this->loadTable('icingaObjectAssignment')
+            ->setObject($this->object);
+        $this->view->title = 'Assign notification';
+        $this->render('object/fields', null, true); // TODO: render table
+    }
+
+    protected function loadObject()
+    {
+        if ($this->object === null) {
+            if ($name = $this->params->get('name')) {
+                $params = array('object_name' => $name);
+                $db = $this->db();
+
+                if ($hostname = $this->params->get('host')) {
+                    $this->view->host = IcingaHost::load($hostname, $db);
+                    $params['host_id'] = $this->view->host->id;
+                }
+
+                if ($service = $this->params->get('service')) {
+                    $this->view->service = IcingaService::load($service, $db);
+                    $params['service_id'] = $this->view->service->id;
+                }
+
+                $this->object = IcingaNotification::load($params, $db);
+            } else {
+                parent::loadObject();
+            }
+        }
+
+        return $this->object;
+    }
 }
diff --git a/application/forms/IcingaNotificationForm.php b/application/forms/IcingaNotificationForm.php
index 5bdf93b..d705873 100644
--- a/application/forms/IcingaNotificationForm.php
+++ b/application/forms/IcingaNotificationForm.php
@@ -27,6 +27,7 @@ class IcingaNotificationForm extends DirectorObjectForm
              ->addIntervalElement()
              ->addPeriodElement()
              ->addTimesElements()
+             ->addAssignmentElements()
              ->addDisabledElement()
              ->addCommandElements()
              ->addEventFilterElements()
@@ -34,6 +35,22 @@ class IcingaNotificationForm extends DirectorObjectForm
              ->setButtons();
     }
 
+    protected function addAssignmentElements()
+    {
+        if (!$this->object || !$this->object->isApplyRule()) {
+            return $this;
+        }
+
+        $sub = new AssignListSubForm();
+        $sub->setObject($this->getObject());
+        $sub->setup();
+        $sub->setOrder(30);
+
+        $this->addSubForm($sub, 'assignlist');
+
+        return $this;
+    }
+
     protected function addUsersElement()
     {
         $users = $this->enumUsers();
diff --git a/application/tables/IcingaNotificationTable.php b/application/tables/IcingaNotificationTable.php
index d676e7e..ded4025 100644
--- a/application/tables/IcingaNotificationTable.php
+++ b/application/tables/IcingaNotificationTable.php
@@ -19,6 +19,11 @@ class IcingaNotificationTable extends IcingaObjectTable
         );
     }
 
+    protected function listTableClasses()
+    {
+        return array_merge(array('assignment-table'), parent::listTableClasses());
+    }
+
     protected function getActionUrl($row)
     {
         return $this->url('director/notification', array('id' => $row->id));
@@ -32,6 +37,75 @@ class IcingaNotificationTable extends IcingaObjectTable
         );
     }
 
+    protected function renderRow($row)
+    {
+        $v = $this->view();
+        $extra = $this->appliedOnes($row->id);
+        $htm = "  <tr" . $this->getRowClassesString($row) . ">\n";
+        $htm .= '<td>' . $v->qlink($row->notification, $this->getActionUrl($row));
+        if (empty($extra)) {
+            $htm .= ' ' . $v->qlink(
+                'Create apply-rule',
+                'director/notification/add',
+                array('apply' => $row->notification),
+                array('class'    => 'icon-plus')
+            );
+
+        } else {
+            $htm .= '. Related apply rules: <ul class="apply-rules">';
+            foreach ($extra as $id => $notification) {
+                $htm .= '<li>'
+                    . $v->qlink($notification, 'director/notification', array('id' => $id))
+                    . '</li>';
+            }
+            $htm .= '</ul>';
+            $htm .= $v->qlink(
+                'Add more',
+                'director/notification/add',
+                array('apply' => $row->notification),
+                array('class' => 'icon-plus')
+            );
+        }
+        $htm .= '</td>';
+        return $htm . "  </tr>\n";
+    }
+
+    protected function appliedOnes($id)
+    {
+        if ($this->connection()->isPgsql()) {
+            $nameCol = "s.object_name || COALESCE(': ' || ARRAY_TO_STRING(ARRAY_AGG("
+                . "a.assign_type || ' where ' || a.filter_string"
+                . " ORDER BY a.assign_type, a.filter_string), ', '), '')";
+        } else {
+            $nameCol = "s.object_name || COALESCE(': ' || GROUP_CONCAT("
+                . "a.assign_type || ' where ' || a.filter_string"
+                . " ORDER BY a.assign_type, a.filter_string SEPARATOR ', '"
+                . "), '')";
+        }
+
+        $db = $this->connection()->getConnection();
+        $query = $db->select()->from(
+            array('s' => 'icinga_notification'),
+            array(
+                'id'         => 's.id',
+                'objectname' => $nameCol,
+            )
+        )->join(
+            array('i' => 'icinga_notification_inheritance'),
+            'i.notification_id = s.id',
+            array()
+        )->where('i.parent_notification_id = ?', $id)
+         ->where('s.object_type = ?', 'apply');
+
+        $query->joinLeft(
+            array('a' => 'icinga_notification_assignment'),
+            'a.notification_id = s.id',
+            array()
+        )->group('s.id');
+
+        return $db->fetchPairs($query);
+    }
+
     public function getUnfilteredQuery()
     {
         $db = $this->connection()->getConnection();



More information about the icinga-checkins mailing list