summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Lookup/ServiceSetServiceInfo.php
blob: a980da88740b861312796906b7e96d412663ab47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace Icinga\Module\Director\DirectorObject\Lookup;

use gipfl\IcingaWeb2\Url;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Repository\IcingaTemplateRepository;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

/**
 * A service belonging to a Service Set, attached either directly to the given
 * Host or to one of it's inherited Host Templates
 */
class ServiceSetServiceInfo implements ServiceInfo
{
    /** @var string */
    protected $hostName;

    /** @var string */
    protected $serviceName;

    /** @var string */
    protected $serviceSetName;

    /** @var UuidInterface */
    protected $uuid;

    public function __construct($hostName, $serviceName, $serviceSetName, UuidInterface $uuid)
    {
        $this->hostName = $hostName;
        $this->serviceName = $serviceName;
        $this->serviceSetName = $serviceSetName;
        $this->uuid = $uuid;
    }

    public static function find(IcingaHost $host, $serviceName)
    {
        $ids = [$host->get('id')];

        foreach (IcingaTemplateRepository::instanceByObject($host)->getTemplatesFor($host, true) as $parent) {
            $ids[] = $parent->get('id');
        }

        $db = $host->getConnection()->getDbAdapter();
        $query = $db->select()
            ->from(
                ['s' => 'icinga_service'],
                [
                    'service_set_name' => 'ss.object_name',
                    'uuid'             => 's.uuid',
                ]
            )->join(
                ['ss' => 'icinga_service_set'],
                's.service_set_id = ss.id',
                []
            )->join(
                ['hsi' => 'icinga_service_set_inheritance'],
                'hsi.parent_service_set_id = ss.id',
                []
            )->join(
                ['hs' => 'icinga_service_set'],
                'hs.id = hsi.service_set_id',
                []
            )->where('hs.host_id IN (?)', $ids)
            ->where('s.object_name = ?', $serviceName)
            ->where( // Ignore deactivated Services:
                'NOT EXISTS (SELECT 1 FROM icinga_host_service_blacklist hsb'
                . ' WHERE hsb.host_id = ? AND hsb.service_id = s.id)',
                (int) $host->get('id')
            );

        if ($row = $db->fetchRow($query)) {
            return new static(
                $host->getObjectName(),
                $serviceName,
                $row->service_set_name,
                Uuid::fromBytes($row->uuid)
            );
        }

        return null;
    }

    public function getHostName()
    {
        return $this->hostName;
    }

    public function getName()
    {
        return $this->serviceName;
    }

    public function getUuid()
    {
        return $this->uuid;
    }

    /**
     * @return string
     */
    public function getServiceSetName()
    {
        return $this->serviceSetName;
    }

    public function getUrl()
    {
        return Url::fromPath('director/host/servicesetservice', [
            'name'    => $this->hostName,
            'service' => $this->serviceName,
            'set'     => $this->serviceSetName,
        ]);
    }

    public function requiresOverrides()
    {
        return true;
    }
}