ServiceCommand: extract override-logic

This commit is contained in:
Thomas Gelf 2022-07-20 08:34:05 +02:00
parent 706a9b1fc1
commit 02bed9265a

View File

@ -18,49 +18,54 @@ 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');
}
$this->setServiceProperties($host);
}
parent::setAction();
}
protected function setServiceProperties($hostname)
{
$name = $this->getName();
$host = IcingaHost::load($hostname, $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');
}
}
protected static function assertVarsForOverrides($properties)
{
if (empty($properties)) {