Object/ServiceCommand: some more refactoring
This commit is contained in:
parent
fc5d3de568
commit
6d0b9310c3
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Icinga\Module\Director\Clicommands;
|
||||
|
||||
use Icinga\Cli\Params;
|
||||
use Icinga\Module\Director\Cli\ObjectCommand;
|
||||
use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder;
|
||||
use Icinga\Module\Director\Objects\IcingaHost;
|
||||
|
@ -31,41 +32,49 @@ class ServiceCommand extends ObjectCommand
|
|||
$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-');
|
||||
self::checkForOverrideSafety($this->params);
|
||||
$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);
|
||||
self::applyOverriddenVars($host, $name, $properties);
|
||||
$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)
|
||||
{
|
||||
if (empty($properties)) {
|
||||
|
|
|
@ -215,8 +215,8 @@ class ObjectCommand extends Command
|
|||
$object = $this->getObject();
|
||||
}
|
||||
|
||||
$appends = $this->stripPrefixedProperties($this->params, 'append-');
|
||||
$remove = $this->stripPrefixedProperties($this->params, 'remove-');
|
||||
$appends = self::stripPrefixedProperties($this->params, 'append-');
|
||||
$remove = self::stripPrefixedProperties($this->params, 'remove-');
|
||||
|
||||
if ($this->params->shift('replace')) {
|
||||
$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 = [];
|
||||
$len = strlen($prefix);
|
||||
|
|
Loading…
Reference in New Issue