From 8a57ca943747f7b1be9d4a46a92607a1da7dd08e Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 24 Oct 2016 19:44:43 +0000 Subject: [PATCH 1/2] ObjectsCommand: initial import --- library/Director/Cli/ObjectsCommand.php | 89 +++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 library/Director/Cli/ObjectsCommand.php diff --git a/library/Director/Cli/ObjectsCommand.php b/library/Director/Cli/ObjectsCommand.php new file mode 100644 index 00000000..f70c6050 --- /dev/null +++ b/library/Director/Cli/ObjectsCommand.php @@ -0,0 +1,89 @@ + 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(); + exit; + } + + /** + * Delete a specific object + * + * Use this command to delete a single Icinga object + * + * USAGE + * + * icingacli director 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 skips null or default values + * With this flag you will get all properties + */ + public function fetchAction() + { + if ($this->params->shift('json')) { + $res = array(); + foreach ($this->getObjects() as $object) { + } + } else { + foreach ($this->getObjects() as $object) { + } + } + } + + 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; + } +} From a540fd08aa930b91e1375215dec8ac2ad9768a8f Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 3 Nov 2016 12:32:51 +0100 Subject: [PATCH 2/2] cli/objects: provide new commands fixes #12965 --- application/clicommands/CommandsCommand.php | 15 ++++++++ application/clicommands/HostsCommand.php | 15 ++++++++ library/Director/Cli/ObjectCommand.php | 4 +- library/Director/Cli/ObjectsCommand.php | 42 ++++++++++++++++----- 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 application/clicommands/CommandsCommand.php create mode 100644 application/clicommands/HostsCommand.php diff --git a/application/clicommands/CommandsCommand.php b/application/clicommands/CommandsCommand.php new file mode 100644 index 00000000..798044de --- /dev/null +++ b/application/clicommands/CommandsCommand.php @@ -0,0 +1,15 @@ += 5.4) * Use this flag to enforce unformatted JSON - * --no-defaults Per default JSON output skips null or default values - * With this flag you will get all properties + * --no-defaults Per default JSON output ships null or default values + * With this flag you will skip those properties */ public function showAction() { diff --git a/library/Director/Cli/ObjectsCommand.php b/library/Director/Cli/ObjectsCommand.php index f70c6050..1c718558 100644 --- a/library/Director/Cli/ObjectsCommand.php +++ b/library/Director/Cli/ObjectsCommand.php @@ -5,7 +5,7 @@ namespace Icinga\Module\Director\Cli; use Icinga\Module\Director\Cli\Command; use Icinga\Module\Director\Objects\IcingaObject; -class ObjectCommand extends Command +class ObjectsCommand extends Command { protected $type; @@ -29,7 +29,20 @@ class ObjectCommand extends Command public function listAction() { $db = $this->db(); - exit; + $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"; + } + } } /** @@ -48,19 +61,30 @@ class ObjectCommand extends Command * --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 skips null or default values - * With this flag you will get all properties + * --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')) { - $res = array(); - foreach ($this->getObjects() as $object) { - } + $noDefaults = $this->params->shift('no-defaults', false); } else { - foreach ($this->getObjects() as $object) { - } + $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')); } protected function getObjects()