summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/Branch/UuidLookup.php
blob: 4db7866b0146debb0725c70f78c2227419a264b3 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

namespace Icinga\Module\Director\Db\Branch;

use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function is_int;
use function is_resource;
use function is_string;

class UuidLookup
{
    /**
     * @param int|string $key
     * @return ?UuidInterface
     */
    public static function findServiceUuid(
        Db $connection,
        Branch $branch,
        ?string $objectType = null,
        $key = null,
        IcingaHost $host = null,
        IcingaServiceSet $set = null
    ) {
        $db = $connection->getDbAdapter();
        $query = $db->select()->from('icinga_service', 'uuid');
        if ($objectType) {
            $query->where('object_type = ?', $objectType);
        }
        $query = self::addKeyToQuery($connection, $query, $key);
        if ($set) {
            $setId = $set->get('id');
            if ($setId === null) {
                $query->where('1 = 0');
            } else {
                $query->where('service_set_id = ?', $setId);
            }
        } elseif ($host) {
            $hostId = $host->get('id');
            if ($hostId === null) {
                $query->where('1 = 0');
            } else {
                $query->where('host_id = ?', $hostId);
            }
        }
        $uuid = self::fetchOptionalUuid($connection, $query);

        if ($uuid === null && $branch->isBranch()) {
            // TODO: use different tables?
            $query = $db->select()
                ->from('branched_icinga_service', 'uuid')
                ->where('branch_uuid = ?', $connection->quoteBinary($branch->getUuid()->getBytes()));
            if ($objectType) {
                $query->where('object_type = ?', $objectType);
            }
            $query = self::addKeyToQuery($connection, $query, $key);
            if ($host) {
                // TODO: uuid?
                $query->where('host = ?', $host->getObjectName());
            }
            if ($set) {
                $query->where('service_set = ?', $set->getObjectName());
            }

            $uuid = self::fetchOptionalUuid($connection, $query);
        }

        return $uuid;
    }

    /**
     * @param int|string|array $key
     * @param string $table
     * @param Db $connection
     * @param Branch $branch
     * @return UuidInterface
     * @throws NotFoundError
     */
    public static function requireUuidForKey($key, $table, Db $connection, Branch $branch)
    {
        $uuid = self::findUuidForKey($key, $table, $connection, $branch);
        if ($uuid === null) {
            throw new NotFoundError('No such object available');
        }

        return $uuid;
    }

    /**
     * @param int|string|array $key
     * @param string $table
     * @param Db $connection
     * @param Branch $branch
     * @return ?UuidInterface
     */
    public static function findUuidForKey($key, $table, Db $connection, Branch $branch)
    {
        $db = $connection->getDbAdapter();
        $query = self::addKeyToQuery($connection, $db->select()->from($table, 'uuid'), $key);
        $uuid = self::fetchOptionalUuid($connection, $query);
        if ($uuid === null && $branch->isBranch()) {
            if (is_array($key) && isset($key['host_id'])) {
                $key['host'] = IcingaHost::loadWithAutoIncId((int) $key['host_id'], $connection)->getObjectName();
                unset($key['host_id']);
            }
            $query = self::addKeyToQuery($connection, $db->select()->from("branched_$table", 'uuid'), $key);
            $query->where('branch_uuid = ?', $connection->quoteBinary($branch->getUuid()->getBytes()));
            $uuid = self::fetchOptionalUuid($connection, $query);
        }

        return $uuid;
    }

    protected static function addKeyToQuery(Db $connection, $query, $key)
    {
        if (is_int($key)) {
            $query->where('id = ?', $key);
        } elseif (is_string($key)) {
            $query->where('object_name = ?', $key);
        } else {
            foreach ($key as $k => $v) {
                $query->where($connection->getDbAdapter()->quoteIdentifier($k) . ' = ?', $v);
            }
        }

        return $query;
    }

    protected static function fetchOptionalUuid(Db $connection, $query)
    {
        $result = $connection->getDbAdapter()->fetchOne($query);
        if (is_resource($result)) {
            $result = stream_get_contents($result);
        }
        if (is_string($result)) {
            return Uuid::fromBytes($result);
        }

        return null;
    }
}