From bb9105e3705620eab0da5f01bc5820cfcd9a19c4 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 4 Jul 2019 13:10:30 +0200 Subject: [PATCH] IcingaDependency: allow for arrays --- library/Director/Objects/IcingaDependency.php | 94 +++++++++++++++++-- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/library/Director/Objects/IcingaDependency.php b/library/Director/Objects/IcingaDependency.php index fabb2ac0..c2d65051 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -41,6 +41,12 @@ class IcingaDependency extends IcingaObject implements ExportInterface protected $supportsApplyRules = true; + /** + * @internal + * @var bool + */ + protected $renderForArray = false; + protected $relatedSets = [ 'states' => 'StateFilterSet', ]; @@ -114,6 +120,11 @@ class IcingaDependency extends IcingaObject implements ExportInterface return $object; } + public function parentHostIsVar() + { + return $this->get('parent_host_var') !== null; + } + /** * @return string * @throws ConfigurationError @@ -128,18 +139,78 @@ class IcingaDependency extends IcingaObject implements ExportInterface ); } - return sprintf( - "%s %s %s to %s {\n", - $this->getObjectTypeName(), - $this->getType(), - c::renderString($this->getObjectName()), - ucfirst($to) - ); + if ($this->renderForArray) { + return $this->renderArrayObjectHeader($to); + } else { + return $this->renderSingleObjectHeader($to); + } } else { return parent::renderObjectHeader(); } } + protected function renderSingleObjectHeader($to) + { + return sprintf( + "%s %s %s to %s {\n", + $this->getObjectTypeName(), + $this->getType(), + c::renderString($this->getObjectName()), + ucfirst($to) + ); + } + + protected function renderArrayObjectHeader($to) + { + return sprintf( + "%s %s %s for (host_parent_name in %s) to %s {\n", + $this->getObjectTypeName(), + $this->getType(), + c::renderString($this->getObjectName()), + $this->get('parent_host_var'), + ucfirst($to) + ); + } + + /** + * @return string + */ + protected function renderSuffix() + { + if ($this->parentHostIsVar() && ! $this->renderForArray) { + return parent::renderSuffix() . $this->renderCloneForArray(); + } else { + return parent::renderSuffix(); + } + } + + protected function renderCloneForArray() + { + $clone = clone($this); + $clone->renderForArray = true; + + return $clone->toConfigString(); + } + + /** + * @codingStandardsIgnoreStart + */ + public function renderAssign_Filter() + { + if ($this->parentHostIsVar()) { + $varName = $this->get('parent_host_var'); + if ($this->renderForArray) { + $suffix = sprintf(' && typeof(%s) == Array', $varName); + } else { + $suffix = sprintf(' && typeof(%s) == String', $varName); + } + + return preg_replace('/\n$/m', $suffix, parent::renderAssign_Filter() . "\n"); + } else { + return parent::renderAssign_Filter(); + } + } + protected function setKey($key) { // TODO: Check if this method can be removed @@ -303,7 +374,6 @@ class IcingaDependency extends IcingaObject implements ExportInterface public function renderParent_host_id() { // @codingStandardsIgnoreEnd - return $this->renderRelationProperty( 'parent_host', $this->get('parent_host_id'), @@ -319,6 +389,14 @@ class IcingaDependency extends IcingaObject implements ExportInterface */ public function renderParent_host_var() { + // @codingStandardsIgnoreEnd + if ($this->renderForArray) { + return c::renderKeyValue( + 'parent_host_name', + 'host_parent_name' + ); + } + // @codingStandardsIgnoreEnd return c::renderKeyValue( 'parent_host_name',