From 51916192a939c4d3a064ada9232bb0bf815363e7 Mon Sep 17 00:00:00 2001
From: Thomas Gelf <thomas@gelf.net>
Date: Sun, 6 Mar 2016 14:21:18 +0100
Subject: [PATCH] IcingaService: render assigned to host template

---
 library/Director/Objects/IcingaService.php | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php
index 2b1be431..6592dc0c 100644
--- a/library/Director/Objects/IcingaService.php
+++ b/library/Director/Objects/IcingaService.php
@@ -2,6 +2,8 @@
 
 namespace Icinga\Module\Director\Objects;
 
+use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
+
 class IcingaService extends IcingaObject
 {
     protected $table = 'icinga_service';
@@ -97,6 +99,45 @@ class IcingaService extends IcingaObject
         return $this->renderRelationProperty('host', $this->host_id, 'host_name');
     }
 
+    protected function renderObjectHeader()
+    {
+        if ($this->hasBeenAssignedToHostTemplate()) {
+
+            return sprintf(
+                "%s %s %s {\n",
+                'apply',
+                $this->getType(),
+                c::renderString($this->getObjectName())
+            );
+
+        } else {
+            return parent::renderObjectHeader();
+        }
+    }
+
+    protected function renderAssignments()
+    {
+        if (! $this->hasBeenAssignedToHostTemplate()) {
+            return parent::renderAssignments();
+        }
+
+        // TODO: use assignment renderer, escape host
+        $filter = sprintf(
+            'assign where "%s" in host.templates',
+            $this->host
+        );
+
+        return "\n    " . $filter . "\n";
+    }
+
+    protected function hasBeenAssignedToHostTemplate()
+    {
+        return $this->host_id && $this->getRelatedObject(
+            'host',
+            $this->host_id
+        )->object_type === 'template';
+    }
+
     protected function renderCustomExtensions()
     {
         if ($this->command_endpoint_id !== null