summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Lookup/AppliedServiceInfo.php
blob: abda497ea1827bad7d41d2d29049bdbc321a4b3a (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
<?php

namespace Icinga\Module\Director\DirectorObject\Lookup;

use gipfl\IcingaWeb2\Url;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\HostApplyMatches;
use Icinga\Module\Director\Objects\IcingaHost;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

/**
 * A Service Apply Rule matching this Host, generating a Service with the given
 * name
 */
class AppliedServiceInfo implements ServiceInfo
{
    /** @var string */
    protected $hostName;

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

    /** @var int */
    protected $serviceApplyRuleId;

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

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

    public static function find(IcingaHost $host, $serviceName)
    {
        $matcher = HostApplyMatches::prepare($host);
        $connection = $host->getConnection();
        foreach (static::fetchApplyRulesByServiceName($connection, $serviceName) as $rule) {
            if ($matcher->matchesFilter($rule->filter)) {
                return new static($host->getObjectName(), $serviceName, (int) $rule->id, $rule->uuid);
            }
        }

        return null;
    }

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

    /**
     * @return int
     */
    public function getServiceApplyRuleId()
    {
        return $this->serviceApplyRuleId;
    }

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

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

    public function getUrl()
    {
        return Url::fromPath('director/host/appliedservice', [
            'name'       => $this->hostName,
            'service_id' => $this->serviceApplyRuleId,
        ]);
    }

    public function requiresOverrides()
    {
        return true;
    }

    protected static function fetchApplyRulesByServiceName(Db $connection, $serviceName)
    {
        $db = $connection->getDbAdapter();
        $query = $db->select()
            ->from(['s' => 'icinga_service'], [
                'id'            => 's.id',
                'uuid'          => 's.uuid',
                'name'          => 's.object_name',
                'assign_filter' => 's.assign_filter',
            ])
            ->where('object_name = ?', $serviceName)
            ->where('object_type = ? AND assign_filter IS NOT NULL', 'apply');

        $allRules = $db->fetchAll($query);
        foreach ($allRules as $rule) {
            $rule->uuid = Uuid::fromBytes(Db\DbUtil::binaryResult($rule->uuid));
            $rule->filter = Filter::fromQueryString($rule->assign_filter);
        }

        return $allRules;
    }
}