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
1 changed files with 52 additions and 21 deletions

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) {
printf("%s '%s' has been deleted\n", $type, $name); if ($this->load($name)->delete()) {
exit(0); printf("%s '%s' has been deleted\n", $type, $name);
} 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,32 +288,50 @@ 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');
$object = $from::fromPlainObject( $from->setProperties($this->remainingParams());
$from->toPlainObject($resolve),
$from->getConnection()
)->set('object_name', $name);
$object->setProperties($this->remainingParams()); foreach ($this->shiftOneOrMoreNames() as $name) {
$object = $from::fromPlainObject(
$from->toPlainObject($resolve),
$from->getConnection()
);
if ($replace && $this->exists($name)) { $object->set('object_name', $name);
$object = $this->load($name)->replaceWith($object);
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); 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() protected function remainingParams()
{ {
if ($json = $this->params->shift('json')) { if ($json = $this->params->shift('json')) {