summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Command/Renderer/IcingaApiCommandRenderer.php
blob: 37075c9943fc8c0b0962abb2f005ef7d1cde2bd5 (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
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Command\Renderer;

use Icinga\Module\Icingadb\Command\IcingaApiCommand;
use Icinga\Module\Icingadb\Command\Object\GetObjectCommand;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Command\Instance\ToggleInstanceFeatureCommand;
use Icinga\Module\Icingadb\Command\Object\AcknowledgeProblemCommand;
use Icinga\Module\Icingadb\Command\Object\AddCommentCommand;
use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand;
use Icinga\Module\Icingadb\Command\Object\DeleteDowntimeCommand;
use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand;
use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand;
use Icinga\Module\Icingadb\Command\Object\RemoveAcknowledgementCommand;
use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand;
use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand;
use Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand;
use Icinga\Module\Icingadb\Command\Object\SendCustomNotificationCommand;
use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand;
use Icinga\Module\Icingadb\Command\IcingaCommand;
use InvalidArgumentException;
use ipl\Orm\Model;
use LogicException;
use Traversable;

/**
 * Icinga command renderer for the Icinga command file
 */
class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
{
    /**
     * Name of the Icinga application object
     *
     * @var string
     */
    protected $app = 'app';

    /**
     * Get the name of the Icinga application object
     *
     * @return string
     */
    public function getApp(): string
    {
        return $this->app;
    }

    /**
     * Set the name of the Icinga application object
     *
     * @param   string  $app
     *
     * @return  $this
     */
    public function setApp(string $app): self
    {
        $this->app = $app;

        return $this;
    }

    /**
     * Apply filter to query data
     *
     * @param array              $data
     * @param Traversable<Model> $objects
     *
     * @return ?Model Any of the objects (useful for further type-dependent handling)
     */
    protected function applyFilter(array &$data, Traversable $objects): ?Model
    {
        $object = null;

        foreach ($objects as $object) {
            if ($object instanceof Service) {
                $data['services'][] = sprintf('%s!%s', $object->host->name, $object->name);
            } else {
                $data['hosts'][] = $object->name;
            }
        }

        return $object;
    }

    /**
     * Get the sub-route of the endpoint for an object
     *
     * @param   Model   $object
     *
     * @return  string
     */
    protected function getObjectPluralType(Model $object): string
    {
        if ($object instanceof Host) {
            return 'hosts';
        }

        if ($object instanceof Service) {
            return 'services';
        }

        throw new LogicException(sprintf('Invalid object type %s provided', get_class($object)));
    }

    /**
     * Render a command
     *
     * @param   IcingaCommand   $command
     *
     * @return  IcingaApiCommand
     */
    public function render(IcingaCommand $command): IcingaApiCommand
    {
        $renderMethod = 'render' . $command->getName();
        if (! method_exists($this, $renderMethod)) {
            throw new InvalidArgumentException(
                sprintf('Can\'t render command. Method %s not found', $renderMethod)
            );
        }

        return $this->$renderMethod($command);
    }

    public function renderGetObject(GetObjectCommand $command): IcingaApiCommand
    {
        $data = [
            'all_joins' => 1,
            'attrs'     => $command->getAttributes() ?: []
        ];

        $endpoint = 'objects/' . $this->getObjectPluralType($this->applyFilter($data, $command->getObjects()));

        return IcingaApiCommand::create($endpoint, $data)->setMethod('GET');
    }

    public function renderAddComment(AddCommentCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/add-comment';
        $data = [
            'author'    => $command->getAuthor(),
            'comment'   => $command->getComment()
        ];

        if ($command->getExpireTime() !== null) {
            $data['expiry'] = $command->getExpireTime();
        }

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderSendCustomNotification(SendCustomNotificationCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/send-custom-notification';
        $data = [
            'author'    => $command->getAuthor(),
            'comment'   => $command->getComment(),
            'force'     => $command->getForced()
        ];

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderProcessCheckResult(ProcessCheckResultCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/process-check-result';
        $data = [
            'exit_status'       => $command->getStatus(),
            'plugin_output'     => $command->getOutput(),
            'performance_data'  => $command->getPerformanceData()
        ];

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderScheduleCheck(ScheduleCheckCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/reschedule-check';
        $data = [
            'next_check'    => $command->getCheckTime(),
            'force'         => $command->getForced()
        ];

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/schedule-downtime';
        $data = [
            'author'        => $command->getAuthor(),
            'comment'       => $command->getComment(),
            'start_time'    => $command->getStart(),
            'end_time'      => $command->getEnd(),
            'duration'      => $command->getDuration(),
            'fixed'         => $command->getFixed(),
            'trigger_name'  => $command->getTriggerId()
        ];

        if ($command instanceof PropagateHostDowntimeCommand) {
            $data['child_options'] = $command->getTriggered() ? 1 : 2;
        }

        if ($command instanceof ScheduleHostDowntimeCommand && $command->getForAllServices()) {
            $data['all_services'] = true;
        }

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/acknowledge-problem';
        $data = [
            'author'        => $command->getAuthor(),
            'comment'       => $command->getComment(),
            'sticky'        => $command->getSticky(),
            'notify'        => $command->getNotify(),
            'persistent'    => $command->getPersistent()
        ];

        if ($command->getExpireTime() !== null) {
            $data['expiry'] = $command->getExpireTime();
        }

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderToggleObjectFeature(ToggleObjectFeatureCommand $command): IcingaApiCommand
    {
        switch ($command->getFeature()) {
            case ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS:
                $attr = 'enable_active_checks';
                break;
            case ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS:
                $attr = 'enable_passive_checks';
                break;
            case ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS:
                $attr = 'enable_notifications';
                break;
            case ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER:
                $attr = 'enable_event_handler';
                break;
            case ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION:
                $attr = 'enable_flapping';
                break;
            default:
                throw new InvalidArgumentException($command->getFeature());
        }

        $endpoint = 'objects/';
        $objects = $command->getObjects();

        $data = [
            'attrs' => [
                $attr => $command->getEnabled()
            ]
        ];


        $endpoint .= $this->getObjectPluralType($this->applyFilter($data, $objects));

        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderDeleteComment(DeleteCommentCommand $command): IcingaApiCommand
    {
        $comments = [];

        foreach ($command->getObjects() as $object) {
            $comments[] = $object->name;
        }

        $endpoint = 'actions/remove-comment';
        $data = [
            'author'    => $command->getAuthor(),
            'comments'  => $comments
        ];

        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderDeleteDowntime(DeleteDowntimeCommand $command): IcingaApiCommand
    {
        $downtimes = [];

        foreach ($command->getObjects() as $object) {
            $downtimes[] = $object->name;
        }

        $endpoint = 'actions/remove-downtime';
        $data = [
            'author'    => $command->getAuthor(),
            'downtimes' => $downtimes
        ];

        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderRemoveAcknowledgement(RemoveAcknowledgementCommand $command): IcingaApiCommand
    {
        $endpoint = 'actions/remove-acknowledgement';
        $data = ['author' => $command->getAuthor()];

        $this->applyFilter($data, $command->getObjects());
        return IcingaApiCommand::create($endpoint, $data);
    }

    public function renderToggleInstanceFeature(ToggleInstanceFeatureCommand $command): IcingaApiCommand
    {
        $endpoint = 'objects/icingaapplications/' . $this->getApp();

        switch ($command->getFeature()) {
            case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS:
                $attr = 'enable_host_checks';
                break;
            case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS:
                $attr = 'enable_service_checks';
                break;
            case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS:
                $attr = 'enable_event_handlers';
                break;
            case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION:
                $attr = 'enable_flapping';
                break;
            case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS:
                $attr = 'enable_notifications';
                break;
            case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA:
                $attr = 'enable_perfdata';
                break;
            default:
                throw new InvalidArgumentException($command->getFeature());
        }

        $data = [
            'attrs' => [
                $attr => $command->getEnabled()
            ]
        ];

        return IcingaApiCommand::create($endpoint, $data);
    }
}