summaryrefslogtreecommitdiffstats
path: root/library/Director/Cli/ObjectsCommand.php
blob: 3e0844a6aee01a74eeca3a2788cee0a9347b40d6 (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
<?php

namespace Icinga\Module\Director\Cli;

use Icinga\Module\Director\Objects\IcingaObject;

class ObjectsCommand extends Command
{
    protected $type;

    private $objects;

    /**
     * List all objects of this type
     *
     * Use this command to get a list of all matching objects
     *
     * USAGE
     *
     * icingacli director <types> list [options]
     *
     * OPTIONS
     *
     *   --json        Use JSON format
     *   --no-pretty   JSON is pretty-printed per default (for PHP >= 5.4)
     *                 Use this flag to enforce unformatted JSON
     */
    public function listAction()
    {
        $db = $this->db();
        $result = array();
        foreach ($this->getObjects() as $o) {
            $result[] = $o->getObjectName();
        }

        sort($result);

        if ($this->params->shift('json')) {
            echo $this->renderJson($result, !$this->params->shift('no-pretty'));
        } else {
            foreach ($result as $name) {
                echo $name . "\n";
            }
        }
    }

    /**
     * Fetch all objects of this type
     *
     * Use this command to fetch all matching objects
     *
     * USAGE
     *
     * icingacli director <types> fetch [options]
     *
     * OPTIONS
     *
     *   --resolved    Resolve all inherited properties and show a flat
     *                 object
     *   --json        Use JSON format
     *   --no-pretty   JSON is pretty-printed per default (for PHP >= 5.4)
     *                 Use this flag to enforce unformatted JSON
     *   --no-defaults Per default JSON output ships null or default values
     *                 With this flag you will skip those properties
     */
    public function fetchAction()
    {
        $resolved = $this->params->shift('resolved');

        if ($this->params->shift('json')) {
            $noDefaults = $this->params->shift('no-defaults', false);
        } else {
            $this->fail('Currently only json is supported when fetching objects');
        }

        $db = $this->db();
        $res = array();
        foreach ($this->getObjects() as $object) {
            if ($resolved) {
                $object = $object::fromPlainObject($object->toPlainObject(true), $db);
            }

            $res[$object->getObjectName()] = $object->toPlainObject(false, $noDefaults);
        }

        echo $this->renderJson($res, !$this->params->shift('no-pretty'));
    }

    /**
     * @return IcingaObject[]
     */
    protected function getObjects()
    {
        if ($this->objects === null) {
            $this->objects = IcingaObject::loadAllByType(
                $this->getType(),
                $this->db()
            );
        }

        return $this->objects;
    }

    protected function getType()
    {
        if ($this->type === null) {
            // Extract the command class name...
            $className = substr(strrchr(get_class($this), '\\'), 1);
            // ...and strip the Command extension
            $this->type = rtrim(substr($className, 0, -7), 's');
        }

        return $this->type;
    }
}