summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/forms/Config/Transport/RemoteTransportForm.php
blob: 7beeacf8a8136441a8fba2eac58f2ff4e20161d3 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Forms\Config\Transport;

use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Web\Form;

class RemoteTransportForm extends Form
{
    /**
     * The available resources split by type
     *
     * @var array
     */
    protected $resources;

    /**
     * (non-PHPDoc)
     * @see Form::init() For the method documentation.
     */
    public function init()
    {
        $this->setName('form_config_command_transport_remote');
    }

    /**
     * Load all available ssh identity resources
     *
     * @return $this
     *
     * @throws \Icinga\Exception\ConfigurationError
     */
    public function loadResources()
    {
        $resourceConfig = ResourceFactory::getResourceConfigs();

        $resources = array();
        foreach ($resourceConfig as $name => $resource) {
            if ($resource->type === 'ssh') {
                $resources['ssh'][$name] = $name;
            }
        }

        if (empty($resources)) {
            throw new ConfigurationError($this->translate('Could not find any valid SSH resources'));
        }

        $this->resources = $resources;

        return $this;
    }

    /**
     * Check whether ssh identity resources exists or not
     *
     * @return boolean
     */
    public function hasResources()
    {
        $resourceConfig = ResourceFactory::getResourceConfigs();

        foreach ($resourceConfig as $name => $resource) {
            if ($resource->type === 'ssh') {
                return true;
            }
        }
        return false;
    }

    /**
     * (non-PHPDoc)
     * @see Form::createElements() For the method documentation.
     */
    public function createElements(array $formData = array())
    {
        $useResource = false;

        if ($this->hasResources()) {
            $useResource = isset($formData['use_resource'])
                ? $formData['use_resource'] : $this->getValue('use_resource');

            $this->addElement(
                'checkbox',
                'use_resource',
                array(
                    'label'         => $this->translate('Use SSH Identity'),
                    'description'   => $this->translate('Make use of the ssh identity resource'),
                    'autosubmit'    => true,
                    'ignore'        => true
                )
            );
        }

        if ($useResource) {
            $this->loadResources();

            $decorators = static::$defaultElementDecorators;
            array_pop($decorators); // Removes the HtmlTag decorator

            $this->addElement(
                'select',
                'resource',
                array(
                    'required'      => true,
                    'label'         => $this->translate('SSH Identity'),
                    'description'   => $this->translate('The resource to use'),
                    'decorators'    => $decorators,
                    'multiOptions'  => $this->resources['ssh'],
                    'value'         => current($this->resources['ssh']),
                    'autosubmit'    => false
                )
            );
            $resourceName = isset($formData['resource']) ? $formData['resource'] : $this->getValue('resource');
            $this->addElement(
                'note',
                'resource_note',
                array(
                    'escape'        => false,
                    'decorators'    => $decorators,
                    'value'         => sprintf(
                        '<a href="%1$s" data-base-target="_next" title="%2$s" aria-label="%2$s">%3$s</a>',
                        $this->getView()->url('config/editresource', array('resource' => $resourceName)),
                        sprintf($this->translate('Show the configuration of the %s resource'), $resourceName),
                        $this->translate('Show resource configuration')
                    )
                )
            );
        }

        $this->addElements(array(
            array(
                'text',
                'host',
                array(
                    'required'      => true,
                    'label'         => $this->translate('Host'),
                    'description'   => $this->translate(
                        'Hostname or address of the remote Icinga instance'
                    )
                )
            ),
            array(
                'number',
                'port',
                array(
                    'required'          => true,
                    'preserveDefault'   => true,
                    'label'             => $this->translate('Port'),
                    'description'       => $this->translate('SSH port to connect to on the remote Icinga instance'),
                    'value'             => 22
                )
            )
        ));

        if (! $useResource) {
            $this->addElement(
                'text',
                'user',
                array(
                    'required'      => true,
                    'label'         => $this->translate('User'),
                    'description'   => $this->translate(
                        'User to log in as on the remote Icinga instance. Please note that key-based SSH login must be'
                        . ' possible for this user'
                    )
                )
            );
        }

        $this->addElement(
            'text',
            'path',
            array(
                'required'      => true,
                'label'         => $this->translate('Command File'),
                'value'         => '/var/run/icinga2/cmd/icinga2.cmd',
                'description'   => $this->translate('Path to the Icinga command file on the remote Icinga instance')
            )
        );

        return $this;
    }
}