Object/ServiceCommand: some more refactoring
This commit is contained in:
parent
fc5d3de568
commit
6d0b9310c3
|
@ -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,16 +32,17 @@ 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);
|
$this->persistChanges($host, 'Host', $host->getObjectName() . " (Overrides for $name)", 'modified');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function applyOverriddenVars(IcingaHost $host, $serviceName, $properties)
|
||||||
|
{
|
||||||
self::assertVarsForOverrides($properties);
|
self::assertVarsForOverrides($properties);
|
||||||
$current = $host->getOverriddenServiceVars($name);
|
$current = $host->getOverriddenServiceVars($serviceName);
|
||||||
foreach ($properties as $key => $value) {
|
foreach ($properties as $key => $value) {
|
||||||
if ($key === 'vars') {
|
if ($key === 'vars') {
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
|
@ -50,7 +52,18 @@ class ServiceCommand extends ObjectCommand
|
||||||
$current->{substr($key, 5)} = $value;
|
$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)) {
|
if (!empty($appends)) {
|
||||||
throw new RuntimeException('--append- is not available for Variable Overrides');
|
throw new RuntimeException('--append- is not available for Variable Overrides');
|
||||||
}
|
}
|
||||||
|
@ -60,10 +73,6 @@ class ServiceCommand extends ObjectCommand
|
||||||
// Alternative, untested:
|
// Alternative, untested:
|
||||||
// $this->appendToArrayProperties($object, $appends);
|
// $this->appendToArrayProperties($object, $appends);
|
||||||
// $this->removeProperties($object, $remove);
|
// $this->removeProperties($object, $remove);
|
||||||
|
|
||||||
$host->overrideServiceVars($name, $current);
|
|
||||||
$this->persistChanges($host, 'Host', $host->getObjectName() . " (Overrides for $name)", 'modified');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function assertVarsForOverrides($properties)
|
protected static function assertVarsForOverrides($properties)
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue