2016-09-17 00:25:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2019-07-04 09:19:40 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
2016-09-17 00:25:11 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaDependency;
|
|
|
|
|
|
|
|
class IcingaDependencyForm extends DirectorObjectForm
|
|
|
|
{
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
public function setup()
|
|
|
|
{
|
|
|
|
$this->setupDependencyElements();
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/***
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2017-08-23 21:39:48 +02:00
|
|
|
protected function setupDependencyElements()
|
|
|
|
{
|
2016-09-17 00:25:11 +02:00
|
|
|
$this->addObjectTypeElement();
|
|
|
|
if (! $this->hasObjectType()) {
|
|
|
|
$this->groupMainProperties();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addNameElement()
|
|
|
|
->addDisabledElement()
|
|
|
|
->addImportsElement()
|
|
|
|
->addObjectsElement()
|
|
|
|
->addBooleanElements()
|
|
|
|
->addPeriodElement()
|
|
|
|
->addAssignmentElements()
|
2017-10-12 14:56:13 +02:00
|
|
|
->addEventFilterElements(['states'])
|
2016-09-17 00:25:11 +02:00
|
|
|
->groupMainProperties()
|
2017-12-29 18:24:51 +01:00
|
|
|
->addZoneSection()
|
2016-09-17 00:25:11 +02:00
|
|
|
->setButtons();
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2017-12-29 18:24:51 +01:00
|
|
|
protected function addZoneSection()
|
|
|
|
{
|
|
|
|
$this->addZoneElement(true);
|
|
|
|
|
|
|
|
$elements = array(
|
|
|
|
'zone_id',
|
|
|
|
);
|
|
|
|
$this->addDisplayGroup($elements, 'clustering', array(
|
|
|
|
'decorators' => array(
|
|
|
|
'FormElements',
|
|
|
|
array('HtmlTag', array('tag' => 'dl')),
|
|
|
|
'Fieldset',
|
|
|
|
),
|
|
|
|
'order' => 80,
|
|
|
|
'legend' => $this->translate('Zone settings')
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
protected function addNameElement()
|
|
|
|
{
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addElement('text', 'object_name', [
|
2016-09-17 00:25:11 +02:00
|
|
|
'label' => $this->translate('Name'),
|
|
|
|
'required' => true,
|
|
|
|
'description' => $this->translate('Name for the Icinga dependency you are going to create')
|
2017-08-25 22:42:38 +02:00
|
|
|
]);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
protected function addAssignmentElements()
|
|
|
|
{
|
|
|
|
if (!$this->object || !$this->object->isApplyRule()) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addElement('select', 'apply_to', [
|
2016-09-17 00:25:11 +02:00
|
|
|
'label' => $this->translate('Apply to'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether this dependency should affect hosts or services'
|
|
|
|
),
|
2019-07-08 13:52:48 +02:00
|
|
|
'required' => true,
|
2016-09-17 00:25:11 +02:00
|
|
|
'class' => 'autosubmit',
|
2017-08-25 22:42:38 +02:00
|
|
|
'multiOptions' => $this->optionalEnum([
|
|
|
|
'host' => $this->translate('Hosts'),
|
|
|
|
'service' => $this->translate('Services'),
|
|
|
|
])
|
|
|
|
]);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
|
|
|
$applyTo = $this->getSentOrObjectValue('apply_to');
|
|
|
|
|
2017-10-12 14:56:13 +02:00
|
|
|
if (! $applyTo) {
|
2016-09-17 00:25:11 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-10-12 14:56:13 +02:00
|
|
|
$suggestionContext = ucfirst($applyTo) . 'FilterColumns';
|
|
|
|
$this->addAssignFilter([
|
|
|
|
'suggestionContext' => $suggestionContext,
|
2016-09-17 00:25:11 +02:00
|
|
|
'description' => $this->translate(
|
|
|
|
'This allows you to configure an assignment filter. Please feel'
|
2019-07-08 14:11:53 +02:00
|
|
|
. ' free to combine as many nested operators as you want. The'
|
|
|
|
. ' "contains" operator is valid for arrays only. Please use'
|
|
|
|
. ' wildcards and the = (equals) operator when searching for'
|
|
|
|
. ' partial string matches, like in *.example.com'
|
2016-09-17 00:25:11 +02:00
|
|
|
)
|
2017-10-12 14:56:13 +02:00
|
|
|
]);
|
|
|
|
|
2016-09-17 00:25:11 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
protected function addPeriodElement()
|
|
|
|
{
|
|
|
|
$periods = $this->db->enumTimeperiods();
|
|
|
|
if (empty($periods)) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'period_id',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Time period'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'The name of a time period which determines when this'
|
|
|
|
. ' notification should be triggered. Not set by default.'
|
|
|
|
),
|
|
|
|
'multiOptions' => $this->optionalEnum($periods),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-08-23 21:39:48 +02:00
|
|
|
protected function addBooleanElements()
|
|
|
|
{
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addBoolean('disable_checks', [
|
|
|
|
'label' => $this->translate('Disable Checks'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether to disable checks when this dependency fails.'
|
|
|
|
. ' Defaults to false.'
|
|
|
|
)
|
|
|
|
], null);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addBoolean('disable_notifications', [
|
|
|
|
'label' => $this->translate('Disable Notificiations'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether to disable notifications when this dependency fails.'
|
|
|
|
. ' Defaults to true.'
|
|
|
|
)
|
|
|
|
], null);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addBoolean('ignore_soft_states', [
|
|
|
|
'label' => $this->translate('Ignore Soft States'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether to ignore soft states for the reachability calculation.'
|
|
|
|
. ' Defaults to true.'
|
|
|
|
)
|
|
|
|
], null);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
protected function addObjectsElement()
|
|
|
|
{
|
2019-07-04 08:26:31 +02:00
|
|
|
$dependency = $this->getObject();
|
|
|
|
$parentHost = $dependency->get('parent_host');
|
|
|
|
if ($parentHost === null) {
|
2019-07-08 13:52:48 +02:00
|
|
|
$parentHostVar = $dependency->get('parent_host_var');
|
|
|
|
if (\strlen($parentHostVar) > 0) {
|
|
|
|
$parentHost = '$' . $dependency->get('parent_host_var') . '$';
|
|
|
|
}
|
2019-07-04 08:26:31 +02:00
|
|
|
}
|
|
|
|
$this->addElement('text', 'parent_host', [
|
|
|
|
'label' => $this->translate('Parent Host'),
|
|
|
|
'description' => $this->translate(
|
2019-07-08 13:28:21 +02:00
|
|
|
'The parent host. You might want to refer Host Custom Variables'
|
|
|
|
. ' via $host.vars.varname$'
|
2019-07-04 08:26:31 +02:00
|
|
|
),
|
|
|
|
'class' => "autosubmit director-suggest",
|
|
|
|
'data-suggestion-context' => 'hostnames',
|
|
|
|
'order' => 10,
|
|
|
|
'required' => $this->isObject(),
|
|
|
|
'value' => $parentHost
|
|
|
|
]);
|
|
|
|
$sentParent = $this->getSentOrObjectValue('parent_host');
|
|
|
|
|
|
|
|
if (!empty($sentParent) || $dependency->isApplyRule()) {
|
|
|
|
$parentService = $dependency->get('parent_service');
|
|
|
|
$this->addElement('text', 'parent_service', [
|
2016-09-17 00:25:11 +02:00
|
|
|
'label' => $this->translate('Parent Service'),
|
|
|
|
'description' => $this->translate(
|
2019-07-04 08:26:31 +02:00
|
|
|
'Optional. The parent service. If omitted this dependency'
|
|
|
|
. ' object is treated as host dependency.'
|
2016-09-17 00:25:11 +02:00
|
|
|
),
|
|
|
|
'class' => "autosubmit director-suggest",
|
|
|
|
'data-suggestion-context' => 'servicenames',
|
2019-07-04 08:26:31 +02:00
|
|
|
'data-suggestion-for-host' => $sentParent,
|
2016-09-17 00:25:11 +02:00
|
|
|
'order' => 20,
|
2019-07-04 08:26:31 +02:00
|
|
|
'value' => $parentService
|
|
|
|
]);
|
2016-09-17 00:25:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:42:38 +02:00
|
|
|
// If configuring Object, allow selection of child host and/or service,
|
|
|
|
// otherwise apply rules will determine child object.
|
2019-07-04 08:26:31 +02:00
|
|
|
if ($dependency->isObject()) {
|
|
|
|
$this->addElement('text', 'child_host', [
|
2019-07-08 13:28:21 +02:00
|
|
|
'label' => $this->translate('Child Host'),
|
|
|
|
'description' => $this->translate('The child host.'),
|
|
|
|
'value' => $dependency->get('child_host'),
|
|
|
|
'order' => 30,
|
|
|
|
'class' => 'autosubmit director-suggest',
|
|
|
|
'required' => $this->isObject(),
|
2019-07-04 08:26:31 +02:00
|
|
|
'data-suggestion-context' => 'hostnames',
|
|
|
|
]);
|
2017-08-23 21:39:48 +02:00
|
|
|
|
2019-07-08 13:28:21 +02:00
|
|
|
$sentChild = $this->getSentOrObjectValue('child_host');
|
2017-08-23 21:39:48 +02:00
|
|
|
|
2019-07-08 13:28:21 +02:00
|
|
|
if (!empty($sentChild)) {
|
2017-08-25 22:42:38 +02:00
|
|
|
$this->addElement('text', 'child_service', [
|
|
|
|
'label' => $this->translate('Child Service'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Optional. The child service. If omitted this dependency'
|
|
|
|
. ' object is treated as host dependency.'
|
|
|
|
),
|
2019-07-08 13:28:21 +02:00
|
|
|
'class' => 'autosubmit director-suggest',
|
2017-08-25 22:42:38 +02:00
|
|
|
'order' => 40,
|
|
|
|
'value' => $this->getObject()->get('child_service'),
|
|
|
|
'data-suggestion-context' => 'servicenames',
|
2019-07-08 13:28:21 +02:00
|
|
|
'data-suggestion-for-host' => $sentChild,
|
2017-08-25 22:42:38 +02:00
|
|
|
]);
|
2016-09-17 00:25:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:42:38 +02:00
|
|
|
$elements = ['parent_host', 'child_host', 'parent_service', 'child_service'];
|
|
|
|
$this->addDisplayGroup($elements, 'related_objects', [
|
|
|
|
'decorators' => [
|
2016-09-17 00:25:11 +02:00
|
|
|
'FormElements',
|
2017-08-25 22:42:38 +02:00
|
|
|
['HtmlTag', ['tag' => 'dl']],
|
2016-09-17 00:25:11 +02:00
|
|
|
'Fieldset',
|
2017-08-25 22:42:38 +02:00
|
|
|
],
|
2016-09-17 00:25:11 +02:00
|
|
|
'order' => 25,
|
|
|
|
'legend' => $this->translate('Related Objects')
|
2017-08-25 22:42:38 +02:00
|
|
|
]);
|
2016-09-17 00:25:11 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:45:54 +02:00
|
|
|
/**
|
|
|
|
* Hint: this is unused. Why?
|
|
|
|
*
|
|
|
|
* @param IcingaDependency $dependency
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-09-17 00:25:11 +02:00
|
|
|
public function createApplyRuleFor(IcingaDependency $dependency)
|
|
|
|
{
|
|
|
|
$object = $this->object();
|
2017-08-25 22:42:38 +02:00
|
|
|
$object->setImports($dependency->getObjectName());
|
|
|
|
$object->set('object_type', 'apply');
|
|
|
|
$object->set('object_name', $dependency->getObjectName());
|
|
|
|
|
2016-09-17 00:25:11 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2019-07-04 09:19:40 +02:00
|
|
|
|
2019-11-28 06:52:09 +01:00
|
|
|
protected function handleProperties(DbObject $object, &$values)
|
2019-07-04 09:19:40 +02:00
|
|
|
{
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
if (isset($values['parent_host'])
|
|
|
|
&& $this->isCustomVar($values['parent_host'])
|
|
|
|
) {
|
2019-07-08 13:28:21 +02:00
|
|
|
$values['parent_host_var'] = \trim($values['parent_host'], '$');
|
2019-07-04 09:19:40 +02:00
|
|
|
$values['parent_host'] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::handleProperties($object, $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isCustomVar($string)
|
|
|
|
{
|
2019-07-08 13:28:21 +02:00
|
|
|
return \preg_match('/^\$(?:host)\.vars\..+\$$/', $string);
|
|
|
|
// Eventually: return \preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
|
2019-07-04 09:19:40 +02:00
|
|
|
}
|
2016-09-17 00:25:11 +02:00
|
|
|
}
|