[icinga-checkins] icinga.org: icingaweb2-module-director/master: host/services: show inherited services...

git at icinga.org git at icinga.org
Tue Aug 23 16:31:37 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 56a7cb285d07e642fdf48a9051366d466a0440f4
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=56a7cb285d07e642fdf48a9051366d466a0440f4

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Aug 23 14:18:54 2016 +0000

host/services: show inherited services...

...and magically applied ones

---

 application/controllers/HostController.php    |  108 ++++++++++++++++++++++++-
 application/views/scripts/host/services.phtml |   14 ++--
 library/Director/Objects/IcingaObject.php     |    6 ++
 3 files changed, 121 insertions(+), 7 deletions(-)

diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php
index d9c1a76..6f1036d 100644
--- a/application/controllers/HostController.php
+++ b/application/controllers/HostController.php
@@ -6,6 +6,7 @@ use Exception;
 use Icinga\Exception\NotFoundError;
 use Icinga\Module\Director\IcingaConfig\AgentWizard;
 use Icinga\Module\Director\Objects\IcingaEndpoint;
+use Icinga\Module\Director\Objects\IcingaService;
 use Icinga\Module\Director\Objects\IcingaZone;
 use Icinga\Module\Director\Util;
 use Icinga\Module\Director\Web\Controller\ObjectController;
@@ -54,6 +55,7 @@ class HostController extends ObjectController
 
     public function servicesAction()
     {
+        $db = $this->db();
         $host = $this->object;
 
         $this->view->addLink = $this->view->qlink(
@@ -68,10 +70,112 @@ class HostController extends ObjectController
             $this->translate('Services: %s'),
             $host->object_name
         );
-        $this->view->table = $this->loadTable('IcingaHostService')
+
+        $resolver = $this->object->templateResolver();
+
+        $tables = array();
+        $table = $this->loadTable('IcingaHostService')
             ->setHost($host)
+            ->setTitle($this->translate('Individual Service objects'))
             ->enforceFilter('host_id', $host->id)
-            ->setConnection($this->db());
+            ->setConnection($db);
+
+        if (count($table)) {
+            $tables[0] = $table;
+        }
+
+        if ($applied = $host->vars()->get($db->getSetting(
+            'magic_apply_for',
+            '_director_apply_for'
+        ))) {
+            $table = $this->loadTable('IcingaHostAppliedForService')
+                ->setHost($host)
+                ->setDictionary($applied)
+                ->setTitle($this->translate('Generated from host vars'));
+
+            if (count($table)) {
+                $tables[1] = $table;
+            }
+        }
+
+        foreach ($resolver->fetchResolvedParents() as $parent) {
+            $table = $this->loadTable('IcingaHostService')
+                ->setHost($parent)
+                ->enforceFilter('host_id', $parent->id)
+                ->setConnection($db);
+            if (! count($table)) {
+                continue;
+            }
+
+            // dup dup
+            $title = sprintf(
+                'Inherited from %s',
+                $parent->object_name
+            );
+
+            $tables[$title] = $table->setTitle($title);
+        }
+
+        $this->view->tables = $tables;
+    }
+
+    public function appliedserviceAction()
+    {
+        $this->forbiddenWithApiKey();
+        $db = $this->db();
+        $host = $this->object;
+        $serviceName = $this->params->get('service');
+
+        $applied = $host->vars()->get($db->getSetting(
+            'magic_apply_for',
+            '_director_apply_for'
+        ));
+
+        $props = $applied->{$serviceName};
+
+        $parent = IcingaService::create(array(
+            'object_type' => 'template',
+            'object_name' => $this->translate('Host'),
+            'vars'        => $props->vars->getValue(),
+        ), $db);
+
+
+        $service = IcingaService::create(array(
+            'object_type' => 'apply',
+            'object_name' => $serviceName,
+            'host_id'     => $host->id,
+        ), $db);
+
+        if ($templates = $props->templates->getValue()) {
+            $imports = $templates;
+        } else {
+            $imports = $serviceName;
+        }
+
+        if (! is_array($imports)) {
+            $imports = array($imports);
+        }
+
+        // TODO: Validation for $imports? They might not exist!
+        array_unshift($imports, $parent);
+        $service->imports = $imports;
+
+        $this->view->title = sprintf(
+            $this->translate('Applied service: %s'),
+            $serviceName
+        );
+
+        $this->getTabs()->activate('services');
+
+        $this->view->form = $this->loadForm('IcingaService')
+            ->setDb($db)
+            ->setHost($host)
+            ->setHostGenerated()
+            ->setObject($service)
+            ->handleRequest()
+            ;
+
+        $this->setViewScript('object/form');
     }
 
     public function agentAction()
diff --git a/application/views/scripts/host/services.phtml b/application/views/scripts/host/services.phtml
index 2109b7b..c5ece5c 100644
--- a/application/views/scripts/host/services.phtml
+++ b/application/views/scripts/host/services.phtml
@@ -4,15 +4,19 @@
 <span class="action-links">
 <?= $this->addLink ?>
 </span>
-<?php if (count($table) || ($this->filterEditor && ! $this->filterEditor->getFilter()->isEmpty())): ?>
-<?= $this->filterEditor ?>
-<?php endif ?>
-<?= $this->table->getPaginator() ?>
 </div>
 
 <div class="content">
 <?= $this->form ?>
+<?php foreach ($this->tables as $key => $table): ?>
 <?php if (count($table)): ?>
-<?= $this->table->render() ?>
+<?php if ($key === 0 || $key === 1): ?>
+<?= $table->render() ?>
+<?php else: ?>
+<div data-base-target="_next">
+<?= $table->render() ?>
+</div>
+<?php endif ?>
 <?php endif ?>
+<?php endforeach ?>
 </div>
diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index e92ef9e..37685af 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -591,6 +591,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         return $this->imports()->listImportNames();
     }
 
+    public function templateResolver()
+    {
+        // preserve object
+        return new IcingaTemplateResolver($this);
+    }
+
     public function getResolvedProperty($key, $default = null)
     {
         if (array_key_exists($key, $this->unresolvedRelatedProperties)) {



More information about the icinga-checkins mailing list