summaryrefslogtreecommitdiffstats
path: root/library/Director/Import/ImportSourceDirectorObject.php
blob: e3f56fc2029392bcb319dfe22cec1b205bf5032a (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
<?php

namespace Icinga\Module\Director\Import;

use Icinga\Application\Config;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Forms\ImportSourceForm;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Form\QuickForm;

class ImportSourceDirectorObject extends ImportSourceHook
{
    protected $db;

    public function getName()
    {
        return 'Director Objects';
    }

    public static function getDefaultKeyColumnName()
    {
        return 'object_name';
    }

    public function fetchData()
    {
        $db = $this->db();
        $objectClass = $this->getSetting('object_class');
        $objectType = $this->getSetting('object_type');
        /** @var IcingaObject $class fake type hint, it's a string */
        $class = DbObjectTypeRegistry::classByType($objectClass);
        if ($objectType) {
            $dummy = $class::create();
            $query = $db->getDbAdapter()->select()
                ->from($dummy->getTableName())
                ->where('object_type = ?', $objectType);
        } else {
            $query = null;
        }
        $result = [];
        $resolved = $this->getSetting('resolved') === 'y';
        foreach ($class::loadAllByType($objectClass, $db, $query) as $object) {
            $result[] = $object->toPlainObject($resolved);
        }
        if ($objectClass === 'zone') {
            $this->enrichZonesWithDeploymentZone($result);
        }
        return $result;
    }

    protected function enrichZonesWithDeploymentZone(&$zones)
    {
        $masterZone = $this->db()->getMasterZoneName();
        foreach ($zones as $zone) {
            $zone->is_master_zone = $zone->object_name === $masterZone;
        }
    }

    public static function addSettingsFormFields(QuickForm $form)
    {
        /** @var ImportSourceForm $form */
        Util::addDbResourceFormElement($form, 'resource');
        $form->getElement('resource')
            ->setValue(Config::module('director')->get('db', 'resource'));
        $form->addElement('select', 'object_class', [
            'label'    => $form->translate('Director Object'),
            'multiOptions' => [
                'host'     => $form->translate('Host'),
                'endpoint' => $form->translate('Endpoint'),
                'zone'     => $form->translate('Zone'),
            ],
            'required' => true,
        ]);
        $form->addElement('select', 'object_type', [
            'label'    => $form->translate('Object Type'),
            'multiOptions' => [
                null              => $form->translate('All Object Types'),
                'object'          => $form->translate('Objects'),
                'template'        => $form->translate('Templates'),
                'external_object' => $form->translate('External Objects'),
                'apply'           => $form->translate('Apply Rules'),
            ],
        ]);

        /** @var $form \Icinga\Module\Director\Web\Form\DirectorObjectForm */
        $form->addBoolean('resolved', [
            'label' => $form->translate('Resolved'),
        ], 'n');

        return $form;
    }

    protected function db()
    {
        if ($this->db === null) {
            $this->db = Db::fromResourceName($this->settings['resource']);
        }

        return $this->db;
    }

    public function listColumns()
    {
        $rows = $this->fetchData();
        $columns = [];

        foreach ($rows as $object) {
            foreach (array_keys((array) $object) as $column) {
                if (! isset($columns[$column])) {
                    $columns[] = $column;
                }
            }
        }

        return $columns;
    }
}