Skip to content

Commit

Permalink
Add global assign/ignore where (Icinga 2).
Browse files Browse the repository at this point in the history
Refs #5929
  • Loading branch information
Michael Friedrich committed Jun 10, 2014
1 parent 279c9ec commit 54cbafc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
12 changes: 9 additions & 3 deletions modules/conftool/library/Conftool/Icinga2/Icinga2Hostgroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ class Icinga2Hostgroup extends Icinga2ObjectDefinition

protected $v1ArrayProperties = array(
'hostgroup_members',
'members'
);

protected $v1AttributeMap = array(
'members' => 'members',
'alias' => 'display_name',
'notes' => 'vars.notes',
'action_url' => 'vars.action_url',
Expand All @@ -22,5 +20,13 @@ class Icinga2Hostgroup extends Icinga2ObjectDefinition

protected function convertMembers($members)
{
foreach ($this->splitComma($members) as $member) {
if (substr($member, 0, 1) === '!') {
$member = substr($member, 1);
$this->ignoreWhere('host.name == ' . $this->migrateLegacyString($member));
} else {
$this->assignWhere('host.name == ' . $this->migrateLegacyString($member));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Icinga2ObjectDefinition
protected $v1ArrayProperties = array();
protected $type;
protected $assigns = array();
protected $ignores = array();

public function __construct($name)
{
Expand All @@ -28,9 +29,24 @@ public function __get($key)
return $this->properties[$key];
}

protected function assignWhere($assign)
{
$this->assigns[] = $assign;
}

protected function ignoreWhere($ignore)
{
$this->ignores[] = $ignore;
}

protected function setAttributesFromIcingaObjectDefinition(IcingaObjectDefinition $object)
{
foreach ($object->getAttributes() as $key => $value) {
$func = 'convert' . ucfirst($key);
if (method_exists($this, $func)) {
$this->$func($value);
continue;
}
if (! array_key_exists($key, $this->v1AttributeMap)) {
throw new Icinga2ConfigMigrationException(
sprintf('Cannot convert the "%s" property of given v1 object: ', $key) . print_r($object, 1)
Expand Down Expand Up @@ -118,14 +134,29 @@ protected function getAttributesAsString()
}
return $str;
}

protected function getAssignmentsAsString()
{
$str = '';
foreach ($this->assigns as $assign) {
$str .= sprintf(" assign where %s\n", $assign);
}
foreach ($this->ignores as $ignore) {
$str .= sprintf(" ignore where %s\n", $ignore);
}
return $str;
}

// inherits "plugin-check-command"

public function __toString()
{
return sprintf(
"object %s \"%s\" {\n%s}\n\n",
"object %s \"%s\" {\n%s%s}\n\n",
$this->type,
$this->name,
$this->getAttributesAsString()
$this->getAttributesAsString(),
$this->getAssignmentsAsString()
);
}

Expand Down

0 comments on commit 54cbafc

Please sign in to comment.