Fix monitoring backend configuration not showing resource selection fields

fixes #6448
This commit is contained in:
Johannes Meyer 2014-07-10 15:50:52 +02:00
parent 53f3d74103
commit b4dc0f38b3
1 changed files with 59 additions and 184 deletions

View File

@ -1,37 +1,12 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Backend;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Application\Icinga;
use Zend_Config;
use Icinga\Web\Form;
use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
/**
@ -44,32 +19,14 @@ class EditBackendForm extends Form
*
* @var array
*/
private $resources;
protected $resources;
/**
* The Backend configuration to use for populating the form
*
* @var Zend_Config
*/
private $backend;
/**
* Mapping from form fields to configuration fields
*
* @var array
*/
private $propertyMapping = array(
'livestatus' => array(
'backend_livestatus_socket' => 'socket'
),
'ido' => array(
'backend_ido_resource' => 'resource'
),
'statusdat' => array(
'backend_statusdat_statusfile' => 'status_file',
'backend_statusdat_objectfile' => 'object_file'
)
);
protected $backend;
/**
* Set the configuration to be used for initial population of the form
@ -105,124 +62,24 @@ class EditBackendForm extends Form
}
/**
* Return a list of all database resource ready to be used as the multiOptions
* attribute in a Zend_Form_Element_Select object
* Return a list of all resources of the given type ready to be used as content for a select input
*
* @return array
* @param string $type The type of resources to return
*
* @return array
*/
private function getDatabaseResources()
protected function getResourcesByType($type)
{
$backends = array();
foreach ($this->getResources() as $resname => $resource) {
if ($resource['type'] !== 'db') {
continue;
foreach ($this->getResources() as $name => $resource) {
if ($resource['type'] === $type) {
$backends[$name] = $name;
}
$backends[$resname] = $resname;
}
return $backends;
}
/**
* Add form elements used for setting IDO backend parameters
*/
private function addIdoBackendForm()
{
$this->addElement(
'select',
'backend_ido_resource',
array(
'label' => 'IDO Connection',
'value' => $this->backend->resource,
'required' => true,
'multiOptions' => $this->getDatabaseResources(),
'helptext' => 'The database to use for querying monitoring data',
)
);
}
/**
* Add form elements used for setting status.dat backend parameters
*/
private function addStatusDatForm()
{
$this->addElement(
'text',
'backend_statusdat_statusfile',
array (
'label' => 'Status.dat File',
'value' => $this->backend->status_file,
'required' => true,
'helptext' => 'Location of your icinga status.dat file'
)
);
$this->addElement(
'text',
'backend_statusdat_objectfile',
array (
'label' => 'Objects.cache File',
'value' => $this->backend->status_file,
'required' => true,
'helptext' => 'Location of your icinga objects.cache file'
)
);
}
/**
* Add form elements used for setting Livestatus parameters
*/
private function addLivestatusForm()
{
$this->addElement(
'text',
'backend_livestatus_socket',
array(
'label' => 'Livestatus Socket Location',
'required' => true,
'helptext' => 'The path to your livestatus socket used for querying monitoring data',
'value' => $this->backend->socket,
)
);
}
/**
* Add a checkbox to disable this backends
*/
private function addDisableButton()
{
$this->addElement(
'checkbox',
'backend_disable',
array(
'label' => 'Disable This Backend',
'required' => true,
'value' => $this->backend->disabled
)
);
}
/**
* Add a select box for choosing the type to use for this backend
*/
private function addTypeSelectionBox()
{
$this->addElement(
'select',
'backend_type',
array(
'label' => 'Backend Type',
'value' => $this->backend->type,
'required' => true,
'helptext' => 'The data source used for retrieving monitoring information',
'multiOptions' => array(
'ido' => 'IDO Backend',
'statusdat' => 'Status.dat',
'livestatus' => 'Livestatus'
)
)
);
$this->enableAutoSubmit(array('backend_type'));
}
/**
* Create this form
*
@ -230,23 +87,45 @@ class EditBackendForm extends Form
*/
public function create()
{
$this->addTypeSelectionBox();
switch ($this->getRequest()->getParam('backend_type', $this->backend->type)) {
case 'ido':
$this->addIdoBackendForm();
break;
case 'statusdat':
$this->addStatusDatForm();
break;
case 'livestatus':
$this->addLivestatusForm();
break;
default:
$this->removeElement('backend_type');
$this->addNote('Unknown Backend Type "' . $this->backend->type. '"');
return;
}
$this->addDisableButton();
$backendType = $this->getRequest()->getParam('backend_type', $this->backend->type);
$this->addElement(
'select',
'backend_type',
array(
'label' => 'Backend Type',
'value' => $this->backend->type,
'required' => true,
'helptext' => 'The data source used for retrieving monitoring information',
'multiOptions' => array(
'ido' => 'IDO Backend',
'statusdat' => 'Status.dat',
'livestatus' => 'Livestatus'
)
)
);
$this->addElement(
'select',
'backend_resource',
array(
'label' => 'Resource',
'value' => $this->backend->resource,
'required' => true,
'multiOptions' => $this->getResourcesByType($backendType === 'ido' ? 'db' : $backendType),
'helptext' => 'The resource to use'
)
);
$this->addElement(
'checkbox',
'backend_disable',
array(
'label' => 'Disable This Backend',
'required' => true,
'value' => $this->backend->disabled
)
);
$this->enableAutoSubmit(array('backend_type'));
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
}
@ -258,16 +137,12 @@ class EditBackendForm extends Form
public function getConfig()
{
$values = $this->getValues();
$type = $values['backend_type'];
$map = $this->propertyMapping[$type];
$result = array(
'type' => $type,
'disabled' => $values['backend_disable']
return new Zend_Config(
array(
'type' => $values['backend_type'],
'disabled' => $values['backend_disable'],
'resource' => $values['backend_resource']
)
);
foreach ($map as $formKey => $mappedKey) {
$result[$mappedKey] = $values[$formKey];
}
return new Zend_Config($result);
}
}