mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 08:14:04 +02:00
legacy/IcingaService/ServiceSet: Improve apply rendering
This now renders one object per zone with a list of hosts.
This commit is contained in:
parent
f1dbeca77c
commit
d4d309a704
@ -1847,13 +1847,17 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
||||
/**
|
||||
* @codingStandardsIgnoreStart
|
||||
*/
|
||||
protected function renderLegacyHost_id()
|
||||
protected function renderLegacyHost_id($value)
|
||||
{
|
||||
return $this->renderLegacyRelationProperty(
|
||||
'host',
|
||||
$this->get('host_id'),
|
||||
'host_name'
|
||||
);
|
||||
if (is_array($value)) {
|
||||
return c1::renderKeyValue('host_name', c1::renderArray($value));
|
||||
} else {
|
||||
return $this->renderLegacyRelationProperty(
|
||||
'host',
|
||||
$this->get('host_id'),
|
||||
'host_name'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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();
|
||||
|
@ -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()
|
||||
|
@ -223,49 +223,27 @@ 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)
|
||||
{
|
||||
if ($this->get('host_id') === null) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user