summaryrefslogtreecommitdiffstats
path: root/application/clicommands/ServiceCommand.php
blob: 1bd21e7668c3a5ca5a9300fdd2775b2e4f798dd7 (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
<?php

namespace Icinga\Module\Director\Clicommands;

use Icinga\Cli\Params;
use Icinga\Module\Director\Cli\ObjectCommand;
use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Resolver\OverrideHelper;
use InvalidArgumentException;

/**
 * Manage Icinga Services
 *
 * Use this command to show, create, modify or delete Icinga Service
 * objects
 */
class ServiceCommand extends ObjectCommand
{
    public function setAction()
    {
        if (($host = $this->params->get('host')) && $this->params->shift('allow-overrides')) {
            if ($this->setServiceProperties($host)) {
                return;
            }
        }

        parent::setAction();
    }

    protected function setServiceProperties($hostname)
    {
        $serviceName = $this->getName();
        $host = IcingaHost::load($hostname, $this->db());
        $service = ServiceFinder::find($host, $serviceName);
        if ($service->requiresOverrides()) {
            self::checkForOverrideSafety($this->params);
            $properties = $this->remainingParams();
            unset($properties['host']);
            OverrideHelper::applyOverriddenVars($host, $serviceName, $properties);
            $this->persistChanges($host, 'Host', $hostname . " (Overrides for $serviceName)", 'modified');
            return true;
        }

        return false;
    }

    protected static function checkForOverrideSafety(Params $params)
    {
        if ($params->shift('replace')) {
            throw new InvalidArgumentException('--replace is not available for Variable Overrides');
        }
        $appends = self::stripPrefixedProperties($params, 'append-');
        $remove = self::stripPrefixedProperties($params, 'remove-');
        OverrideHelper::assertVarsForOverrides($appends);
        OverrideHelper::assertVarsForOverrides($remove);
        if (!empty($appends)) {
            throw new InvalidArgumentException('--append- is not available for Variable Overrides');
        }
        if (!empty($remove)) {
            throw new InvalidArgumentException('--remove- is not available for Variable Overrides');
        }
        // Alternative, untested:
        // $this->appendToArrayProperties($object, $appends);
        // $this->removeProperties($object, $remove);
    }

    protected function load($name)
    {
        return parent::load($this->makeServiceKey($name));
    }

    protected function exists($name)
    {
        return parent::exists($this->makeServiceKey($name));
    }

    protected function makeServiceKey($name)
    {
        if ($host = $this->params->get('host')) {
            return [
                'object_name' => $name,
                'host_id'     => IcingaHost::load($host, $this->db())->get('id'),
            ];
        } else {
            return [
                'object_name' => $name,
                'object_type' => 'template',
            ];
        }
    }
}