monitoring: Write transport directive to instances INI configuration

Further I replaced if-elseif blocks w/ switch when branching based on the value of a single parameter,
which would have to be implied otherwise by looking at all the conditions.
This commit is contained in:
Eric Lippmann 2014-10-29 13:36:09 +01:00
parent 8ee52f8737
commit 5fc1f85b76

View File

@ -5,12 +5,14 @@
namespace Icinga\Module\Monitoring\Form\Config; namespace Icinga\Module\Monitoring\Form\Config;
use InvalidArgumentException; use InvalidArgumentException;
use Icinga\Web\Request;
use Icinga\Form\ConfigForm;
use Icinga\Web\Notification;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Form\ConfigForm;
use Icinga\Module\Monitoring\Command\Transport\LocalCommandFile;
use Icinga\Module\Monitoring\Command\Transport\RemoteCommandFile;
use Icinga\Module\Monitoring\Form\Config\Instance\LocalInstanceForm; use Icinga\Module\Monitoring\Form\Config\Instance\LocalInstanceForm;
use Icinga\Module\Monitoring\Form\Config\Instance\RemoteInstanceForm; use Icinga\Module\Monitoring\Form\Config\Instance\RemoteInstanceForm;
use Icinga\Web\Notification;
use Icinga\Web\Request;
/** /**
* Form for modifying/creating monitoring instances * Form for modifying/creating monitoring instances
@ -18,7 +20,8 @@ use Icinga\Module\Monitoring\Form\Config\Instance\RemoteInstanceForm;
class InstanceConfigForm extends ConfigForm class InstanceConfigForm extends ConfigForm
{ {
/** /**
* Initialize this form * (non-PHPDoc)
* @see Form::init() For the method documentation.
*/ */
public function init() public function init()
{ {
@ -27,23 +30,29 @@ class InstanceConfigForm extends ConfigForm
} }
/** /**
* Return a form object for the given instance type * Get a form object for the given instance type
* *
* @param string $type The instance type for which to return a form * @param string $type The instance type for which to return a form
* *
* @return Form * @return LocalInstanceForm|RemoteInstanceForm
* *
* @throws InvalidArgumentException In case the given instance type is invalid * @throws InvalidArgumentException In case the given instance type is invalid
*/ */
public function getInstanceForm($type) public function getInstanceForm($type)
{ {
if ($type === 'local') { switch (strtolower($type)) {
return new LocalInstanceForm(); case LocalCommandFile::TRANSPORT:
} elseif ($type === 'remote') { $form = new LocalInstanceForm();
return new RemoteInstanceForm(); break;
} else { case RemoteCommandFile::TRANSPORT;
throw new InvalidArgumentException(sprintf(mt('monitoring', 'Invalid instance type "%s" provided'), $type)); $form = new RemoteInstanceForm();
break;
default:
throw new InvalidArgumentException(
sprintf(mt('monitoring', 'Invalid instance type "%s" given'), $type)
);
} }
return $form;
} }
/** /**
@ -62,7 +71,8 @@ class InstanceConfigForm extends ConfigForm
$name = isset($values['name']) ? $values['name'] : ''; $name = isset($values['name']) ? $values['name'] : '';
if (! $name) { if (! $name) {
throw new InvalidArgumentException(mt('monitoring', 'Instance name missing')); throw new InvalidArgumentException(mt('monitoring', 'Instance name missing'));
} elseif ($this->config->get($name) !== null) { }
if (isset($this->config->{$name})) {
throw new InvalidArgumentException(mt('monitoring', 'Instance already exists')); throw new InvalidArgumentException(mt('monitoring', 'Instance already exists'));
} }
@ -93,7 +103,7 @@ class InstanceConfigForm extends ConfigForm
unset($values['name']); unset($values['name']);
unset($this->config->{$name}); unset($this->config->{$name});
$this->config->{$newName} = array_merge($instanceConfig->toArray(), $values); $this->config->{$newName} = $values;
return $this->config->{$newName}; return $this->config->{$newName};
} }
@ -102,7 +112,7 @@ class InstanceConfigForm extends ConfigForm
* *
* @param string $name The name of the resource to remove * @param string $name The name of the resource to remove
* *
* @return array The removed resource confguration * @return array The removed resource configuration
* *
* @throws InvalidArgumentException In case the resource name is missing or invalid * @throws InvalidArgumentException In case the resource name is missing or invalid
*/ */
@ -119,12 +129,33 @@ class InstanceConfigForm extends ConfigForm
} }
/** /**
* @see Form::onSuccess() * @see Form::onRequest() For the method documentation.
* @throws ConfigurationError In case the instance name is missing or invalid
*/
public function onRequest(Request $request)
{
$instanceName = $request->getQuery('instance');
if ($instanceName !== null) {
if (! $instanceName) {
throw new ConfigurationError(mt('monitoring', 'Instance name missing'));
}
if (! isset($this->config->{$instanceName})) {
throw new ConfigurationError(mt('monitoring', 'Unknown instance name given'));
}
$instanceConfig = $this->config->{$instanceName}->toArray();
$instanceConfig['name'] = $instanceName;
$this->populate($instanceConfig);
}
}
/**
* (non-PHPDoc)
* @see Form::onSuccess() For the method documentation.
*/ */
public function onSuccess(Request $request) public function onSuccess(Request $request)
{ {
$instanceName = $request->getQuery('instance'); $instanceName = $request->getQuery('instance');
try { try {
if ($instanceName === null) { // create new instance if ($instanceName === null) { // create new instance
$this->add($this->getValues()); $this->add($this->getValues());
@ -146,63 +177,37 @@ class InstanceConfigForm extends ConfigForm
} }
/** /**
* @see Form::onRequest() * (non-PHPDoc)
* * @see Form::createElements() For the method documentation.
* @throws ConfigurationError In case the instance name is missing or invalid
*/ */
public function onRequest(Request $request) public function createElements(array $formData = array())
{ {
$instanceName = $request->getQuery('instance'); $instanceType = isset($formData['transport']) ? $formData['transport'] : LocalCommandFile::TRANSPORT;
if ($instanceName !== null) {
if (! $instanceName) {
throw new ConfigurationError(mt('monitoring', 'Instance name missing'));
} elseif (false === isset($this->config->{$instanceName})) {
throw new ConfigurationError(mt('monitoring', 'Unknown instance name provided'));
}
$instanceConfig = $this->config->{$instanceName}->toArray(); $this->addElements(array(
$instanceConfig['name'] = $instanceName;
if (isset($instanceConfig['host'])) {
// Necessary as we have no config directive for setting the instance's type
$instanceConfig['type'] = 'remote';
}
$this->populate($instanceConfig);
}
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$instanceType = isset($formData['type']) ? $formData['type'] : 'local';
$this->addElement(
'text',
'name',
array( array(
'required' => true, 'text',
'label' => mt('monitoring', 'Instance Name') 'name',
) array(
); 'required' => true,
$this->addElement( 'label' => mt('monitoring', 'Instance Name')
'select', )
'type', ),
array( array(
'required' => true, 'select',
'ignore' => true, 'transport',
'autosubmit' => true, array(
'label' => mt('monitoring', 'Instance Type'), 'required' => true,
'description' => mt('monitoring', 'autosubmit' => true,
'When configuring a remote host, you need to setup passwordless key authentication' 'label' => mt('monitoring', 'Instance Type'),
), 'multiOptions' => array(
'multiOptions' => array( LocalCommandFile::TRANSPORT => mt('monitoring', 'Local Command File'),
'local' => mt('monitoring', 'Local Command Pipe'), RemoteCommandFile::TRANSPORT => mt('monitoring', 'Remote Command File')
'remote' => mt('monitoring', 'Remote Command Pipe') ),
), 'value' => $instanceType
'value' => $instanceType )
) )
); ));
$this->addElements($this->getInstanceForm($instanceType)->createElements($formData)->getElements()); $this->addElements($this->getInstanceForm($instanceType)->createElements($formData)->getElements());
} }