IcingaServiceForm: fix blacklist service from set

fixes #1519
This commit is contained in:
Thomas Gelf 2018-08-13 09:52:07 +02:00
parent 2cf3151fdd
commit 49d9ed7ecd
3 changed files with 46 additions and 25 deletions

View File

@ -332,13 +332,23 @@ class HostController extends ObjectController
$db = $this->db();
$host = $this->getHostObject();
$serviceName = $this->params->get('service');
$set = IcingaServiceSet::load($this->params->get('set'), $db);
$setParams = [
'object_name' => $this->params->get('set'),
'host_id' => $host->get('id')
];
$setTemplate = IcingaServiceSet::load($this->params->get('set'), $db);
if (IcingaServiceSet::exists($setParams, $db)) {
$set = IcingaServiceSet::load($setParams, $db);
} else {
$set = $setTemplate;
}
$service = IcingaService::load([
'object_name' => $serviceName,
'service_set_id' => $set->get('id')
'service_set_id' => $setTemplate->get('id')
], $this->db());
$service = IcingaService::create([
'id' => $service->get('id'),
'object_type' => 'apply',
'object_name' => $serviceName,
'host_id' => $host->get('id'),

View File

@ -13,6 +13,7 @@ use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use dipl\Html\Html;
use dipl\Html\Link;
use RuntimeException;
class IcingaServiceForm extends DirectorObjectForm
{
@ -174,22 +175,22 @@ class IcingaServiceForm extends DirectorObjectForm
/**
* @param IcingaService $service
* @return IcingaService
* @throws ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
protected function getFirstParent(IcingaService $service)
{
$objects = $service->imports()->getObjects();
if (empty($objects)) {
throw new ProgrammingError('Something went wrong, got no parent');
throw new RuntimeException('Something went wrong, got no parent');
}
reset($objects);
return current($objects);
}
/**
* @return bool
* @throws IcingaException
* @throws ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
protected function hasBeenBlacklisted()
{
@ -199,7 +200,7 @@ class IcingaServiceForm extends DirectorObjectForm
if ($this->blacklisted === null) {
$host = $this->host;
$service = $this->getFirstParent($this->object);
$service = $this->getServiceToBeBlacklisted();
$db = $this->db->getDbAdapter();
if ($this->providesOverrides()) {
$this->blacklisted = 1 === (int)$db->fetchOne(
@ -237,13 +238,12 @@ class IcingaServiceForm extends DirectorObjectForm
/**
* @throws IcingaException
* @throws ProgrammingError
* @throws \Zend_Db_Adapter_Exception
*/
protected function blacklist()
{
$host = $this->host;
$service = $this->getFirstParent($this->object);
$service = $this->getServiceToBeBlacklisted();
$db = $this->db->getDbAdapter();
$host->unsetOverriddenServiceVars($this->object->getObjectName())->store();
@ -262,13 +262,25 @@ class IcingaServiceForm extends DirectorObjectForm
}
/**
* @throws IcingaException
* @throws ProgrammingError
* @return IcingaService
* @throws \Icinga\Exception\NotFoundError
*/
protected function getServiceToBeBlacklisted()
{
if ($this->set) {
return $this->object;
} else {
return $this->getFirstParent($this->object);
}
}
/**
* @throws \Icinga\Exception\NotFoundError
*/
protected function removeFromBlacklist()
{
$host = $this->host;
$service = $this->getFirstParent($this->object);
$service = $this->getServiceToBeBlacklisted();
$db = $this->db->getDbAdapter();
$where = implode(' AND ', [
@ -277,7 +289,7 @@ class IcingaServiceForm extends DirectorObjectForm
]);
if ($db->delete('icinga_host_service_blacklist', $where)) {
$msg = sprintf(
$this->translate('%s has been removed from blacklist %s'),
$this->translate('%s is no longer blacklisted on %s'),
$service->getObjectName(),
$host->getObjectName()
);
@ -288,15 +300,15 @@ class IcingaServiceForm extends DirectorObjectForm
/**
* @param IcingaService $service
* @return $this
* @throws ProgrammingError
*/
public function createApplyRuleFor(IcingaService $service)
{
$this->apply = $service;
$object = $this->object();
$object->set('imports', $service->getObjectName());
$object->object_type = 'apply';
$object->object_name = $service->object_name;
$object->set('object_type', 'apply');
$object->set('object_name', $service->getObjectName());
return $this;
}

View File

@ -444,17 +444,16 @@ class IcingaService extends IcingaObject
*/
public function getBlacklistedHostnames()
{
if ($this->isApplyRule()) {
if (PrefetchCache::shouldBeUsed()) {
$lookup = PrefetchCache::instance()->hostServiceBlacklist();
} else {
$lookup = new HostServiceBlacklist($this->getConnection());
}
return $lookup->getBlacklistedHostnamesForService($this);
// Hint: if ($this->isApplyRule()) would be nice, but apply rules are
// not enough, one might want to blacklist single services from Sets
// assigned to single Hosts.
if (PrefetchCache::shouldBeUsed()) {
$lookup = PrefetchCache::instance()->hostServiceBlacklist();
} else {
$lookup = new HostServiceBlacklist($this->getConnection());
}
return [];
return $lookup->getBlacklistedHostnamesForService($this);
}
/**