[icinga-checkins] icinga.org: icingaweb2-module-director/feature/service-set-12891: Servicesset: add controller, form and table

git at icinga.org git at icinga.org
Thu Oct 20 09:26:58 CEST 2016


Module: icingaweb2-module-director
Branch: feature/service-set-12891
Commit: 6a3502ea8c1445bf55240b387f2bfe9a20f31cd2
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=6a3502ea8c1445bf55240b387f2bfe9a20f31cd2

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Oct 13 09:13:21 2016 +0000

Servicesset: add controller, form and table

---

 application/controllers/ServicesetController.php   |   42 ++++++++++++
 application/forms/IcingaServiceSetForm.php         |   71 ++++++++++++++++++++
 application/tables/IcingaServiceSetTable.php       |   62 +++++++++++++++++
 library/Director/Objects/IcingaObject.php          |    2 +
 .../Director/Web/Controller/ObjectController.php   |    7 +-
 .../Director/Web/Controller/ObjectsController.php  |   28 ++++++++
 6 files changed, 208 insertions(+), 4 deletions(-)

diff --git a/application/controllers/ServicesetController.php b/application/controllers/ServicesetController.php
new file mode 100644
index 0000000..2f230f1
--- /dev/null
+++ b/application/controllers/ServicesetController.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Icinga\Module\Director\Controllers;
+
+use Icinga\Module\Director\Objects\IcingaHost;
+use Icinga\Module\Director\Objects\IcingaServiceSet;
+use Icinga\Module\Director\Web\Controller\ObjectController;
+
+class ServicesetController extends ObjectController
+{
+    protected $host;
+
+    public function init()
+    {
+        if ($host = $this->params->get('host')) { 
+            $this->host = IcingaHost::load($host, $this->db());
+        }
+
+        parent::init();
+    }
+
+    protected function loadObject()
+    {
+        if ($this->object === null) {
+            if ($name = $this->params->get('name')) {
+                $params = array('object_name' => $name);
+                $db = $this->db();
+
+                if ($this->host) {
+                    $this->view->host = $this->host;
+                    $params['host_id'] = $this->host->id;
+                }
+
+                $this->object = IcingaServiceSet::load($params, $db);
+            } else {
+                parent::loadObject();
+            }
+        }
+
+        return $this->object;
+    }
+}
diff --git a/application/forms/IcingaServiceSetForm.php b/application/forms/IcingaServiceSetForm.php
new file mode 100644
index 0000000..a843de1
--- /dev/null
+++ b/application/forms/IcingaServiceSetForm.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Icinga\Module\Director\Forms;
+
+use Icinga\Module\Director\Object\IcingaHost;
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+
+class IcingaServiceSetForm extends DirectorObjectForm
+{
+    protected $host;
+
+    public function setup()
+    {
+        $this->addImportsElement();
+
+        $this->addElement('text', 'object_name', array(
+            'label'       => $this->translate('Service set name'),
+            'description' => $this->translate(
+                'A short name identifying this set of ser'
+            ),
+            'required'    => true,
+        ));
+        
+        $this->addElement('textarea', 'description', array(
+            'label'       => $this->translate('Description'),
+            'description' => $this->translate(
+                'A meaningful description explaining your users what to expect'
+                . ' when assigning this set of services'
+            ),
+            'rows'        => '3',
+            'required'    => ! $this->isTemplate(),
+        ));
+
+
+        if ($this->host === null) {
+            $this->addHidden('object_type', 'object');
+
+            $this->addElement('multiselect', 'service', array(
+                'label'        => $this->translate('Services'),
+                'description'  => $this->translate(
+                    'Services in this set'
+                ),
+                'rows'         => '5',
+                'multiOptions' => $this->enumServices(),
+                'required'     => true,
+            ));
+        } else {
+            $this->addHidden('object_type', 'object');
+            $this->addHidden('host_id', $this->host->id);
+        }
+
+        $this->setButtons();
+    }
+
+    public function setHost(IcingaHost $host)
+    {
+        $this->host = $host;
+        return $this;
+    }
+
+    protected function enumServices()
+    {
+        $db = $this->db->getDbAdapter();
+        $query = $db->select()
+            ->from('icinga_service', 'object_name')
+            ->where('object_type = ?', 'template');
+        $names = $db->fetchCol($query);
+
+        return array_combine($names, $names);
+    }
+}
diff --git a/application/tables/IcingaServiceSetTable.php b/application/tables/IcingaServiceSetTable.php
new file mode 100644
index 0000000..bd612d5
--- /dev/null
+++ b/application/tables/IcingaServiceSetTable.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Icinga\Module\Director\Tables;
+
+use Icinga\Module\Director\Web\Table\IcingaObjectTable;
+
+class IcingaServiceSetTable extends IcingaObjectTable
+{
+    public function getColumns()
+    {
+        return array(
+            'id'          => 'sset.id',
+            'name'        => 'sset.object_name',
+            'object_type' => 'sset.object_type',
+            'description' => 'sset.description',
+            'host_name'   => 'h.object_name',
+        );
+    }
+
+    public function getTitles()
+    {
+        $view = $this->view();
+        return array(
+            'name'    => $view->translate('Service set'),
+        );
+    }
+
+    protected function getActionUrl($row)
+    {
+        // TODO: Remove once we got a separate apply table
+        if ($row->object_type === 'apply') {
+            $params['id'] = $row->id;
+        } else {
+            $params = array('name' => $row->name);
+            if ($row->host_name) {
+                $params['host'] = $row->host_name;
+            }
+        }
+
+        return $this->url('director/serviceset', $params);
+    }
+
+    protected function getUnfilteredQuery()
+    {
+        $db = $this->connection()->getConnection();
+        $query = $db->select()->from(
+            array('sset' => 'icinga_service_set'),
+            array()
+        )->joinLeft(
+            array('h' => 'icinga_host'),
+            'h.id = sset.host_id',
+            array()
+        )->order('sset.object_name');
+
+        return $query;
+    }
+
+    public function getBaseQuery()
+    {
+        return $this->getUnfilteredQuery();
+    }
+}
diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index 616a1f9..b7ff863 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -2065,6 +2065,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
             $type = 'timePeriod';
         } elseif ($type === 'servicegroup') {
             $type = 'serviceGroup';
+        } elseif ($type === 'service_set') {
+            $type = 'serviceSet';
         } elseif ($type === 'apiuser') {
             $type = 'apiUser';
         }
diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php
index e97bdda..ff157a3 100644
--- a/library/Director/Web/Controller/ObjectController.php
+++ b/library/Director/Web/Controller/ObjectController.php
@@ -26,7 +26,7 @@ abstract class ObjectController extends ActionController
     {
         parent::init();
 
-        $type = $this->getType();
+        $type = strtolower($this->getType());
 
         if ($object = $this->loadObject()) {
             $this->beforeTabs();
@@ -61,7 +61,6 @@ abstract class ObjectController extends ActionController
                     'label'     => $this->translate('Fields')
                 ));
             }
-
         } else {
             $this->beforeTabs();
             $this->getTabs()->add('add', array(
@@ -301,8 +300,8 @@ abstract class ObjectController extends ActionController
     {
         // Strip final 's' and upcase an eventual 'group'
         return preg_replace(
-            array('/group$/', '/period$/', '/argument$/', '/apiuser$/'),
-            array('Group', 'Period', 'Argument', 'ApiUser'),
+            array('/group$/', '/period$/', '/argument$/', '/apiuser$/', '/set$/'),
+            array('Group', 'Period', 'Argument', 'ApiUser', 'Set'),
             $this->getRequest()->getControllerName()
         );
     }
diff --git a/library/Director/Web/Controller/ObjectsController.php b/library/Director/Web/Controller/ObjectsController.php
index 79fbac0..6020c28 100644
--- a/library/Director/Web/Controller/ObjectsController.php
+++ b/library/Director/Web/Controller/ObjectsController.php
@@ -67,6 +67,13 @@ abstract class ObjectsController extends ActionController
             ));
         }
 
+        if ($object->supportsSets() || $object->isGroup() /** Bullshit, need base object, wrong on users */) {
+            $tabs->add('sets', array(
+                'url'       => sprintf('director/%ss/sets', $type),
+                'label'     => $this->translate('Sets')
+            ));
+        }
+
         $tabs->add('tree', array(
             'url'   => sprintf('director/%ss/templatetree', $type),
             'label' => $this->translate('Tree'),
@@ -246,6 +253,27 @@ abstract class ObjectsController extends ActionController
         $this->setViewScript('objects/tree');
     }
 
+    public function setsAction()
+    {
+        $this->view->title = $this->translate('Service sets');
+        $this->view->table = $this
+            ->loadTable('IcingaServiceSet')
+            ->setConnection($this->db());
+
+        $this->view->addLink = $this->view->qlink(
+            $this->translate('Add'),
+            'director/serviceset/add',
+            null,
+            array(
+                'class'            => 'icon-plus',
+                'data-base-target' => '_next'
+            )
+        );
+
+        $this->getTabs()->activate('sets');
+        $this->setViewScript('objects/table');
+    }
+
     protected function dummyObject()
     {
         if ($this->dummy === null) {



More information about the icinga-checkins mailing list