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
#2298)

Earlier the director did not support the rendering of service custom
variables for Parent Service in Dependencies form. Here, this issue is
fixed.

fixes #2289
  • Loading branch information
nilmerg authored Feb 7, 2024
2 parents c653696 + 48ea3e4 commit 6351df6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 30 deletions.
110 changes: 83 additions & 27 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 @@ -196,34 +197,79 @@ protected function addObjectsElement()
$parentHost = '$' . $dependency->get('parent_host_var') . '$';
}
}

$parentHostDescription = $this->translate('Optional. The parent host.');
$applyTo = $this->getSentOrObjectValue('apply_to');
$parentHostValidator = new Zend_Validate_Callback(function ($value) use ($applyTo) {
if ($applyTo === 'host' && $this->isCustomVar($value)) {
return explode('.', trim($value, '$'))[0] === 'host';
}

return true;
});

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

if ($applyTo === 'service') {
$additionalDescription = $this->translate(
'You might want to refer to Host or Service Custom Variables via $host|service.vars.varname$'
);
} else {
$additionalDescription = $this->translate(
'You might want to refer to Host Custom Variables via $host.vars.varname$'
);
}

$parentHostDescription .= ' ' . $additionalDescription;

$this->addElement('text', 'parent_host', [
'label' => $this->translate('Parent Host'),
'description' => $this->translate(
'The parent host. You might want to refer Host Custom Variables'
. ' via $host.vars.varname$'
),
'class' => "autosubmit director-suggest",
'label' => $this->translate('Parent Host'),
'description' => $parentHostDescription,
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'hostnames',
'order' => 10,
'required' => $this->isObject(),
'value' => $parentHost
'order' => 10,
'required' => $this->isObject(),
'value' => $parentHost,
'validators' => [$parentHostValidator]
]);
$sentParent = $this->getSentOrObjectValue('parent_host');

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

$parentServiceDescription = $this->translate(
'Optional. The parent service. If omitted this dependency'
. ' object is treated as host dependency.'
);

$parentServiceDescription .= ' ' . $additionalDescription;

$parentServiceValidator = clone $parentHostValidator;

$parentServiceValidator->setMessage(
$this->translate('The parent service cannot be a service custom variable for a 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' => $parentServiceDescription,
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'servicenames',
'data-suggestion-for-host' => $sentParent,
'order' => 20,
'value' => $parentService,
'validators' => [$parentServiceValidator]
]);
}

// If configuring Object, allow selection of child host and/or service,
Expand Down Expand Up @@ -290,11 +336,22 @@ public function createApplyRuleFor(IcingaDependency $dependency)
protected function handleProperties(DbObject $object, &$values)
{
if ($this->hasBeenSent()) {
if (isset($values['parent_host'])
&& $this->isCustomVar($values['parent_host'])
) {
$values['parent_host_var'] = \trim($values['parent_host'], '$');
$values['parent_host'] = '';
if (isset($values['parent_host'])) {
if ($this->isCustomVar($values['parent_host'])) {
$values['parent_host_var'] = \trim($values['parent_host'], '$');
$values['parent_host'] = '';
} else {
$values['parent_host_var'] = '';
}
}

if (isset($values['parent_service'])) {
if ($this->isCustomVar($values['parent_service'])) {
$values['parent_service_by_name'] = trim($values['parent_service'], '$');
$values['parent_service'] = '';
} else {
$values['parent_service_by_name'] = '';
}
}
}

Expand All @@ -303,7 +360,6 @@ protected function handleProperties(DbObject $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);
}
}
29 changes: 26 additions & 3 deletions library/Director/Objects/IcingaDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public function parentHostIsVar()
return $this->get('parent_host_var') !== null;
}

/**
* Check if the given string is a custom variable
*
* @param $string string
*
* @return false|int
*/
protected function isCustomVar(string $string)
{
return preg_match('/^(?:host|service)\.vars\..+$/', $string);
}

/**
* @return string
* @throws ConfigurationError
Expand Down Expand Up @@ -440,9 +452,16 @@ public function renderParent_service_id()
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)
);
}

Expand Down Expand Up @@ -593,8 +612,12 @@ 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->isCustomVar($this->get('parent_service_by_name'))
)
) {
return $this->get('parent_service_by_name');
}
Expand Down

0 comments on commit 6351df6

Please sign in to comment.