summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaZone.php
blob: 8d77e478731ad4bfd8dce70cfd5b43df64623b33 (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
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;

class IcingaZone extends IcingaObject
{
    protected $table = 'icinga_zone';

    protected $uuidColumn = 'uuid';

    protected $defaultProperties = [
        'id'          => null,
        'uuid'        => null,
        'object_name' => null,
        'object_type' => null,
        'disabled'    => 'n',
        'parent_id'   => null,
        'is_global'   => 'n',
    ];

    protected $booleans = [
        // Global is a reserved word in SQL, column name was prefixed
        'is_global' => 'global'
    ];

    protected $relations = [
        'parent' => 'IcingaZone',
    ];

    protected $supportsImports = true;

    protected static $globalZoneNames;

    private $endpointList;

    protected function renderCustomExtensions()
    {
        $endpoints = $this->listEndpoints();
        if (empty($endpoints)) {
            return '';
        }

        return c::renderKeyValue('endpoints', c::renderArray($endpoints));
    }

    public function isGlobal()
    {
        return $this->get('is_global') === 'y';
    }

    public static function zoneNameIsGlobal($name, Db $connection)
    {
        if (self::$globalZoneNames === null) {
            $db = $connection->getDbAdapter();
            self::setCachedGlobalZoneNames($db->fetchCol(
                $db->select()->from('icinga_zone', 'object_name')->where('is_global = ?', 'y')
            ));
        }

        return \in_array($name, self::$globalZoneNames);
    }

    public static function setCachedGlobalZoneNames($names)
    {
        self::$globalZoneNames = $names;
    }

    public function getRenderingZone(IcingaConfig $config = null)
    {
        // If the zone has a parent zone...
        if ($this->get('parent_id')) {
            // ...we render the zone object to the parent zone
            return $this->get('parent');
        } elseif ($this->get('is_global') === 'y') {
            // ...additional global zones are rendered to our global zone...
            return $this->connection->getDefaultGlobalZoneName();
        } else {
            // ...and all the other zones are rendered to our master zone
            return $this->connection->getMasterZoneName();
        }
    }

    public function setEndpointList($list)
    {
        $this->endpointList = $list;

        return $this;
    }

    // TODO: Move this away, should be prefetchable:
    public function listEndpoints()
    {
        $id = $this->get('id');
        if ($id && $this->endpointList === null) {
            $db = $this->getDb();
            $query = $db->select()
                ->from('icinga_endpoint', 'object_name')
                ->where('zone_id = ?', $id)
                ->order('object_name');

            $this->endpointList = $db->fetchCol($query);
        }

        return $this->endpointList;
    }
}