From fffa6a5f3b2cbc185ff7eea643e5a6aad4e1f633 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 22 Jul 2016 02:14:11 +0200 Subject: [PATCH] CLI: add clone functionality fixes #12203 --- doc/60-CLI.md | 30 ++++++++++++++ library/Director/Cli/ObjectCommand.php | 55 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/doc/60-CLI.md b/doc/60-CLI.md index cbf82bbf..3751f7a0 100644 --- a/doc/60-CLI.md +++ b/doc/60-CLI.md @@ -189,6 +189,36 @@ in JSON format. | | With this flag you will get all properties | +### Clone an existing object + +Use this command to clone a specific object + +#### Usage + +`icingacli director clone --from [options]` + +#### Options + +| Option | Description | +|---------------------|-----------------------------------------------------| +| `--from ` | The name of the object you want to clone | +| `-- ` | Override specific properties while cloning | +| `--replace` | In case an object already exists replace it | +| | with the clone | +| `--flat` | Do no keep inherited properties but create a flat | +| | object with all resolved/inherited properties | + +#### Examples + +```shell +icingacli director host clone localhost2 --from localhost +``` + +```shell +icingacli director host clone localhost3 --from localhost --address 127.0.0.3 +``` + + ### Other interesting tasks diff --git a/library/Director/Cli/ObjectCommand.php b/library/Director/Cli/ObjectCommand.php index d0357fda..1011d9e9 100644 --- a/library/Director/Cli/ObjectCommand.php +++ b/library/Director/Cli/ObjectCommand.php @@ -246,6 +246,61 @@ class ObjectCommand extends Command } } + /** + * Clone an existing object + * + * Use this command to clone a specific object + * + * USAGE + * + * icingacli director clone --from [options] + * + * OPTIONS + * --from The name of the object you want to clone + * -- Override specific properties while cloning + * --replace In case an object already exists replace + * it with the clone + * --flat Do no keep inherited properties but create a flat + * object with all resolved/inherited properties + * + * EXAMPLES + * + * icingacli director host clone localhost2 --from localhost + * + * icingacli director host clone localhost3 --from localhost \ + * --address 127.0.0.3 + */ + public function cloneAction() + { + $fromName = $this->params->shiftRequired('from'); + $from = $this->load($fromName); + + $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); + + $object->setProperties($this->remainingParams()); + + 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); + exit(0); + } + + printf("%s '%s' has not been modified\n", $this->getType(), $name); + exit(0); + } + protected function remainingParams() { if ($json = $this->params->shift('json')) {