diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 56535647..54428418 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -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'), diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index da926775..63b511e7 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -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; } diff --git a/library/Director/Objects/IcingaService.php b/library/Director/Objects/IcingaService.php index a4f2f4f1..a57a4d0e 100644 --- a/library/Director/Objects/IcingaService.php +++ b/library/Director/Objects/IcingaService.php @@ -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); } /**