IcingaHostGroup(legacy): Render assign to a hostgroup_member list

Supporting active-passive and masterless, by writing multiple versions of the object.

refs #13049
This commit is contained in:
Markus Frosch 2016-11-07 16:41:00 +01:00
parent 31520a7d21
commit a8a9278806
1 changed files with 99 additions and 0 deletions

View File

@ -2,6 +2,11 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaHostGroup extends IcingaObjectGroup
{
protected $table = 'icinga_hostgroup';
@ -10,4 +15,98 @@ class IcingaHostGroup extends IcingaObjectGroup
{
return true;
}
public function renderToLegacyConfig(IcingaConfig $config)
{
if ($this->get('assign_filter') !== null) {
$this->renderLegacyApplyToConfig($config);
} else {
parent::renderToLegacyConfig($config);
}
}
/**
* @param IcingaConfig $config
*
* @throws ProgrammingError When IcingaConfig deployment mode is not supported
*/
protected function renderLegacyApplyToConfig(IcingaConfig $config)
{
$conn = $this->getConnection();
$filter = Filter::fromQueryString($this->get('assign_filter'));
$hosts = HostApplyMatches::forFilter($filter, $conn);
$this->set('object_type', 'object');
$zoneMap = array();
foreach ($hosts as $hostname) {
$host = IcingaHost::load($hostname, $this->connection);
if (($zoneId = $host->getResolvedProperty('zone_id')) !== null) {
$zoneMap[$zoneId][] = $hostname;
} else {
$zoneMap[0][] = $hostname;
}
}
if (empty($zoneMap)) {
// no hosts matched
$file = $this->legacyZoneHostgroupFile($config);
$this->properties['hostgroup_members'] = array();
$file->addLegacyObject($this);
} else {
$allMembers = array();
foreach ($zoneMap as $zoneId => $members) {
$file = $this->legacyZoneHostgroupFile($config, $zoneId);
$this->properties['hostgroup_members'] = $members;
$file->addLegacyObject($this);
$allMembers = array_merge($allMembers, $members);
}
$deploymentMode = $config->getDeploymentMode();
if ($deploymentMode === 'active-passive') {
$this->properties['hostgroup_members'] = $allMembers;
$this->legacyZoneHostgroupFile($config, 0)
->addLegacyObject($this);
} else if ($deploymentMode == 'masterless') {
// nothing to add
} else {
throw new ProgrammingError('Unsupported deployment mode: %s' . $deploymentMode);
}
}
}
protected function legacyZoneHostgroupFile(IcingaConfig $config, $zoneId = null)
{
if ($zoneId !== null) {
$zone = IcingaZone::load($zoneId, $this->getConnection())->getObjectName();
} else {
$zone = $this->connection->getDefaultGlobalZoneName();
}
return $config->configFile(
'director/' . $zone . '/hostgroups'
);
}
protected function renderLegacyHostgroup_members()
{
if (empty($this->properties['hostgroup_members'])) {
return '';
}
return c1::renderKeyValue('hostgroup_members', join(',', $this->properties['hostgroup_members']));
}
/**
* Note: rendered with renderLegacyHostgroup_members()
*
* @return string
*/
protected function renderLegacyAssign_filter()
{
return '';
}
}