summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaDependency.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/Objects/IcingaDependency.php')
-rw-r--r--library/Director/Objects/IcingaDependency.php90
1 files changed, 43 insertions, 47 deletions
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');
}