Cli\ObjectCommand: allow multiple objects...

...as a parameter for clone and delete
This commit is contained in:
Thomas Gelf 2016-09-05 18:37:00 +00:00
parent 138f5d2aed
commit 83031c1349

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Cli; namespace Icinga\Module\Director\Cli;
use Icinga\Exception\MissingParameterException;
use Icinga\Module\Director\Cli\Command; use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
@ -210,18 +211,28 @@ class ObjectCommand extends Command
* USAGE * USAGE
* *
* icingacli director <type> delete <name> * icingacli director <type> delete <name>
*
* EXAMPLES
*
* icingacli director host delete localhost2
*
* icingacli director host delete localhost{3..8}
*/ */
public function deleteAction() public function deleteAction()
{ {
$type = $this->getType(); $type = $this->getType();
$name = $this->getName();
if ($this->getObject()->delete()) { foreach ($this->shiftOneOrMoreNames() as $name) {
if ($this->load($name)->delete()) {
printf("%s '%s' has been deleted\n", $type, $name); printf("%s '%s' has been deleted\n", $type, $name);
exit(0);
} else { } else {
printf("Something went wrong while deleting %s '%s'\n", $type, $name); printf("Something went wrong while deleting %s '%s'\n", $type, $name);
exit(1); 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 localhost2 --from localhost
* *
* icingacli director host clone localhost{3..8} --from localhost2
*
* icingacli director host clone localhost3 --from localhost \ * icingacli director host clone localhost3 --from localhost \
* --address 127.0.0.3 * --address 127.0.0.3
*/ */
@ -275,18 +288,21 @@ class ObjectCommand extends Command
$fromName = $this->params->shiftRequired('from'); $fromName = $this->params->shiftRequired('from');
$from = $this->load($fromName); $from = $this->load($fromName);
$name = $this->getName(); // $name = $this->getName();
$type = $this->getType(); $type = $this->getType();
$resolve = $this->params->shift('flat'); $resolve = $this->params->shift('flat');
$replace = $this->params->shift('replace'); $replace = $this->params->shift('replace');
$from->setProperties($this->remainingParams());
foreach ($this->shiftOneOrMoreNames() as $name) {
$object = $from::fromPlainObject( $object = $from::fromPlainObject(
$from->toPlainObject($resolve), $from->toPlainObject($resolve),
$from->getConnection() $from->getConnection()
)->set('object_name', $name); );
$object->setProperties($this->remainingParams()); $object->set('object_name', $name);
if ($replace && $this->exists($name)) { if ($replace && $this->exists($name)) {
$object = $this->load($name)->replaceWith($object); $object = $this->load($name)->replaceWith($object);
@ -294,11 +310,26 @@ class ObjectCommand extends Command
if ($object->hasBeenModified() && $object->store()) { if ($object->hasBeenModified() && $object->store()) {
printf("%s '%s' has been cloned from %s\n", $type, $name, $fromName); printf("%s '%s' has been cloned from %s\n", $type, $name, $fromName);
} else {
printf("%s '%s' has not been modified\n", $this->getType(), $name);
}
}
exit(0); exit(0);
} }
printf("%s '%s' has not been modified\n", $this->getType(), $name); protected function shiftOneOrMoreNames()
exit(0); {
$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() protected function remainingParams()