From 02bed9265a41684606c51b946a53e47a9338a32e Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 20 Jul 2022 08:34:05 +0200 Subject: [PATCH] ServiceCommand: extract override-logic --- application/clicommands/ServiceCommand.php | 81 ++++++++++++---------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/application/clicommands/ServiceCommand.php b/application/clicommands/ServiceCommand.php index 8ef48fc4..9c9f3954 100644 --- a/application/clicommands/ServiceCommand.php +++ b/application/clicommands/ServiceCommand.php @@ -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)) {