mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-30 17:24:18 +02:00
legacy/IcingaHost: Render assigned hostgroups into host object
This commit is contained in:
parent
26e58834f6
commit
f1dbeca77c
@ -2,11 +2,6 @@
|
||||
|
||||
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';
|
||||
@ -19,15 +14,6 @@ class IcingaHostGroup extends IcingaObjectGroup
|
||||
return true;
|
||||
}
|
||||
|
||||
public function renderToLegacyConfig(IcingaConfig $config)
|
||||
{
|
||||
if ($this->get('assign_filter') !== null) {
|
||||
$this->renderLegacyApplyToConfig($config);
|
||||
} else {
|
||||
parent::renderToLegacyConfig($config);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getHostGroupMembershipResolver()
|
||||
{
|
||||
if ($this->hostgroupMembershipResolver === null) {
|
||||
@ -53,100 +39,4 @@ class IcingaHostGroup extends IcingaObjectGroup
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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['members'] = array();
|
||||
$file->addLegacyObject($this);
|
||||
} else {
|
||||
$allMembers = array();
|
||||
|
||||
// make sure we write to all zones
|
||||
// so host -> group relations are still possible
|
||||
foreach (IcingaObject::loadAllByType('zone', $conn) as $zone) {
|
||||
if (! array_key_exists($zone->id, $zoneMap)) {
|
||||
$zoneMap[$zone->id] = array();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($zoneMap as $zoneId => $members) {
|
||||
$file = $this->legacyZoneHostgroupFile($config, $zoneId);
|
||||
$this->properties['members'] = $members;
|
||||
$file->addLegacyObject($this);
|
||||
|
||||
$allMembers = array_merge($allMembers, $members);
|
||||
}
|
||||
|
||||
$deploymentMode = $config->getDeploymentMode();
|
||||
if ($deploymentMode === 'active-passive') {
|
||||
$this->properties['members'] = $allMembers;
|
||||
$this->legacyZoneHostgroupFile($config, 0)
|
||||
->addLegacyObject($this);
|
||||
} elseif ($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',
|
||||
'.cfg'
|
||||
);
|
||||
}
|
||||
|
||||
protected function renderLegacyMembers()
|
||||
{
|
||||
if (empty($this->properties['members'])) {
|
||||
return '';
|
||||
}
|
||||
return c1::renderKeyValue('members', join(',', $this->properties['members']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: rendered with renderLegacyMembers()
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @codingStandardsIgnoreStart
|
||||
*/
|
||||
protected function renderLegacyAssign_filter()
|
||||
{
|
||||
// @codingStandardsIgnoreEnd
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
@ -2142,7 +2142,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
||||
protected function renderLegacyGroups()
|
||||
{
|
||||
if ($this->supportsGroups()) {
|
||||
return $this->groups()->toLegacyConfigString();
|
||||
$applied = array();
|
||||
if ($this instanceof IcingaHost) {
|
||||
$applied = $this->getAppliedGroups();
|
||||
}
|
||||
return $this->groups()->toLegacyConfigString($applied);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
@ -2351,12 +2355,25 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
||||
*/
|
||||
public function renderAssign_Filter()
|
||||
{
|
||||
// @codingStandardsIgnoreEnd
|
||||
return ' ' . AssignRenderer::forFilter(
|
||||
Filter::fromQueryString($this->get('assign_filter'))
|
||||
)->renderAssign() . "\n";
|
||||
}
|
||||
|
||||
public function renderLegacyAssign_Filter()
|
||||
{
|
||||
// @codingStandardsIgnoreEnd
|
||||
if ($this instanceof IcingaHostGroup) {
|
||||
$c = " # resolved memberships are set via the individual object\n";
|
||||
} else {
|
||||
$c = " # assign is not supported for " . $this->type . "\n";
|
||||
}
|
||||
$c .= ' #' . AssignRenderer::forFilter(
|
||||
Filter::fromQueryString($this->get('assign_filter'))
|
||||
)->renderAssign() . "\n";
|
||||
return $c;
|
||||
}
|
||||
|
||||
public function toLegacyConfigString()
|
||||
{
|
||||
$str = implode(array(
|
||||
|
@ -348,9 +348,10 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
|
||||
return c::renderKeyValue('groups', c::renderArray($groups));
|
||||
}
|
||||
|
||||
public function toLegacyConfigString()
|
||||
public function toLegacyConfigString($additionalGroups = array())
|
||||
{
|
||||
$groups = array_keys($this->groups);
|
||||
$groups = array_merge(array_keys($this->groups), $additionalGroups);
|
||||
$groups = array_unique($groups);
|
||||
|
||||
if (empty($groups)) {
|
||||
return '';
|
||||
|
Loading…
x
Reference in New Issue
Block a user