summaryrefslogtreecommitdiffstats
path: root/application/forms/IcingaDependencyForm.php
blob: 56597f91ae3deffdabe2c89c2e6c5a34e6faf452 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaDependency;
use Zend_Validate_Callback;

class IcingaDependencyForm extends DirectorObjectForm
{
    /**
     * @throws \Zend_Form_Exception
     */
    public function setup()
    {
        $this->setupDependencyElements();
    }

    /***
     * @throws \Zend_Form_Exception
     */
    protected function setupDependencyElements()
    {
        $this->addObjectTypeElement();
        if (! $this->hasObjectType()) {
            $this->groupMainProperties();
            return;
        }

        $this->addNameElement()
             ->addDisabledElement()
             ->addImportsElement()
             ->addObjectsElement()
             ->addBooleanElements()
             ->addPeriodElement()
             ->addAssignmentElements()
             ->addEventFilterElements(['states'])
             ->groupMainProperties()
             ->addZoneSection()
             ->setButtons();
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addZoneSection()
    {
        $this->addZoneElement(true);

        $elements = array(
            'zone_id',
        );
        $this->addDisplayGroup($elements, 'clustering', array(
            'decorators' => array(
                'FormElements',
                array('HtmlTag', array('tag' => 'dl')),
                'Fieldset',
            ),
            'order' => self::GROUP_ORDER_CLUSTERING,
            'legend' => $this->translate('Zone settings')
        ));

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addNameElement()
    {
        $this->addElement('text', 'object_name', [
            'label'       => $this->translate('Name'),
            'required'    => true,
            'description' => $this->translate('Name for the Icinga dependency you are going to create')
        ]);

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addAssignmentElements()
    {
        if (!$this->object || !$this->object->isApplyRule()) {
            return $this;
        }

        $this->addElement('select', 'apply_to', [
            'label'        => $this->translate('Apply to'),
            'description'  => $this->translate(
                'Whether this dependency should affect hosts or services'
            ),
            'required'     => true,
            'class'        => 'autosubmit',
            'multiOptions' => $this->optionalEnum([
                'host'    => $this->translate('Hosts'),
                'service' => $this->translate('Services'),
            ])
        ]);

        $applyTo = $this->getSentOrObjectValue('apply_to');

        if (! $applyTo) {
            return $this;
        }

        $suggestionContext = ucfirst($applyTo) . 'FilterColumns';
        $this->addAssignFilter([
            'suggestionContext' => $suggestionContext,
            'description' => $this->translate(
                'This allows you to configure an assignment filter. Please feel'
                . ' free to combine as many nested operators as you want. The'
                . ' "contains" operator is valid for arrays only. Please use'
                . ' wildcards and the = (equals) operator when searching for'
                . ' partial string matches, like in *.example.com'
            )
        ]);

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addPeriodElement()
    {
        $periods = $this->db->enumTimeperiods();
        if (empty($periods)) {
            return $this;
        }

        $this->addElement(
            'select',
            'period_id',
            array(
                'label' => $this->translate('Time period'),
                'description' => $this->translate(
                    'The name of a time period which determines when this'
                    . ' notification should be triggered. Not set by default.'
                ),
                'multiOptions' => $this->optionalEnum($periods),
            )
        );

        return $this;
    }

    /**
     * @return $this
     */
    protected function addBooleanElements()
    {
        $this->addBoolean('disable_checks', [
            'label'       => $this->translate('Disable Checks'),
            'description' => $this->translate(
                'Whether to disable checks when this dependency fails.'
                . ' Defaults to false.'
            )
        ], null);

        $this->addBoolean('disable_notifications', [
            'label'       => $this->translate('Disable Notifications'),
            'description' => $this->translate(
                'Whether to disable notifications when this dependency fails.'
                . ' Defaults to true.'
            )
        ], null);

        $this->addBoolean('ignore_soft_states', [
            'label'       => $this->translate('Ignore Soft States'),
            'description' => $this->translate(
                'Whether to ignore soft states for the reachability calculation.'
                . ' Defaults to true.'
            )
        ], null);

        return $this;
    }

    /**
     * @return $this
     * @throws \Zend_Form_Exception
     */
    protected function addObjectsElement()
    {
        $dependency = $this->getObject();
        $parentHost = $dependency->get('parent_host');
        if ($parentHost === null) {
            $parentHostVar = $dependency->get('parent_host_var');
            if ($parentHostVar !== null && \strlen($parentHostVar) > 0) {
                $parentHost = '$' . $dependency->get('parent_host_var') . '$';
            }
        }

        $parentHostDescription = $this->translate('Optional. The parent host.');
        $applyTo = $this->getSentOrObjectValue('apply_to');
        $parentHostValidator = new Zend_Validate_Callback(function ($value) use ($applyTo) {
            if ($applyTo === 'host' && $this->isCustomVar($value)) {
                return explode('.', trim($value, '$'))[0] === 'host';
            }

            return true;
        });

        $parentHostValidator->setMessage(
            $this->translate('The parent host cannot be a service custom variable for a host dependency'),
            Zend_Validate_Callback::INVALID_VALUE
        );

        if ($applyTo === 'service') {
            $additionalDescription = $this->translate(
                'You might want to refer to Host or Service Custom Variables via $host|service.vars.varname$'
            );
        } else {
            $additionalDescription = $this->translate(
                'You might want to refer to Host Custom Variables via $host.vars.varname$'
            );
        }

        $parentHostDescription .= ' ' . $additionalDescription;

        $this->addElement('text', 'parent_host', [
            'label'                   => $this->translate('Parent Host'),
            'description'             => $parentHostDescription,
            'class'                   => "autosubmit director-suggest",
            'data-suggestion-context' => 'hostnames',
            'order'                   => 10,
            'required'                => $this->isObject(),
            'value'                   => $parentHost,
            'validators'               => [$parentHostValidator]
        ]);
        $sentParent = $this->getSentOrObjectValue('parent_host');

        if (!empty($sentParent) || $dependency->isApplyRule()) {
            $parentService = $dependency->get('parent_service');
            if ($parentService === null) {
                $parentServiceVar = $dependency->get('parent_service_by_name');
                if ($parentServiceVar) {
                    $parentService = '$' . $parentServiceVar . '$';
                }
            }

            $parentServiceDescription = $this->translate(
                'Optional. The parent service. If omitted this dependency'
                . ' object is treated as host dependency.'
            );

            $parentServiceDescription .= ' ' . $additionalDescription;

            $parentServiceValidator = clone $parentHostValidator;

            $parentServiceValidator->setMessage(
                $this->translate('The parent service cannot be a service custom variable for a host dependency'),
                Zend_Validate_Callback::INVALID_VALUE
            );

            $this->addElement('text', 'parent_service', [
                'label'                    => $this->translate('Parent Service'),
                'description'              => $parentServiceDescription,
                'class'                    => "autosubmit director-suggest",
                'data-suggestion-context'  => 'servicenames',
                'data-suggestion-for-host' => $sentParent,
                'order'                    => 20,
                'value'                    => $parentService,
                'validators'               => [$parentServiceValidator]
            ]);
        }

        // If configuring Object, allow selection of child host and/or service,
        // otherwise apply rules will determine child object.
        if ($dependency->isObject()) {
            $this->addElement('text', 'child_host', [
                'label'       => $this->translate('Child Host'),
                'description' => $this->translate('The child host.'),
                'value'       => $dependency->get('child_host'),
                'order'       => 30,
                'class'       => 'autosubmit director-suggest',
                'required'    => $this->isObject(),
                'data-suggestion-context' => 'hostnames',
            ]);

            $sentChild = $this->getSentOrObjectValue('child_host');

            if (!empty($sentChild)) {
                $this->addElement('text', 'child_service', [
                    'label' => $this->translate('Child Service'),
                    'description' => $this->translate(
                        'Optional. The child service. If omitted this dependency'
                        . ' object is treated as host dependency.'
                    ),
                    'class' => 'autosubmit director-suggest',
                    'order' => 40,
                    'value' => $this->getObject()->get('child_service'),
                    'data-suggestion-context'  => 'servicenames',
                    'data-suggestion-for-host' => $sentChild,
                ]);
            }
        }

        $elements = ['parent_host', 'child_host', 'parent_service', 'child_service'];
        $this->addDisplayGroup($elements, 'related_objects', [
            'decorators' => [
                'FormElements',
                ['HtmlTag', ['tag' => 'dl']],
                'Fieldset',
            ],
            'order' => self::GROUP_ORDER_RELATED_OBJECTS,
            'legend' => $this->translate('Related Objects')
        ]);

        return $this;
    }

    /**
     * Hint: this is unused. Why?
     *
     * @param IcingaDependency $dependency
     * @return $this
     */
    public function createApplyRuleFor(IcingaDependency $dependency)
    {
        $object = $this->object();
        $object->setImports($dependency->getObjectName());
        $object->set('object_type', 'apply');
        $object->set('object_name', $dependency->getObjectName());

        return $this;
    }

    protected function handleProperties(DbObject $object, &$values)
    {
        if ($this->hasBeenSent()) {
            if (isset($values['parent_host'])) {
                if ($this->isCustomVar($values['parent_host'])) {
                    $values['parent_host_var'] = \trim($values['parent_host'], '$');
                    $values['parent_host'] = '';
                } else {
                    $values['parent_host_var'] = '';
                }
            }

            if (isset($values['parent_service'])) {
                if ($this->isCustomVar($values['parent_service'])) {
                    $values['parent_service_by_name'] = trim($values['parent_service'], '$');
                    $values['parent_service'] = '';
                } else {
                    $values['parent_service_by_name'] = '';
                }
            }
        }

        parent::handleProperties($object, $values);
    }

    protected function isCustomVar($string)
    {
        return preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
    }
}