From c4201ba49c8aff7876c19e72aa7129e823cf6ba0 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Fri, 23 May 2025 11:09:27 +0200 Subject: [PATCH] IcingaServiceForm: Show dictionary's nested keys accessible for apply-for-rule --- application/forms/IcingaServiceForm.php | 70 +++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index 9abb9dc8..56b5b718 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -7,7 +7,6 @@ use Icinga\Data\Filter\Filter; use Icinga\Exception\IcingaException; use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Auth\Permission; -use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter; use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Web\Form\DirectorObjectForm; @@ -17,6 +16,9 @@ use Icinga\Module\Director\Objects\IcingaServiceSet; use Icinga\Module\Director\Web\Table\ObjectsTableHost; use ipl\Html\Html; use gipfl\IcingaWeb2\Link; +use ipl\Html\HtmlElement; +use ipl\Html\Table; +use ipl\Html\Text; use RuntimeException; class IcingaServiceForm extends DirectorObjectForm @@ -40,6 +42,8 @@ class IcingaServiceForm extends DirectorObjectForm /** @var bool|null */ private $blacklisted; + private $dictionaryUuidMap = []; + public function setApplyGenerated(IcingaService $applyGenerated) { $this->applyGenerated = $applyGenerated; @@ -635,11 +639,31 @@ class IcingaServiceForm extends DirectorObjectForm $properties = []; foreach ($vars as $var) { $properties['host.vars.' . $var->key_name] = $var->label . ' (' . $var->key_name . ')'; + if ($var->value_type === 'dict') { + $this->dictionaryUuidMap['host.vars.' . $var->key_name] = $var->uuid; + } } return [t('director', 'Custom variables') => $properties]; } + + + private function fetchNestedDictionaryKeys(string $dictionaryUuid) + { + $query = $this->db->getDbAdapter() + ->select() + ->from( + ['dp' => 'director_property'], + [ + 'key_name' => 'dp.label', + 'label' => 'dp.key_name', + ] + )->where("parent_uuid = ?", $dictionaryUuid); + + return $this->db->getDbAdapter()->fetchPairs($query); + } + /** * @return $this * @throws \Zend_Form_Exception @@ -649,7 +673,7 @@ class IcingaServiceForm extends DirectorObjectForm if ($this->object->isApplyRule()) { $hostProperties = $this->applyForVars(); - $this->addElement('select', 'apply_for', array( + $this->addElement('select', 'apply_for', [ 'label' => $this->translate('Apply For'), 'class' => 'assign-property autosubmit', 'multiOptions' => $this->optionalEnum($hostProperties, $this->translate('None')), @@ -660,7 +684,47 @@ class IcingaServiceForm extends DirectorObjectForm 'host.vars.array_var)" where "config" will be accessible through "$config$". ' . 'NOTE: only custom variables of type "Array" and "Dictionary" are eligible.' ) - )); + ]); + + if ($this->hasBeenSent()) { + $applyFor = $this->getRequest()->getPost('apply_for'); + } else { + $applyFor = $this->object->get('apply_for'); + } + + if (isset($this->dictionaryUuidMap[$applyFor])) { + $dictionaryKeys = $this->fetchNestedDictionaryKeys($this->dictionaryUuidMap[$applyFor]); + + if (! empty($dictionaryKeys)) { + $configVariables = new Table(); + foreach ($dictionaryKeys as $label => $key) { + $configVariables->add([$label . ' (' . $key . ')', '=>', '$config.' . $key . '$']); + } + + + $this->addHtmlHint( + HtmlElement::create( + 'div', + null, + [ + Text::create($this->translate( + 'Nested keys of selected host dictionary variable for apply-for-rule' + . ' are accessible through config as shown below:' + )), + $configVariables + ] + ), + ['name' => 'apply_for_hint'] + ); + + $this->addElementsToGroup( + ['apply_for_hint'], + 'custom_fields', + DirectorObjectForm::GROUP_ORDER_CUSTOM_FIELDS, + $this->translate('Custom properties') + ); + } + } } return $this;