Allow to add Service Set to multiple hosts at once

fixes #1281
This commit is contained in:
Thomas Gelf 2017-11-10 14:13:52 +01:00
parent 24515e708f
commit 6be7daf36a
3 changed files with 161 additions and 0 deletions

View File

@ -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()
);
}
}

View File

@ -0,0 +1,123 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaAddServiceSetForm extends DirectorObjectForm
{
/** @var IcingaHost[] */
private $hosts;
/** @var IcingaHost */
private $host;
/** @var IcingaServiceSet */
protected $object;
protected $objectName = 'service_set';
protected $listUrl = 'director/services/sets';
public function setup()
{
if ($this->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);
}
}

View File

@ -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)