Skip to content

Commit

Permalink
Support the rendering of service custom variable in Dependencies form
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Feb 2, 2024
1 parent 35c6086 commit 2f95d64
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
60 changes: 47 additions & 13 deletions application/forms/IcingaDependencyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaDependency;
use Zend_Validate_Callback;

class IcingaDependencyForm extends DirectorObjectForm
{
Expand Down Expand Up @@ -212,18 +213,46 @@ protected function addObjectsElement()

if (!empty($sentParent) || $dependency->isApplyRule()) {
$parentService = $dependency->get('parent_service');
if ($parentService === null) {
$parentServiceVar = $dependency->get('parent_service_by_name');
if ($parentServiceVar) {
$parentService = '$' . $parentServiceVar . '$';
}
}

$applyTo = $this->getSentOrObjectValue('apply_to');

$description = 'Optional. The parent service. If omitted this dependency'
. ' object is treated as host dependency.';

if ($applyTo === 'service') {
$description .= ' You might want to refer to Service Custom Variables via $service.vars.varname$';
}

$callbackValidator = new Zend_Validate_Callback(function ($value) {
$applyTo = $this->getSentValue('apply_to');
if ($applyTo === 'service') {
return true;
}

return ! $this->isCustomVar($value);
});

$callbackValidator->setMessage(
$this->translate('The parent service cannot be a custom variable for host dependency'),
Zend_Validate_Callback::INVALID_VALUE
);

$this->addElement('text', 'parent_service', [
'label' => $this->translate('Parent Service'),
'description' => $this->translate(
'Optional. The parent service. If omitted this dependency'
. ' object is treated as host dependency.'
),
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'servicenames',
'data-suggestion-for-host' => $sentParent,
'order' => 20,
'value' => $parentService
]);
'label' => $this->translate('Parent Service'),
'description' => $this->translate($description),
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'servicenames',
'data-suggestion-for-host' => $sentParent,
'order' => 20,
'value' => $parentService,
'validators' => [$callbackValidator]
]);
}

// If configuring Object, allow selection of child host and/or service,
Expand Down Expand Up @@ -296,14 +325,19 @@ protected function handleProperties(DbObject $object, &$values)
$values['parent_host_var'] = \trim($values['parent_host'], '$');
$values['parent_host'] = '';
}
if (isset($values['parent_service'])
&& $this->isCustomVar($values['parent_service'])
) {
$values['parent_service_by_name'] = trim($values['parent_service'], '$');
$values['parent_service'] = '';
}
}

parent::handleProperties($object, $values);
}

protected function isCustomVar($string)
{
return \preg_match('/^\$(?:host)\.vars\..+\$$/', $string);
// Eventually: return \preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
return preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
}
}
23 changes: 20 additions & 3 deletions library/Director/Objects/IcingaDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function parentHostIsVar()
return $this->get('parent_host_var') !== null;
}

protected function isServiceCustomVar($string)
{
return preg_match('/^service\.vars\..+$/', $string);
}

/**
* @return string
* @throws ConfigurationError
Expand Down Expand Up @@ -440,9 +445,16 @@ public function renderParent_service_id()
public function renderParent_service_by_name()
{
// @codingStandardsIgnoreEnd
$var = $this->get('parent_service_by_name');
if ($this->isServiceCustomVar($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)
);
}

Expand Down Expand Up @@ -593,8 +605,13 @@ protected function getRelatedProperty($key)
$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->isServiceCustomVar($this->get('parent_service_by_name'))
)
) {
return $this->get('parent_service_by_name');
}
Expand Down

0 comments on commit 2f95d64

Please sign in to comment.