IcingaServiceSet: introduce a new object type

This commit is contained in:
Thomas Gelf 2016-10-12 07:19:02 +00:00
parent 1a03cea5d7
commit b7018627a0
3 changed files with 209 additions and 0 deletions

View File

@ -367,6 +367,16 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->isApplyRule();
}
/**
* Whether this object can be part of a 'set'
*
* @return bool
*/
public function supportsSets()
{
return $this->supportsSets;
}
/**
* It sometimes makes sense to defer lookups for related properties. This
* kind of lazy-loading allows us to for example set host = 'localhost' and

View File

@ -0,0 +1,179 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\Objects\IcingaService;
class IcingaServiceSet extends IcingaObject
{
protected $table = 'icinga_service_set';
protected $defaultProperties = array(
'id' => null,
'host_id' => null,
'object_name' => null,
'object_type' => null,
'description' => null,
);
protected $supportsCustomVars = true;
protected $supportsApplyRules = true;
protected $relations = array(
'host' => 'IcingaHost',
);
protected $multiRelations = array(
'service' => 'IcingaService',
);
public function isDisabled()
{
return false;
}
public function supportsAssignments()
{
return true;
}
/**
* @return IcingaService
*/
public function getServiceObjects()
{
if (! $this->hasBeenLoadedFromDb()) {
return array();
}
$conn = $this->getConnection();
$db = $conn->getDbAdapter();
$query = $db->select()->from(
array('s' => 'icinga_service'),
'*'
)->join(
array('sset' => 'icinga_service_set_service'),
'sset.service_id = s.id',
array()
)->where(
$db->quoteInto(
'sset.service_set_id = ?',
(int) $this->id
)
)->order('s.object_name');
// TODO: This cannot be prefetched
return IcingaService::loadAll($conn, $query, 'object_name');
}
public function renderToConfig(IcingaConfig $config)
{
if ($this->isTemplate()) {
return;
}
if ($config->isLegacy()) {
return $this->renderToLegacyConfig($config);
}
// Loop over all services belonging to this set
// add our assign rules and then add the service to the config
// eventually clone them beforehand to not get into trouble with caches
// figure out whether we might need a zone property
$file = $config->configFile(
'zones.d/' . $this->getRenderingZone($config) . '/servicesets'
);
$file->prepend($this->getConfigHeaderComment($config));
foreach ($this->getServiceObjects() as $service) {
$service->object_type = $this->object_type;
if ($this->isApplyRule()) {
$service->setAssignments($this->getAssignments());
}
$service->host_id = $this->host_id;
$file->addObject($service);
}
}
protected function getConfigHeaderComment(IcingaConfig $config)
{
if ($config->isLegacy()) {
$comment = "## Service Set '%s'\n\n";
} else {
$comment = "/** Service Set '%s' **/\n\n";
}
return sprintf($comment, $this->object_name);
}
public function renderToLegacyConfig(IcingaConfig $config)
{
if ($this->isTemplate()) {
return;
}
if ($this->isApplyRule()) {
// Not yet
return;
}
// evaluate my assign rules once, get related hosts
// Loop over all services belonging to this set
// generate every service with host_name host1,host2...
$file = $config->configFile(
// TODO: zones.d?
'zones.d/' . $this->getRenderingZone($config) . '/servicesets'
);
$file->prepend($this->getConfigHeaderComment($config));
foreach ($this->getServiceObjects() as $service) {
$service->object_type = 'object';
$service->host_id = $this->host_id;
$file->addLegacyObject($service);
}
}
protected function resolve($what)
{
return array();
}
protected function getResolved($what)
{
return array();
}
protected function getInherited($what)
{
return array();
}
protected function getOrigins($what)
{
return array();
}
public function getRenderingZone(IcingaConfig $config = null)
{
return $this->connection->getDefaultGlobalZoneName();
}
public function xxisObject()
{
return true;
}
public function getUrlParams()
{
return array('name' => $this->object_name);
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Icinga\Module\Director\Objects;
class IcingaServiceAssignment extends IcingaObject
{
protected $table = 'icinga_service_set_assignment';
protected $keyName = 'id';
protected $defaultProperties = array(
'id' => null,
'service_set_id' => null,
'filter_string' => null,
);
protected $relations = array(
'service_set' => 'IcingaServiceSet',
);
}