IcingaServiceForm: set host, not host_id

This allows to create services on hosts which have been created in a branch
This commit is contained in:
Thomas Gelf 2022-02-06 13:49:40 +01:00
parent 269637ce9f
commit ef360809e6

View File

@ -12,6 +12,7 @@ use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet; use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Table\ObjectsTableHost;
use ipl\Html\Html; use ipl\Html\Html;
use gipfl\IcingaWeb2\Link; use gipfl\IcingaWeb2\Link;
use RuntimeException; use RuntimeException;
@ -474,7 +475,7 @@ class IcingaServiceForm extends DirectorObjectForm
*/ */
protected function setupHostRelatedElements() protected function setupHostRelatedElements()
{ {
$this->addHidden('host_id', $this->host->id); $this->addHidden('host', $this->host->getObjectName());
$this->addHidden('object_type', 'object'); $this->addHidden('object_type', 'object');
$this->addImportsElement(); $this->addImportsElement();
$imports = $this->getSentOrObjectValue('imports'); $imports = $this->getSentOrObjectValue('imports');
@ -578,14 +579,14 @@ class IcingaServiceForm extends DirectorObjectForm
protected function addHostObjectElement() protected function addHostObjectElement()
{ {
if ($this->isObject()) { if ($this->isObject()) {
$this->addElement('select', 'host_id', array( $this->addElement('select', 'host', [
'label' => $this->translate('Host'), 'label' => $this->translate('Host'),
'required' => true, 'required' => true,
'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()), 'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()),
'description' => $this->translate( 'description' => $this->translate(
'Choose the host this single service should be assigned to' 'Choose the host this single service should be assigned to'
) )
)); ]);
} }
return $this; return $this;
@ -687,10 +688,36 @@ class IcingaServiceForm extends DirectorObjectForm
protected function enumHostsAndTemplates() protected function enumHostsAndTemplates()
{ {
return array( if ($this->branch && $this->branch->isBranch()) {
$this->translate('Templates') => $this->db->enumHostTemplates(), return $this->enumHosts();
$this->translate('Hosts') => $this->db->enumHosts(), }
);
return [
$this->translate('Templates') => $this->enumHostTemplates(),
$this->translate('Hosts') => $this->enumHosts(),
];
}
protected function enumHostTemplates()
{
$names = array_values($this->db->enumHostTemplates());
return array_combine($names, $names);
}
protected function enumHosts()
{
$db = $this->db->getDbAdapter();
$table = new ObjectsTableHost($this->db);
$table->setAuth($this->getAuth());
if ($this->branch && $this->branch->isBranch()) {
$table->setBranchUuid($this->branch->getUuid());
}
$result = [];
foreach ($db->fetchAll($table->getQuery()) as $row) {
$result[$row->object_name] = $row->object_name;
}
return $result;
} }
protected function enumServicegroups() protected function enumServicegroups()