From cdcf99592bff0e75a388efd3a3502103f512d7c5 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 8 Jun 2018 22:02:27 +0200 Subject: [PATCH] ServiceCommand: allow to delete single services fixes #719 --- application/clicommands/ServiceCommand.php | 12 ++++++------ library/Director/Cli/Command.php | 21 +++++++++++++++------ library/Director/Cli/ObjectCommand.php | 5 +++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/application/clicommands/ServiceCommand.php b/application/clicommands/ServiceCommand.php index b4720882..c947064e 100644 --- a/application/clicommands/ServiceCommand.php +++ b/application/clicommands/ServiceCommand.php @@ -16,26 +16,26 @@ class ServiceCommand extends ObjectCommand protected function load($name) { - return parent::load($this->makeServiceKey($this->getName())); + return parent::load($this->makeServiceKey($name)); } protected function exists($name) { - return parent::exists($this->makeServiceKey($this->getName())); + return parent::exists($this->makeServiceKey($name)); } protected function makeServiceKey($name) { if ($host = $this->params->get('host')) { - return array( + return [ 'object_name' => $name, 'host_id' => IcingaHost::load($host, $this->db())->get('id'), - ); + ]; } else { - return array( + return [ 'object_name' => $name, 'object_type' => 'template', - ); + ]; } } } diff --git a/library/Director/Cli/Command.php b/library/Director/Cli/Command.php index 6ea17a52..5cbf725f 100644 --- a/library/Director/Cli/Command.php +++ b/library/Director/Cli/Command.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Cli; use Icinga\Cli\Command as CliCommand; +use Icinga\Exception\ConfigurationError; use Icinga\Module\Director\Application\MemoryLimit; use Icinga\Module\Director\Core\CoreApi; use Icinga\Module\Director\Db; @@ -19,13 +20,14 @@ class Command extends CliCommand protected function renderJson($object, $pretty = true) { - if ($pretty && version_compare(PHP_VERSION, '5.4.0') >= 0) { - return json_encode($object, JSON_PRETTY_PRINT) . "\n"; - } else { - return json_encode($object) . "\n"; - } + return json_encode($object, $pretty ? JSON_PRETTY_PRINT : null) . "\n"; } + /** + * @param $json + * @return mixed + * @throws \Icinga\Exception\IcingaException + */ protected function parseJson($json) { $res = json_decode($json); @@ -59,6 +61,12 @@ class Command extends CliCommand } } + /** + * @param null $endpointName + * @return CoreApi|\Icinga\Module\Director\Core\LegacyDeploymentApi + * @throws \Icinga\Exception\IcingaException + * @throws \Icinga\Exception\NotFoundError + */ protected function api($endpointName = null) { if ($this->api === null) { @@ -95,6 +103,7 @@ class Command extends CliCommand /** * @return Db + * @throws ConfigurationError */ protected function db() { @@ -109,7 +118,7 @@ class Command extends CliCommand if ($resourceName) { $this->db = Db::fromResourceName($resourceName); } else { - $this->fail('Director is not configured correctly'); + throw new ConfigurationError('Director is not configured correctly'); } } diff --git a/library/Director/Cli/ObjectCommand.php b/library/Director/Cli/ObjectCommand.php index cf689fe8..7b9f4778 100644 --- a/library/Director/Cli/ObjectCommand.php +++ b/library/Director/Cli/ObjectCommand.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Cli; use Icinga\Exception\MissingParameterException; use Icinga\Module\Director\Objects\IcingaObject; +use InvalidArgumentException; class ObjectCommand extends Command { @@ -107,7 +108,7 @@ class ObjectCommand extends Command } if (! array_key_exists('object_name', $props)) { - $this->fail('Cannot creat an object with at least an object name'); + $this->fail('Cannot create an object with at least an object name'); } $object = IcingaObject::createByType( @@ -396,7 +397,7 @@ class ObjectCommand extends Command if ($this->name === null) { $name = $this->params->shift(); if (! $name) { - $this->fail('Object name parameter is required'); + throw new InvalidArgumentException('Object name parameter is required'); } $this->name = $name;