diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index 621383a5..816961ab 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -6,6 +6,7 @@ use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\FilterChain; use Icinga\Data\Filter\FilterExpression; use Icinga\Module\Director\Forms\IcingaAddServiceForm; +use Icinga\Module\Director\Forms\IcingaAddServiceSetForm; use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Web\Controller\ObjectsController; use dipl\Html\Link; @@ -35,6 +36,11 @@ class HostsController extends ObjectsController $url, null, ['class' => 'icon-plus'] + ))->add(Link::create( + $this->translate('Add Service Set'), + clone($url)->setPath('director/hosts/addserviceset'), + null, + ['class' => 'icon-plus'] )); } @@ -68,4 +74,35 @@ class HostsController extends ObjectsController ->handleRequest() ); } + + public function addservicesetAction() + { + $this->addSingleTab($this->translate('Add Service Set')); + $filter = Filter::fromQueryString($this->params->toString()); + + $objects = array(); + $db = $this->db(); + /** @var $filter FilterChain */ + foreach ($filter->filters() as $sub) { + /** @var $sub FilterChain */ + foreach ($sub->filters() as $ex) { + /** @var $ex FilterChain|FilterExpression */ + if ($ex->isExpression() && $ex->getColumn() === 'name') { + $name = $ex->getExpression(); + $objects[$name] = IcingaHost::load($name, $db); + } + } + } + $this->addTitle( + $this->translate('Add Service Set to %d hosts'), + count($objects) + ); + + $this->content()->add( + IcingaAddServiceSetForm::load() + ->setHosts($objects) + ->setDb($this->db()) + ->handleRequest() + ); + } } diff --git a/application/forms/IcingaAddServiceSetForm.php b/application/forms/IcingaAddServiceSetForm.php new file mode 100644 index 00000000..b8891103 --- /dev/null +++ b/application/forms/IcingaAddServiceSetForm.php @@ -0,0 +1,123 @@ +object === null) { + $this->object = IcingaServiceSet::create( + ['object_type' => 'object'], + $this->db + ); + } + + $object = $this->object(); + if ($this->hasBeenSent()) { + $object->set('object_name', $this->getSentValue('imports')); + $object->set('imports', $object->getObjectName()); + } + + if (! $object->hasBeenLoadedFromDb()) { + $this->addSingleImportsElement(); + } + + if (count($object->get('imports'))) { + $description = $object->getResolvedProperty('description'); + if ($description) { + $this->addHtmlHint($description); + } + } + + $this->addHidden('object_type', 'object'); + $this->setButtons(); + } + + protected function setObjectSuccessUrl() + { + if ($this->host) { + $this->setSuccessUrl( + 'director/host/services', + array('name' => $this->host->getObjectName()) + ); + } else { + parent::setObjectSuccessUrl(); + } + } + + public function setHost(IcingaHost $host) + { + $this->host = $host; + return $this; + } + /** + * @param IcingaHost[] $hosts + * @return $this + */ + public function setHosts(array $hosts) + { + $this->hosts = $hosts; + return $this; + } + + protected function addSingleImportsElement() + { + $enum = $this->enumAllowedTemplates(); + + $this->addElement('select', 'imports', array( + 'label' => $this->translate('Service set'), + 'description' => $this->translate( + 'The service Set that should be assigned' + ), + 'required' => true, + 'multiOptions' => $this->optionallyAddFromEnum($enum), + 'class' => 'autosubmit' + )); + + return $this; + } + + public function onSuccess() + { + if ($this->host !== null) { + $this->object->set('host_id', $this->host->get('id')); + parent::onSuccess(); + return; + } + + $plain = $this->object->toPlainObject(); + $db = $this->object->getConnection(); + + foreach ($this->hosts as $host) { + IcingaServiceSet::fromPlainObject($plain, $db) + ->set('host_id', $host->get('id')) + ->store(); + } + + $msg = sprintf( + $this->translate('The Service Set "%s" has been added to %d hosts'), + $this->object->getObjectName(), + count($this->hosts) + ); + + $this->redirectOnSuccess($msg); + } +} diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index a0c66b45..0ef7120f 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -17,6 +17,7 @@ before switching to a new version. ### User Interface * FEATURE: Admins have now access to JSON download links in many places * FEATURE: Users equipped with related permissions can toggle "Show SQL" in the GUI +* FEATURE: A Service Set can now be assigned to multiple hosts at once #1281 ### CLI * FEATURE: Director Health Check Plugin (#1278)