DbSelectorForm: allow to switch Director database

fixes #1498
This commit is contained in:
Thomas Gelf 2018-06-07 23:50:31 +02:00
parent 885f76715a
commit aaaa8aed0a
3 changed files with 63 additions and 0 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Dashboard\Dashboard;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Form\DbSelectorForm;
class DashboardController extends ActionController
{
@ -11,6 +12,17 @@ class DashboardController extends ActionController
{
}
protected function addDbSelection()
{
$form = new DbSelectorForm($this->Window(), $this->listAllowedDbResourceNames());
$this->content()->add($form);
$form->handleRequest($this->getRequest());
}
/**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\Http\HttpNotFoundException
*/
public function indexAction()
{
if ($this->getRequest()->isGet()) {
@ -20,6 +32,9 @@ class DashboardController extends ActionController
$mainDashboards = ['Objects', 'Alerts', 'Automation', 'Deployment', 'Data'];
$this->setTitle($this->translate('Icinga Director - Main Dashboard'));
$names = $this->params->getValues('name', $mainDashboards);
if (! $this->params->has('name')) {
$this->addDbSelection();
}
if (count($names) === 1) {
$name = $names[0];
$dashboard = Dashboard::loadByName($name, $this->db());

View File

@ -24,6 +24,7 @@ before switching to a new version.
* FEATURE: it's now possible to blacklist inherited or applied Services on
single hosts (#907)
* FEATURE: timestamped startup log rendering for upcoming Icinga v2.9.0 (#1478)
* FEATURE: allow to switch between multiple Director databases (#1498)
### User Interface
* FEATURE: Admins have now access to JSON download links in many places

View File

@ -0,0 +1,47 @@
<?php
namespace Icinga\Module\Director\Web\Form;
use dipl\Html\Form;
use Icinga\Web\Window;
class DbSelectorForm extends Form
{
protected $defaultAttributes = [
'class' => 'db-selector'
];
protected $allowedNames;
/** @var Window */
protected $window;
public function __construct(Window $window, $allowedNames)
{
$this->window = $window;
$this->allowedNames = $allowedNames;
}
protected function assemble()
{
$this->addElement('db_resource', 'select', [
'options' => $this->allowedNames,
'class' => 'autosubmit',
'value' => $this->getSession()->get('db_resource')
]);
}
public function onSuccess()
{
$this->getSession()->set('db_resource', $this->getValue('db_resource'));
$this->redirectOnSuccess();
}
/**
* @return \Icinga\Web\Session\SessionNamespace
*/
protected function getSession()
{
return $this->window->getSessionNamespace('director');
}
}