summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/IcingaObjectFilterHelper.php
blob: 2d9f8f37f09fa43af27ac5dea1905e0118744690 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace Icinga\Module\Director\Db;

use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree;
use InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use Zend_Db_Select as ZfSelect;

class IcingaObjectFilterHelper
{
    const INHERIT_DIRECT = 'direct';
    const INHERIT_INDIRECT = 'indirect';
    const INHERIT_DIRECT_OR_INDIRECT = 'total';

    /**
     * @param IcingaObject|int|string $id
     * @return int
     */
    public static function wantId($id)
    {
        if (is_int($id)) {
            return $id;
        } elseif ($id instanceof IcingaObject) {
            return (int) $id->get('id');
        } elseif (is_string($id) && ctype_digit($id)) {
            return (int) $id;
        } else {
            throw new InvalidArgumentException(sprintf(
                'Numeric ID or IcingaObject expected, got %s',
                // TODO: just type/class info?
                var_export($id, true)
            ));
        }
    }

    /**
     * @param ZfSelect $query
     * @param IcingaObject|int|string $template
     * @param string $tableAlias
     * @param string $inheritanceType
     * @return ZfSelect
     */
    public static function filterByTemplate(
        ZfSelect $query,
        $template,
        $tableAlias = 'o',
        $inheritanceType = self::INHERIT_DIRECT,
        UuidInterface $branchuuid = null
    ) {
        $i = $tableAlias . 'i';
        $o = $tableAlias;
        $type = $template->getShortTableName();
        $db = $template->getDb();
        $id = static::wantId($template);

        if ($branchuuid) {
            if ($inheritanceType === self::INHERIT_DIRECT) {
                return $query->where('imports LIKE \'%"' . $template->getObjectName() . '"%\'');
            } elseif ($inheritanceType === self::INHERIT_INDIRECT
                || $inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT
            ) {
                $tree = new TemplateTree($type, $template->getConnection());
                $templateNames = $tree->getDescendantsFor($template);

                if ($inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT) {
                    $templateNames[] = $template->getObjectName();
                }

                if (empty($templateNames)) {
                    $condition = '(1 = 0)';
                } else {
                    $condition = 'imports LIKE \'%"' . array_pop($templateNames) . '"%\'';

                    foreach ($templateNames as $templateName) {
                        $condition .= " OR imports LIKE '%\"$templateName\"%'";
                    }
                }

                return $query->where($condition);
            }
        }

        $sub = $db->select()->from(
            array($i => "icinga_{$type}_inheritance"),
            array('e' => '(1)')
        )->where("$i.{$type}_id = $o.id");

        if ($inheritanceType === self::INHERIT_DIRECT) {
            $sub->where("$i.parent_{$type}_id = ?", $id);
        } elseif ($inheritanceType === self::INHERIT_INDIRECT
            || $inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT
        ) {
            $tree = new TemplateTree($type, $template->getConnection());
            $ids = $tree->listDescendantIdsFor($template);
            if ($inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT) {
                $ids[] = $template->getAutoincId();
            }

            if (empty($ids)) {
                $sub->where('(1 = 0)');
            } else {
                $sub->where("$i.parent_{$type}_id IN (?)", $ids);
            }
        } else {
            throw new RuntimeException(sprintf(
                'Unable to understand "%s" inheritance',
                $inheritanceType
            ));
        }

        return $query->where('EXISTS ?', $sub);
    }

    public static function filterByHostgroups(
        ZfSelect $query,
        $type,
        $groups,
        $tableAlias = 'o'
    ) {
        if (empty($groups)) {
            // Asked for an empty set of groups? Give no result
            $query->where('(1 = 0)');
        } else {
            $sub = $query->getAdapter()->select()->from(
                array('go' => "icinga_{$type}group_{$type}"),
                array('e' => '(1)')
            )->join(
                array('g' => "icinga_{$type}group"),
                "go.{$type}group_id = g.id"
            )->where("go.{$type}_id = {$tableAlias}.id")
                ->where('g.object_name IN (?)', $groups);

            $query->where('EXISTS ?', $sub);
        }
    }

    public static function filterByResolvedHostgroups(
        ZfSelect $query,
        $type,
        $groups,
        $tableAlias = 'o'
    ) {
        if (empty($groups)) {
            // Asked for an empty set of groups? Give no result
            $query->where('(1 = 0)');
        } else {
            $sub = $query->getAdapter()->select()->from(
                array('go' => "icinga_{$type}group_{$type}_resolved"),
                array('e' => '(1)')
            )->join(
                array('g' => "icinga_{$type}group"),
                "go.{$type}group_id = g.id",
                []
            )->where("go.{$type}_id = {$tableAlias}.id")
                ->where('g.object_name IN (?)', $groups);

            $query->where('EXISTS ?', $sub);
        }
    }
}