From 5419d4428c86c488a43124f85e5407d7cbae6541 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:47 +0200 Subject: Adding upstream version 1.11.1. Signed-off-by: Daniel Baumann --- library/Director/Objects/IcingaDependency.php | 90 +++++++++++++-------------- 1 file changed, 43 insertions(+), 47 deletions(-) (limited to 'library/Director/Objects/IcingaDependency.php') diff --git a/library/Director/Objects/IcingaDependency.php b/library/Director/Objects/IcingaDependency.php index c9d9b89..abd92e6 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -3,9 +3,7 @@ namespace Icinga\Module\Director\Objects; use Icinga\Exception\ConfigurationError; -use Icinga\Module\Director\Db; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; -use Icinga\Module\Director\Exception\DuplicateKeyException; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Exception\NotFoundError; use Icinga\Data\Filter\Filter; @@ -80,52 +78,21 @@ class IcingaDependency extends IcingaObject implements ExportInterface return $this->getObjectName(); } - /** - * @return object - * @deprecated please use \Icinga\Module\Director\Data\Exporter - * @throws \Icinga\Exception\NotFoundError - */ - public function export() + public function parentHostIsVar() { - $props = (array) $this->toPlainObject(); - ksort($props); - - return (object) $props; + return $this->get('parent_host_var') !== null; } /** - * @param $plain - * @param Db $db - * @param bool $replace - * @return static - * @throws DuplicateKeyException - * @throws \Icinga\Exception\NotFoundError + * Check if the given string is a custom variable + * + * @param $string string + * + * @return false|int */ - public static function import($plain, Db $db, $replace = false) + protected function isCustomVar(string $string) { - $properties = (array) $plain; - $name = $properties['object_name']; - $key = $name; - - if ($replace && static::exists($key, $db)) { - $object = static::load($key, $db); - } elseif (static::exists($key, $db)) { - throw new DuplicateKeyException( - 'Dependency "%s" already exists', - $name - ); - } else { - $object = static::create([], $db); - } - - $object->setProperties($properties); - - return $object; - } - - public function parentHostIsVar() - { - return $this->get('parent_host_var') !== null; + return preg_match('/^(?:host|service)\.vars\..+$/', $string); } /** @@ -485,9 +452,16 @@ class IcingaDependency extends IcingaObject implements ExportInterface public function renderParent_service_by_name() { // @codingStandardsIgnoreEnd + $var = $this->get('parent_service_by_name'); + if ($this->isCustomVar($var)) { + return c::renderKeyValue( + 'parent_service_name', + $var + ); + } return c::renderKeyValue( 'parent_service_name', - c::renderString($this->get('parent_service_by_name')) + c::renderString($var) ); } @@ -506,7 +480,7 @@ class IcingaDependency extends IcingaObject implements ExportInterface protected function resolveUnresolvedRelatedProperty($name) { $short = substr($name, 0, -3); - /** @var IcingaObject $class */ + /** @var IcingaObject|string $class */ $class = $this->getRelationClass($short); $objKey = $this->unresolvedRelatedProperties[$name]; @@ -589,7 +563,25 @@ class IcingaDependency extends IcingaObject implements ExportInterface $this->reallySet($name, $object->get('id')); unset($this->unresolvedRelatedProperties[$name]); } else { - throw new NotFoundError('Unable to resolve related property: "%s"', $name); + // Depend on a single service on a single host. Rare case, as usually you want to + // depend on a service on the very same host - and leave the Host field empty. The + // latter is already being handled above. This duplicates some code, but I'll leave + // it this way for now. There might have been a reason for the parent_host_id = null + // check in that code. + if ($name === 'parent_service_id' && $this->get('object_type') === 'apply') { + $this->reallySet( + 'parent_service_by_name', + $this->unresolvedRelatedProperties[$name] + ); + $this->reallySet('parent_service_id', null); + unset($this->unresolvedRelatedProperties[$name]); + return; + } + throw new NotFoundError(sprintf( + 'Unable to resolve related property: %s "%s"', + $name, + $this->unresolvedRelatedProperties[$name] + )); } } @@ -620,8 +612,12 @@ class IcingaDependency extends IcingaObject implements ExportInterface $related = parent::getRelatedProperty($key); // handle special case for plain string parent service on Dependency // Apply rules - if ($related === null && $key === 'parent_service' - && null !== $this->get('parent_service_by_name') + if ($related === null + && $key === 'parent_service' + && ( + $this->get('parent_service_by_name') + && ! $this->isCustomVar($this->get('parent_service_by_name')) + ) ) { return $this->get('parent_service_by_name'); } -- cgit v1.2.3