Object/ServiceCommand: some more refactoring

This commit is contained in:
Thomas Gelf 2022-07-20 08:48:09 +02:00
parent fc5d3de568
commit 6d0b9310c3
2 changed files with 42 additions and 33 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Clicommands; namespace Icinga\Module\Director\Clicommands;
use Icinga\Cli\Params;
use Icinga\Module\Director\Cli\ObjectCommand; use Icinga\Module\Director\Cli\ObjectCommand;
use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder; use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
@ -31,41 +32,49 @@ class ServiceCommand extends ObjectCommand
$service = ServiceFinder::find($host, $name); $service = ServiceFinder::find($host, $name);
if ($service->requiresOverrides()) { if ($service->requiresOverrides()) {
$this->params->shift('host'); $this->params->shift('host');
if ($this->params->shift('replace')) { self::checkForOverrideSafety($this->params);
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(); $properties = $this->remainingParams();
self::assertVarsForOverrides($appends); self::applyOverriddenVars($host, $name, $properties);
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'); $this->persistChanges($host, 'Host', $host->getObjectName() . " (Overrides for $name)", 'modified');
} }
} }
protected static function applyOverriddenVars(IcingaHost $host, $serviceName, $properties)
{
self::assertVarsForOverrides($properties);
$current = $host->getOverriddenServiceVars($serviceName);
foreach ($properties as $key => $value) {
if ($key === 'vars') {
foreach ($value as $k => $v) {
$current->$k = $v;
}
} else {
$current->{substr($key, 5)} = $value;
}
}
$host->overrideServiceVars($serviceName, $current);
}
protected static function checkForOverrideSafety(Params $params)
{
if ($params->shift('replace')) {
throw new RuntimeException('--replace is not available for Variable Overrides');
}
$appends = self::stripPrefixedProperties($params, 'append-');
$remove = self::stripPrefixedProperties($params, 'remove-');
self::assertVarsForOverrides($appends);
self::assertVarsForOverrides($remove);
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);
}
protected static function assertVarsForOverrides($properties) protected static function assertVarsForOverrides($properties)
{ {
if (empty($properties)) { if (empty($properties)) {

View File

@ -215,8 +215,8 @@ class ObjectCommand extends Command
$object = $this->getObject(); $object = $this->getObject();
} }
$appends = $this->stripPrefixedProperties($this->params, 'append-'); $appends = self::stripPrefixedProperties($this->params, 'append-');
$remove = $this->stripPrefixedProperties($this->params, 'remove-'); $remove = self::stripPrefixedProperties($this->params, 'remove-');
if ($this->params->shift('replace')) { if ($this->params->shift('replace')) {
$new = $this->create($name)->setProperties($this->remainingParams()); $new = $this->create($name)->setProperties($this->remainingParams());
@ -407,7 +407,7 @@ class ObjectCommand extends Command
} }
} }
protected function stripPrefixedProperties(Params $params, $prefix = 'append-') protected static function stripPrefixedProperties(Params $params, $prefix = 'append-')
{ {
$appends = []; $appends = [];
$len = strlen($prefix); $len = strlen($prefix);