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

View File

@ -1,37 +1,12 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}} // {{{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}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Backend; namespace Icinga\Module\Monitoring\Form\Config\Backend;
use \Zend_Config; use Zend_Config;
use \Icinga\Web\Form; use Icinga\Web\Form;
use \Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
/** /**
@ -44,32 +19,14 @@ class EditBackendForm extends Form
* *
* @var array * @var array
*/ */
private $resources; protected $resources;
/** /**
* The Backend configuration to use for populating the form * The Backend configuration to use for populating the form
* *
* @var Zend_Config * @var Zend_Config
*/ */
private $backend; protected $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'
)
);
/** /**
* Set the configuration to be used for initial population of the form * 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 * Return a list of all resources of the given type ready to be used as content for a select input
* attribute in a Zend_Form_Element_Select object
* *
* @return array * @param string $type The type of resources to return
*
* @return array
*/ */
private function getDatabaseResources() protected function getResourcesByType($type)
{ {
$backends = array(); $backends = array();
foreach ($this->getResources() as $resname => $resource) { foreach ($this->getResources() as $name => $resource) {
if ($resource['type'] !== 'db') { if ($resource['type'] === $type) {
continue; $backends[$name] = $name;
} }
$backends[$resname] = $resname;
} }
return $backends; 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 * Create this form
* *
@ -230,23 +87,45 @@ class EditBackendForm extends Form
*/ */
public function create() public function create()
{ {
$this->addTypeSelectionBox(); $backendType = $this->getRequest()->getParam('backend_type', $this->backend->type);
switch ($this->getRequest()->getParam('backend_type', $this->backend->type)) {
case 'ido': $this->addElement(
$this->addIdoBackendForm(); 'select',
break; 'backend_type',
case 'statusdat': array(
$this->addStatusDatForm(); 'label' => 'Backend Type',
break; 'value' => $this->backend->type,
case 'livestatus': 'required' => true,
$this->addLivestatusForm(); 'helptext' => 'The data source used for retrieving monitoring information',
break; 'multiOptions' => array(
default: 'ido' => 'IDO Backend',
$this->removeElement('backend_type'); 'statusdat' => 'Status.dat',
$this->addNote('Unknown Backend Type "' . $this->backend->type. '"'); 'livestatus' => 'Livestatus'
return; )
} )
$this->addDisableButton(); );
$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'); $this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
} }
@ -258,16 +137,12 @@ class EditBackendForm extends Form
public function getConfig() public function getConfig()
{ {
$values = $this->getValues(); $values = $this->getValues();
$type = $values['backend_type']; return new Zend_Config(
array(
$map = $this->propertyMapping[$type]; 'type' => $values['backend_type'],
$result = array( 'disabled' => $values['backend_disable'],
'type' => $type, 'resource' => $values['backend_resource']
'disabled' => $values['backend_disable'] )
); );
foreach ($map as $formKey => $mappedKey) {
$result[$mappedKey] = $values[$formKey];
}
return new Zend_Config($result);
} }
} }