IcingaAddServiceForm: refactor, rename and use...

...it for single and for multiple hosts

refs #1087
This commit is contained in:
Thomas Gelf 2017-08-21 19:55:24 +02:00
parent 089197d2c5
commit c15a6e1a90
3 changed files with 73 additions and 64 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Controllers;
use Exception;
use Icinga\Module\Director\CustomVariable\CustomVariableDictionary;
use Icinga\Module\Director\Db\AppliedServiceSetLoader;
use Icinga\Module\Director\Forms\IcingaAddServiceForm;
use Icinga\Module\Director\Forms\IcingaServiceForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
@ -43,6 +44,12 @@ class HostController extends ObjectController
$host = $this->getHostObject();
$this->addServicesHeader();
$this->addTitle($this->translate('Add Service: %s'), $host->getObjectName());
$this->content()->add(
IcingaAddServiceForm::load()
->setHost($host)
->setDb($this->db())
->handleRequest()
);
}
public function servicesetAction()

View File

@ -5,7 +5,7 @@ namespace Icinga\Module\Director\Controllers;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Module\Director\Forms\IcingaAddServiceToMultipleHostsForm;
use Icinga\Module\Director\Forms\IcingaAddServiceForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Controller\ObjectsController;
use ipl\Html\Link;
@ -62,7 +62,7 @@ class HostsController extends ObjectsController
);
$this->content()->add(
IcingaAddServiceToMultipleHostsForm::load()
IcingaAddServiceForm::load()
->setHosts($objects)
->setDb($this->db())
->handleRequest()

View File

@ -6,19 +6,24 @@ use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
class IcingaAddServiceToMultipleHostsForm extends DirectorObjectForm
class IcingaAddServiceForm extends DirectorObjectForm
{
/** @var IcingaHost[] */
private $hosts;
/** @var IcingaHost */
private $host;
/** @var IcingaService */
protected $object;
protected $objectName = 'service';
public function setup()
{
if ($this->object === null) {
$this->object = IcingaService::create(
array('object_type' => 'object'),
['object_type' => 'object'],
$this->db
);
}
@ -26,34 +31,43 @@ class IcingaAddServiceToMultipleHostsForm extends DirectorObjectForm
$this->addSingleImportElement();
if (! ($imports = $this->getSentOrObjectValue('imports'))) {
$this->setButtons();
$this->setSubmitLabel($this->translate('Next'));
$this->groupMainProperties();
return;
}
$this->addNameElement()
->groupMainProperties()
$this->removeElement('imports');
$this->addHidden('imports', $imports);
$this->setElementValue('imports', $imports);
$this->addNameElement();
$name = $this->getSentOrObjectValue('object_name');
if (empty($name)) {
$this->setElementValue('object_name', $imports);
}
$this->groupMainProperties()
->setButtons();
}
/**
* @return $this
*/
protected function groupMainProperties()
{
$elements = array(
$elements = [
'object_type',
'imports',
'object_name',
);
];
$this->addDisplayGroup($elements, 'object_definition', array(
'decorators' => array(
$this->addDisplayGroup($elements, 'object_definition', [
'decorators' => [
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
['HtmlTag', ['tag' => 'dl']],
'Fieldset',
),
'order' => 20,
],
'order' => 20,
'legend' => $this->translate('Main properties')
));
]);
return $this;
}
@ -82,93 +96,81 @@ class IcingaAddServiceToMultipleHostsForm extends DirectorObjectForm
return $this;
}
$this->addElement('select', 'imports', array(
$this->addElement('select', 'imports', [
'label' => $this->translate('Service'),
'description' => $this->translate(
'Choose a service template'
),
'required' => true,
'multiOptions' => $this->optionallyAddFromEnum($enum),
// TODO -> 'value' => $this->presetImports,
'multiOptions' => $this->optionalEnum($enum),
'class' => 'autosubmit'
));
]);
return $this;
}
protected function enumServiceTemplates()
{
$tpl = $this->getDb()->enumIcingaTemplates('service');
if (empty($tpl)) {
return array();
}
$tpl = array_combine($tpl, $tpl);
return $tpl;
}
protected function setupHostRelatedElements()
{
$this->addHidden('host_id', $this->host->id);
$this->addHidden('object_type', 'object');
$this->addImportsElement();
$imports = $this->getSentOrObjectValue('imports');
if ($this->hasBeenSent()) {
$imports = $this->getElement('imports')->setValue($imports)->getValue();
}
if ($this->isNew() && empty($imports)) {
$this->groupMainProperties();
return;
}
if ($this->hasBeenSent()) {
$name = $this->getSentOrObjectValue('object_name');
if (!strlen($name)) {
$this->setElementValue('object_name', end($imports));
$this->object->object_name = end($imports);
}
}
return array_combine($tpl, $tpl);
}
/**
* @param IcingaHost[] $hosts
* @return $this
*/
public function setHosts(array $hosts)
{
$this->hosts = $hosts;
return $this;
}
/**
* @param IcingaHost $host
* @return $this
*/
public function setHost(IcingaHost $host)
{
$this->host = $host;
return $this;
}
protected function addNameElement()
{
$this->addElement('text', 'object_name', array(
$this->addElement('text', 'object_name', [
'label' => $this->translate('Name'),
'required' => true,
'description' => $this->translate(
'Name for the Icinga service you are going to create'
)
));
]);
return $this;
}
public function onSuccess()
{
$vars = array();
foreach ($this->object->vars() as $key => $var) {
$vars[$key] = $var->getValue();
if ($this->host !== null) {
$this->object->set('host_id', $this->host->get('id'));
parent::onSuccess();
return;
}
$o = $this->object;
$plain = $this->object->toPlainObject();
$db = $this->object->getConnection();
foreach ($this->hosts as $host) {
$service = IcingaService::fromPlainObject(
$o->toPlainObject(),
$o->getConnection()
)->set('host_id', $host->get('id'));
$service->store();
IcingaService::fromPlainObject($plain, $db)
->set('host_id', $host->get('id'))
->store();
}
$this->redirectOnSuccess('Gogogo');
$msg = sprintf(
$this->translate('The service "%s" has been added to %d hosts'),
$this->object->getObjectName(),
count($this->hosts)
);
$this->redirectOnSuccess($msg);
}
}