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;
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 <type> delete <name>
*
* 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')) {