Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global element for default group behavior. #2782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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