summaryrefslogtreecommitdiffstats
path: root/application/controllers/HostsController.php
blob: 03320729e2e9a2db37df295e7d74bbe02932fd5c (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
<?php

namespace Icinga\Module\Director\Controllers;

use gipfl\IcingaWeb2\Url;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Forms\IcingaAddServiceForm;
use Icinga\Module\Director\Forms\IcingaAddServiceSetForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Controller\ObjectsController;
use gipfl\IcingaWeb2\Link;

class HostsController extends ObjectsController
{
    protected $multiEdit = array(
        'imports',
        'groups',
        'disabled'
    );

    protected function checkDirectorPermissions()
    {
        $this->assertPermission('director/hosts');
    }

    public function editAction()
    {
        $url = clone($this->getRequest()->getUrl());
        $url->setPath('director/hosts/addservice');

        $urlSet = clone($url);
        $urlSet->setPath('director/hosts/addserviceset');

        parent::editAction();

        $this->actions()->add(Link::create(
            $this->translate('Add Service'),
            $url,
            null,
            ['class' => 'icon-plus']
        ))->add(Link::create(
            $this->translate('Add Service Set'),
            $urlSet,
            null,
            ['class' => 'icon-plus']
        ));
    }

    public function edittemplatesAction()
    {
        parent::editAction();

        $objects = $this->loadMultiObjectsFromParams();
        $names = [];
        /** @var ExportInterface $object */
        foreach ($objects as $object) {
            $names[] = $object->getUniqueIdentifier();
        }

        $url = Url::fromPath('director/basket/add', [
            'type'  => 'HostTemplate',
        ]);

        $url->getParams()->addValues('names', $names);

        $this->actions()->add(Link::create(
            $this->translate('Add to Basket'),
            $url,
            null,
            ['class' => 'icon-tag']
        ));
    }

    public function addserviceAction()
    {
        $this->addSingleTab($this->translate('Add Service'));
        $filter = Filter::fromQueryString($this->params->toString());

        $objects = array();
        $db = $this->db();
        /** @var $filter FilterChain */
        foreach ($filter->filters() as $sub) {
            /** @var $sub FilterChain */
            foreach ($sub->filters() as $ex) {
                /** @var $ex FilterChain|FilterExpression */
                if ($ex->isExpression() && $ex->getColumn() === 'name') {
                    $name = $ex->getExpression();
                    $objects[$name] = IcingaHost::load($name, $db);
                }
            }
        }
        $this->addTitle(
            $this->translate('Add service to %d hosts'),
            count($objects)
        );

        $this->content()->add(
            IcingaAddServiceForm::load()
                ->setHosts($objects)
                ->setDb($this->db())
                ->handleRequest()
        );
    }

    public function addservicesetAction()
    {
        $this->addSingleTab($this->translate('Add Service Set'));
        $filter = Filter::fromQueryString($this->params->toString());

        $objects = array();
        $db = $this->db();
        /** @var $filter FilterChain */
        foreach ($filter->filters() as $sub) {
            /** @var $sub FilterChain */
            foreach ($sub->filters() as $ex) {
                /** @var $ex FilterChain|FilterExpression */
                if ($ex->isExpression() && $ex->getColumn() === 'name') {
                    $name = $ex->getExpression();
                    $objects[$name] = IcingaHost::load($name, $db);
                }
            }
        }
        $this->addTitle(
            $this->translate('Add Service Set to %d hosts'),
            count($objects)
        );

        $this->content()->add(
            IcingaAddServiceSetForm::load()
                ->setHosts($objects)
                ->setDb($this->db())
                ->handleRequest()
        );
    }
}