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(); $db = $this->db();
$host = $this->getHostObject(); $host = $this->getHostObject();
$serviceName = $this->params->get('service'); $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([ $service = IcingaService::load([
'object_name' => $serviceName, 'object_name' => $serviceName,
'service_set_id' => $set->get('id') 'service_set_id' => $setTemplate->get('id')
], $this->db()); ], $this->db());
$service = IcingaService::create([ $service = IcingaService::create([
'id' => $service->get('id'),
'object_type' => 'apply', 'object_type' => 'apply',
'object_name' => $serviceName, 'object_name' => $serviceName,
'host_id' => $host->get('id'), 'host_id' => $host->get('id'),

View File

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

View File

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