legacy/IcingaService/ServiceSet: Improve apply rendering

This now renders one object per zone with a list of hosts.
This commit is contained in:
Markus Frosch 2018-04-26 13:42:42 +02:00
parent f1dbeca77c
commit d4d309a704
3 changed files with 52 additions and 73 deletions

View File

@ -1847,14 +1847,18 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
/**
* @codingStandardsIgnoreStart
*/
protected function renderLegacyHost_id()
protected function renderLegacyHost_id($value)
{
if (is_array($value)) {
return c1::renderKeyValue('host_name', c1::renderArray($value));
} else {
return $this->renderLegacyRelationProperty(
'host',
$this->get('host_id'),
'host_name'
);
}
}
/**
* Display Name only exists for host/service in Icinga 1
@ -2365,6 +2369,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
// @codingStandardsIgnoreEnd
if ($this instanceof IcingaHostGroup) {
$c = " # resolved memberships are set via the individual object\n";
} elseif ($this instanceof IcingaService) {
$c = " # resolved objects are listed here\n";
} else {
$c = " # assign is not supported for " . $this->type . "\n";
}
@ -2947,6 +2953,27 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
protected function mapHostsToZones($names)
{
$map = array();
foreach ($names as $hostname) {
/** @var IcingaHost $host */
$host = IcingaHost::load($hostname, $this->connection);
$zone = $host->getRenderingZone();
if (! array_key_exists($zone, $map)) {
$map[$zone] = array();
}
$map[$zone][] = $hostname;
}
ksort($map);
return $map;
}
public function getUrlParams()
{
$params = array();

View File

@ -198,7 +198,6 @@ class IcingaService extends IcingaObject
public function renderHost_id()
{
// @codingStandardsIgnoreEnd
if ($this->hasBeenAssignedToHostTemplate()) {
return '';
}
@ -231,37 +230,16 @@ class IcingaService extends IcingaObject
$assign_filter = $this->get('assign_filter');
$filter = Filter::fromQueryString($assign_filter);
$hosts = HostApplyMatches::forFilter($filter, $conn);
$hostnames = HostApplyMatches::forFilter($filter, $conn);
$this->set('object_type', 'object');
$this->set('assign_filter', null);
foreach ($hosts as $hostname) {
$file = $this->legacyHostnameServicesFile($hostname, $config);
$this->set('host', $hostname);
$file->addLegacyObject($this);
foreach ($this->mapHostsToZones($hostnames) as $zone => $names) {
$this->set('host_id', $names);
$config->configFile('director/' . $zone . '/service_apply', '.cfg')
->addLegacyObject($this);
}
$this->set('host', null);
$this->set('object_type', 'apply');
$this->set('assign_filter', $assign_filter);
}
/**
* @param string $hostname
* @param IcingaConfig $config
* @return \Icinga\Module\Director\IcingaConfig\IcingaConfigFile
* @throws \Icinga\Exception\NotFoundError
*/
protected function legacyHostnameServicesFile($hostname, IcingaConfig $config)
{
return $config->configFile(
sprintf(
'director/%s/service_apply',
IcingaHost::load($hostname, $this->getConnection())
->getRenderingZone($config)
),
'.cfg'
);
}
/**
@ -273,10 +251,6 @@ class IcingaService extends IcingaObject
return '';
}
if ($this->isApplyRule()) {
throw new InvalidArgumentException('Apply Services can not be rendered directly.');
}
$str = parent::toLegacyConfigString();
if (! $this->isDisabled()

View File

@ -223,47 +223,25 @@ class IcingaServiceSet extends IcingaObject
// Delegating this to the service would look, but this way it's faster
if ($filter = $this->get('assign_filter')) {
$filter = Filter::fromQueryString($filter);
$hosts = HostApplyMatches::forFilter($filter, $conn);
$hostnames = HostApplyMatches::forFilter($filter, $conn);
} else {
$hostnames = array($this->getRelated('host')->object_name);
}
foreach ($this->mapHostsToZones($hostnames) as $zone => $names) {
$file = $config->configFile('director/' . $zone . '/servicesets', '.cfg');
$file->addContent($this->getConfigHeaderComment($config));
foreach ($this->getServiceObjects() as $service) {
$service->set('object_type', 'object');
$service->set('host_id', $names);
$this->copyVarsToService($service);
foreach ($hosts as $hostname) {
$file = $this->legacyHostnameServicesFile($hostname, $config);
$file->addContent($this->getConfigHeaderComment($config));
$service->set('host', $hostname);
$file->addLegacyObject($service);
}
}
} else {
foreach ($this->getServiceObjects() as $service) {
$service->set('object_type', 'object');
$service->set('host_id', $this->get('host_id'));
foreach ($this->vars() as $k => $var) {
$service->$k = $var;
}
$file = $this->legacyRelatedHostFile($service, $config);
$file->addContent($this->getConfigHeaderComment($config));
$file->addLegacyObject($service);
}
}
}
protected function legacyHostnameServicesFile($hostname, IcingaConfig $config)
{
$host = IcingaHost::load($hostname, $this->getConnection());
return $config->configFile(
'director/' . $host->getRenderingZone($config) . '/servicesets',
'.cfg'
);
}
protected function legacyRelatedHostFile(IcingaService $service, IcingaConfig $config)
{
return $config->configFile(
'director/' . $service->getRelated('host')->getRenderingZone($config) . '/servicesets',
'.cfg'
);
}
public function getRenderingZone(IcingaConfig $config = null)