ServiceCommand: allow magic overrides

fixes #2560
This commit is contained in:
Thomas Gelf 2022-07-12 11:27:21 +02:00
parent 922b19397c
commit 050553164d
3 changed files with 70 additions and 2 deletions

View File

@ -3,7 +3,9 @@
namespace Icinga\Module\Director\Clicommands;
use Icinga\Module\Director\Cli\ObjectCommand;
use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder;
use Icinga\Module\Director\Objects\IcingaHost;
use RuntimeException;
/**
* Manage Icinga Services
@ -13,6 +15,64 @@ use Icinga\Module\Director\Objects\IcingaHost;
*/
class ServiceCommand extends ObjectCommand
{
public function setAction()
{
if (($host = $this->params->get('host')) && $this->params->shift('allow-overrides')) {
$name = $this->getName();
$host = IcingaHost::load($host, $this->db());
$service = ServiceFinder::find($host, $name);
if ($service->requiresOverrides()) {
$this->params->shift('host');
if ($this->params->shift('replace')) {
throw new RuntimeException('--replace is not available for Variable Overrides');
}
$appends = $this->stripPrefixedProperties($this->params, 'append-');
$remove = $this->stripPrefixedProperties($this->params, 'remove-');
$properties = $this->remainingParams();
self::assertVarsForOverrides($appends);
self::assertVarsForOverrides($remove);
self::assertVarsForOverrides($properties);
$current = $host->getOverriddenServiceVars($name);
foreach ($properties as $key => $value) {
if ($key === 'vars') {
foreach ($value as $k => $v) {
$current->$k = $v;
}
} else {
$current->{substr($key, 5)} = $value;
}
}
if (! empty($appends)) {
throw new RuntimeException('--append- is not available for Variable Overrides');
}
if (! empty($remove)) {
throw new RuntimeException('--remove- is not available for Variable Overrides');
}
// Alternative, untested:
// $this->appendToArrayProperties($object, $appends);
// $this->removeProperties($object, $remove);
$host->overrideServiceVars($name, $current);
$this->persistChanges($host, 'Host', $host->getObjectName() . " (Overrides for $name)", 'modified');
}
}
parent::setAction();
}
protected static function assertVarsForOverrides($properties)
{
if (empty($properties)) {
return;
}
foreach ($properties as $key => $value) {
if ($key !== 'vars' && substr($key, 0, 5) !== 'vars.') {
throw new RuntimeException("Only Custom Variables can be set based on Variable Overrides");
}
}
}
protected function load($name)
{

View File

@ -137,6 +137,7 @@ Use this command to modify specific properties of an existing Icinga object.
| `--json` | Otherwise provide all options as a JSON string |
| `--replace` | Replace all object properties with the given ones |
| `--auto-create` | Create the object in case it does not exist |
| `--allow-overrides` | Set variable overrides for virtual Services |
#### Examples

View File

@ -4,6 +4,8 @@ namespace Icinga\Module\Director\Cli;
use Icinga\Cli\Params;
use Icinga\Exception\MissingParameterException;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use InvalidArgumentException;
@ -208,12 +210,17 @@ class ObjectCommand extends Command
$this->appendToArrayProperties($object, $appends);
$this->removeProperties($object, $remove);
$this->persistChanges($object, $this->getType(), $name, $action);
}
protected function persistChanges(DbObject $object, $type, $name, $action)
{
if ($object->hasBeenModified() && $object->store()) {
printf("%s '%s' has been %s\n", $this->getType(), $this->name, $action);
printf("%s '%s' has been %s\n", $type, $name, $action);
exit(0);
}
printf("%s '%s' has not been modified\n", $this->getType(), $name);
printf("%s '%s' has not been modified\n", $type, $name);
exit(0);
}