Skip to content

Commit

Permalink
Add global element for default group behaviour.
Browse files Browse the repository at this point in the history
Add default value 'assign' for default group behaviour which matches group behaviour.
Add logic to IcingaObjectGroups so the class initalizes with the global group behaviour and sets the group operator based on the class operator.
Partial Fix for Icinga#344, Icinga#636, Icinga#824, Icinga#2273
  • Loading branch information
sol1-matt committed Aug 7, 2023
1 parent f3455c1 commit a5a5086
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
19 changes: 19 additions & 0 deletions application/forms/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public function setup()
'value' => $settings->getStoredValue('default_global_zone')
));

$this->addElement('select', 'default_group_behaviour', array(
'label' => $this->translate('Default group behaviour'),
'multiOptions' => $this->eventuallyConfiguredEnum(
'default_group_behaviour',
array(
'assign' => $this->translate('Assign'),
'add' => $this->translate('Add'),
// Remove (-=) is a group option but global remove isn't useful as all groups would be removed all the time.
)
),
'description' => $this->translate(
'Icinga Director deploys group objects using the operator assign (=)'
. ', which overrides groups set in inherited objects, by default.'
. ' This option allows you to adjust the group operator globally to'
. ' add (+=) which appends groups from inherited objects.'
),
'value' => $settings->getStoredValue('default_group_behaviour')
));

$this->addElement('text', 'icinga_package_name', array(
'label' => $this->translate('Icinga Package Name'),
'description' => $this->translate(
Expand Down
27 changes: 25 additions & 2 deletions library/Director/Objects/IcingaObjectGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer

protected $object;

private $operator;

private $position = 0;

protected $idx = array();
Expand All @@ -35,6 +37,10 @@ public function __construct(IcingaObject $object)
$class = $this->getGroupClass();
$class::prefetchAll($this->object->getConnection());
}

// Set the default operator from global config
$settings = $this->object->getConnection()->settings();
$this->operator($settings->get('default_group_behaviour'));
}

#[\ReturnTypeWillChange]
Expand All @@ -54,6 +60,11 @@ public function hasBeenModified()
return $this->modified;
}

public function operator($val)
{
$this->operator = $val;
}

#[\ReturnTypeWillChange]
public function current()
{
Expand Down Expand Up @@ -363,7 +374,13 @@ public function toConfigString()
return '';
}

return c::renderKeyValue('groups', c::renderArray($groups));
if ($this->operator === "add") {
return c::renderKeyOperatorValue('groups', "+=", c::renderArray($groups));
} elseif ($this->operator === "remove") {
return c::renderKeyOperatorValue('groups', "-=", c::renderArray($groups));
} else {
return c::renderKeyValue('groups', c::renderArray($groups));
}
}

public function toLegacyConfigString($additionalGroups = array())
Expand All @@ -376,7 +393,13 @@ public function toLegacyConfigString($additionalGroups = array())
}

$type = $this->object->getLegacyObjectType();
return c1::renderKeyValue($type.'groups', c1::renderArray($groups));
if ($this->operator === "add") {
return c1::renderKeyOperatorValue('groups', "+=", c1::renderArray($groups));
} elseif ($this->operator == "remove") {
return c1::renderKeyOperatorValue('groups', "-=", c1::renderArray($groups));
} else {
return c1::renderKeyValue($type.'groups', c1::renderArray($groups));
}
}

public function __toString()
Expand Down
1 change: 1 addition & 0 deletions library/Director/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Settings

protected $defaults = [
'default_global_zone' => 'director-global',
'default_group_behaviour' => 'assign',
'icinga_package_name' => 'director',
'config_format' => 'v2',
'override_services_varname' => '_override_servicevars',
Expand Down

0 comments on commit a5a5086

Please sign in to comment.