summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Setup/ApiTransportStep.php
blob: 1e2e905d27a09b1f231113b96a45a44663a311ef (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
<?php

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

namespace Icinga\Module\Icingadb\Setup;

use Exception;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;
use Icinga\Module\Setup\Step;
use ipl\Html\Attributes;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\Table;
use ipl\Html\Text;

class ApiTransportStep extends Step
{
    /** @var array */
    protected $data;

    /** @var Exception */
    protected $error;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function apply()
    {
        $transportConfig = $this->data;
        $transportName = $transportConfig['name'];
        unset($transportConfig['name']);

        try {
            $config = Config::module('icingadb', 'commandtransports', true);
            $config->setSection($transportName, $transportConfig);
            $config->saveIni();
        } catch (Exception $e) {
            $this->error = $e;
            return false;
        }

        return true;
    }

    public function getSummary()
    {
        $description = new HtmlElement('p', null, Text::create(mt(
            'icingadb',
            'The Icinga 2 API will be accessed using the following connection details:'
        )));

        $apiOptions = new Table();
        $apiOptions->addHtml(Table::row([
            new HtmlElement('strong', null, Text::create(t('Host'))),
            $this->data['host']
        ]));
        $apiOptions->addHtml(Table::row([
            new HtmlElement('strong', null, Text::create(t('Port'))),
            $this->data['port']
        ]));
        $apiOptions->addHtml(Table::row([
            new HtmlElement('strong', null, Text::create(t('Username'))),
            $this->data['username']
        ]));
        $apiOptions->addHtml(Table::row([
            new HtmlElement('strong', null, Text::create(t('Password'))),
            str_repeat('*', strlen($this->data['password']))
        ]));

        $topic = new HtmlElement('div', Attributes::create(['class' => 'topic']));
        $topic->addHtml($description, $apiOptions);

        $summary = new HtmlDocument();
        $summary->addHtml(
            new HtmlElement('h2', null, Text::create(mt('icingadb', 'Icinga 2 API'))),
            $topic
        );

        return $summary->render();
    }

    public function getReport()
    {
        if ($this->error === null) {
            return [sprintf(
                mt('icingadb', 'Commandtransport configuration update successful: %s'),
                Config::module('icingadb', 'commandtransports')->getConfigFile()
            )];
        } else {
            return [
                sprintf(
                    mt('icingadb', 'Commandtransport configuration update failed: %s'),
                    Config::module('icingadb', 'commandtransports')->getConfigFile()
                ),
                sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->error))
            ];
        }
    }
}