From 83031c13499589de0525ebdabb526518eddf0cb0 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 5 Sep 2016 18:37:00 +0000 Subject: [PATCH] Cli\ObjectCommand: allow multiple objects... ...as a parameter for clone and delete --- library/Director/Cli/ObjectCommand.php | 73 ++++++++++++++++++-------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/library/Director/Cli/ObjectCommand.php b/library/Director/Cli/ObjectCommand.php index 1011d9e9..aa169c63 100644 --- a/library/Director/Cli/ObjectCommand.php +++ b/library/Director/Cli/ObjectCommand.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Cli; +use Icinga\Exception\MissingParameterException; use Icinga\Module\Director\Cli\Command; use Icinga\Module\Director\Objects\IcingaObject; @@ -210,18 +211,28 @@ class ObjectCommand extends Command * USAGE * * icingacli director delete + * + * EXAMPLES + * + * icingacli director host delete localhost2 + * + * icingacli director host delete localhost{3..8} */ public function deleteAction() { $type = $this->getType(); - $name = $this->getName(); - if ($this->getObject()->delete()) { - printf("%s '%s' has been deleted\n", $type, $name); - exit(0); - } else { - printf("Something went wrong while deleting %s '%s'\n", $type, $name); - exit(1); + + foreach ($this->shiftOneOrMoreNames() as $name) { + if ($this->load($name)->delete()) { + printf("%s '%s' has been deleted\n", $type, $name); + } else { + printf("Something went wrong while deleting %s '%s'\n", $type, $name); + exit(1); + } + + $this->object = null; } + exit(0); } /** @@ -267,6 +278,8 @@ class ObjectCommand extends Command * * icingacli director host clone localhost2 --from localhost * + * icingacli director host clone localhost{3..8} --from localhost2 + * * icingacli director host clone localhost3 --from localhost \ * --address 127.0.0.3 */ @@ -275,32 +288,50 @@ class ObjectCommand extends Command $fromName = $this->params->shiftRequired('from'); $from = $this->load($fromName); - $name = $this->getName(); + // $name = $this->getName(); $type = $this->getType(); $resolve = $this->params->shift('flat'); $replace = $this->params->shift('replace'); - $object = $from::fromPlainObject( - $from->toPlainObject($resolve), - $from->getConnection() - )->set('object_name', $name); + $from->setProperties($this->remainingParams()); - $object->setProperties($this->remainingParams()); + foreach ($this->shiftOneOrMoreNames() as $name) { + $object = $from::fromPlainObject( + $from->toPlainObject($resolve), + $from->getConnection() + ); - if ($replace && $this->exists($name)) { - $object = $this->load($name)->replaceWith($object); + $object->set('object_name', $name); + + if ($replace && $this->exists($name)) { + $object = $this->load($name)->replaceWith($object); + } + + if ($object->hasBeenModified() && $object->store()) { + printf("%s '%s' has been cloned from %s\n", $type, $name, $fromName); + } else { + printf("%s '%s' has not been modified\n", $this->getType(), $name); + } } - if ($object->hasBeenModified() && $object->store()) { - printf("%s '%s' has been cloned from %s\n", $type, $name, $fromName); - exit(0); - } - - printf("%s '%s' has not been modified\n", $this->getType(), $name); exit(0); } + protected function shiftOneOrMoreNames() + { + $names = array(); + while ($name = $this->params->shift()) { + $names[] = $name; + } + + if (empty($names)) { + throw new MissingParameterException('Required object name is missing'); + } + + return $names; + } + protected function remainingParams() { if ($json = $this->params->shift('json')) {