Adjust Monitoring\InstanceForm to use handleRequest() & Co.

refs #5525
This commit is contained in:
Johannes Meyer 2014-09-04 08:47:16 +02:00
parent 49562e75d7
commit 9d66cc9023
6 changed files with 348 additions and 222 deletions

View File

@ -7,7 +7,7 @@ use Icinga\Web\Controller\ModuleActionController;
use Icinga\Web\Notification;
use Icinga\Form\ConfirmRemovalForm;
use Icinga\Module\Monitoring\Form\Config\BackendForm;
use Icinga\Module\Monitoring\Form\Config\InstanceForm;
use Icinga\Module\Monitoring\Form\Config\InstanceConfigForm;
use Icinga\Module\Monitoring\Form\Config\SecurityConfigForm;
use Icinga\Exception\NotReadableError;
@ -134,29 +134,31 @@ class Monitoring_ConfigController extends ModuleActionController
*/
public function removeinstanceAction()
{
$instance = $this->getParam('instance');
$instancesConfig = $this->Config('instances')->toArray();
if (false === array_key_exists($instance, $instancesConfig)) {
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
Notification::error(sprintf($this->translate('Cannot remove "%s". Instance not found.'), $instance));
$this->redirectNow('monitoring/config');
}
$config = $this->Config('instances');
$form = new ConfirmRemovalForm(array(
'onSuccess' => function ($request) use ($config) {
$instanceName = $request->getQuery('instance');
$configForm = new InstanceConfigForm();
$configForm->setConfig($config);
$form = new ConfirmRemovalForm();
$request = $this->getRequest();
if ($request->isPost() && $form->isValid($request->getPost())) {
unset($instancesConfig[$instance]);
if ($this->writeConfiguration($instancesConfig, 'instances')) {
Notification::success(sprintf($this->translate('Instance "%s" successfully removed.'), $instance));
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
try {
$configForm->remove($instanceName);
} catch (InvalidArgumentException $e) {
Notification::error($e->getMessage());
return;
}
if ($configForm->save()) {
Notification::success(sprintf(t('Instance "%s" successfully removed.'), $instanceName));
} else {
return false;
}
}
));
$form->setRedirectUrl('monitoring/config');
$form->handleRequest();
$this->view->form = $form;
$this->view->name = $instance;
}
/**
@ -164,36 +166,10 @@ class Monitoring_ConfigController extends ModuleActionController
*/
public function editinstanceAction()
{
$instance = $this->getParam('instance');
$instancesConfig = $this->Config('instances')->toArray();
if (false === array_key_exists($instance, $instancesConfig)) {
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
Notification::error(sprintf($this->translate('Cannot edit "%s". Instance not found.'), $instance));
$this->redirectNow('monitoring/config');
}
$form = new InstanceForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
list($newName, $config) = $form->getInstanceConfig();
if ($newName !== $instance) {
unset($instancesConfig[$instance]); // We can safely use unset as all values are part of the form
}
$instancesConfig[$newName] = $config;
if ($this->writeConfiguration($instancesConfig, 'instances')) {
Notification::success(sprintf($this->translate('Instance "%s" successfully modified.'), $instance));
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
}
}
} else {
$form->setInstanceConfig($instance, $instancesConfig[$instance]);
}
$form = new InstanceConfigForm();
$form->setConfig($this->Config('instances'));
$form->setRedirectUrl('monitoring/config');
$form->handleRequest();
$this->view->form = $form;
}
@ -203,20 +179,10 @@ class Monitoring_ConfigController extends ModuleActionController
*/
public function createinstanceAction()
{
$form = new InstanceForm();
$request = $this->getRequest();
if ($request->isPost() && $form->isValid($request->getPost())) {
list($name, $config) = $form->getInstanceConfig();
$instancesConfig = $this->Config('instances')->toArray();
$instancesConfig[$name] = $config;
if ($this->writeConfiguration($instancesConfig, 'instances')) {
Notification::success(sprintf($this->translate('Instance "%s" created successfully.'), $name));
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
}
}
$form = new InstanceConfigForm();
$form->setConfig($this->Config('instances'));
$form->setRedirectUrl('monitoring/config');
$form->handleRequest();
$this->view->form = $form;
}

View File

@ -0,0 +1,37 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Instance;
use Icinga\Web\Form;
class LocalInstanceForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_monitoring_instance_local');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$this->addElement(
'text',
'path',
array(
'required' => true,
'label' => t('Local Filepath'),
'value' => '/usr/local/icinga/var/rw/icinga.cmd',
'description' => t('The file path where the icinga commandpipe can be found')
)
);
return $this;
}
}

View File

@ -0,0 +1,68 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Instance;
use Icinga\Web\Form;
use Icinga\Web\Form\Element\Number;
class RemoteInstanceForm extends Form
{
public function init()
{
$this->setName('form_config_monitoring_instance_remote');
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$this->addElement(
'text',
'host',
array(
'required' => true,
'label' => t('Remote Host'),
'description' => t(
'Enter the hostname or address of the machine on which the icinga instance is running'
)
)
);
$this->addElement(
new Number(
array(
'required' => true,
'name' => 'port',
'label' => t('Remote SSH Port'),
'description' => t('Enter the ssh port to use for connecting to the remote icinga instance'),
'value' => 22
)
)
);
$this->addElement(
'text',
'user',
array(
'required' => true,
'label' => t('Remote SSH User'),
'description' => t(
'Enter the username to use for connecting to the remote machine or leave blank for default'
)
)
);
$this->addElement(
'text',
'path',
array(
'required' => true,
'label' => t('Remote Filepath'),
'value' => '/usr/local/icinga/var/rw/icinga.cmd',
'description' => t('The file path where the icinga commandpipe can be found')
)
);
return $this;
}
}

View File

@ -0,0 +1,211 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config;
use InvalidArgumentException;
use Icinga\Web\Request;
use Icinga\Form\ConfigForm;
use Icinga\Web\Notification;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Monitoring\Form\Config\Instance\LocalInstanceForm;
use Icinga\Module\Monitoring\Form\Config\Instance\RemoteInstanceForm;
/**
* Form for modifying/creating monitoring instances
*/
class InstanceConfigForm extends ConfigForm
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_monitoring_instance');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* Return a form object for the given instance type
*
* @param string $type The instance type for which to return a form
*
* @return Form
*
* @throws InvalidArgumentException In case the given instance type is invalid
*/
public function getInstanceForm($type)
{
if ($type === 'local') {
return new LocalInstanceForm();
} elseif ($type === 'remote') {
return new RemoteInstanceForm();
} else {
throw new InvalidArgumentException(sprintf(t('Invalid instance type "%s" provided'), $type));
}
}
/**
* Add a new instance
*
* The resource to add is identified by the array-key `name'.
*
* @param array $values The values to extend the configuration with
*
* @return self
*
* @throws InvalidArgumentException In case the resource already exists
*/
public function add(array $values)
{
$name = isset($values['name']) ? $values['name'] : '';
if (! $name) {
throw new InvalidArgumentException(t('Instance name missing'));
} elseif ($this->config->get($name) !== null) {
throw new InvalidArgumentException(t('Instance already exists'));
}
unset($values['name']);
$this->config->{$name} = $values;
return $this;
}
/**
* Edit an existing instance
*
* @param string $name The name of the resource to edit
* @param array $values The values to edit the configuration with
*
* @return array The edited resource configuration
*
* @throws InvalidArgumentException In case the resource name is missing or invalid
*/
public function edit($name, array $values)
{
if (! $name) {
throw new InvalidArgumentException(t('Old instance name missing'));
} elseif (! ($newName = isset($values['name']) ? $values['name'] : '')) {
throw new InvalidArgumentException(t('New instance name missing'));
} elseif (! ($instanceConfig = $this->config->get($name))) {
throw new InvalidArgumentException(t('Unknown instance name provided'));
}
unset($values['name']);
unset($this->config->{$name});
$this->config->{$newName} = array_merge($instanceConfig->toArray(), $values);
return $this->config->{$newName};
}
/**
* Remove a instance
*
* @param string $name The name of the resource to remove
*
* @return array The removed resource confguration
*
* @throws InvalidArgumentException In case the resource name is missing or invalid
*/
public function remove($name)
{
if (! $name) {
throw new InvalidArgumentException(t('Instance name missing'));
} elseif (! ($instanceConfig = $this->config->get($name))) {
throw new InvalidArgumentException(t('Unknown instance name provided'));
}
unset($this->config->{$name});
return $instanceConfig;
}
/**
* @see Form::onSuccess()
*/
public function onSuccess(Request $request)
{
$instanceName = $request->getQuery('instance');
try {
if ($instanceName === null) { // create new instance
$this->add($this->getValues());
$message = t('Instance "%s" created successfully.');
} else { // edit existing instance
$this->edit($instanceName, $this->getValues());
$message = t('Instance "%s" edited successfully.');
}
} catch (InvalidArgumentException $e) {
Notification::error($e->getMessage());
return;
}
if ($this->save()) {
Notification::success(sprintf($message, $this->getElement('name')->getValue()));
} else {
return false;
}
}
/**
* @see Form::onRequest()
*
* @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(t('Instance name missing'));
} elseif (false === isset($this->config->{$instanceName})) {
throw new ConfigurationError(t('Unknown instance name provided'));
}
$instanceConfig = $this->config->{$instanceName}->toArray();
$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(
'required' => true,
'label' => t('Instance Name')
)
);
$this->addElement(
'select',
'type',
array(
'required' => true,
'ignore' => true,
'autosubmit' => true,
'label' => t('Instance Type'),
'description' => t(
'When configuring a remote host, you need to setup passwordless key authentication'
),
'multiOptions' => array(
'local' => t('Local Command Pipe'),
'remote' => t('Remote Command Pipe')
),
'value' => $instanceType
)
);
$this->addElements($this->getInstanceForm($instanceType)->createElements($formData)->getElements());
return $this;
}
}

View File

@ -1,156 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config;
use Icinga\Web\Form;
use Icinga\Web\Form\Element\Number;
use Icinga\Web\Form\Decorator\HelpText;
use Icinga\Web\Form\Decorator\ElementWrapper;
/**
* Form for modifying/creating monitoring instances
*/
class InstanceForm extends Form
{
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_monitoring_instances');
$this->setSubmitLabel(t('Save Changes'));
}
/**
* @see Form::createElements()
*/
public function createElements(array $formData)
{
$elements = array(
$this->createElement(
'text',
'name',
array(
'required' => true,
'label' => t('Instance Name')
)
),
$this->createElement(
'select',
'type',
array(
'required' => true,
'ignore' => true,
'label' => t('Instance Type'),
'class' => 'autosubmit',
'helptext' => t(
'When configuring a remote host, you need to setup passwordless key authentication'
),
'multiOptions' => array(
'local' => t('Local Command Pipe'),
'remote' => t('Remote Command Pipe')
)
)
)
);
if (isset($formData['type']) && $formData['type'] === 'remote') {
$elements[] = $this->createElement(
'text',
'host',
array(
'required' => true,
'label' => t('Remote Host'),
'helptext' => t(
'Enter the hostname or address of the machine on which the icinga instance is running'
)
)
);
$elements[] = new Number(
array(
'required' => true,
'name' => 'port',
'label' => t('Remote SSH Port'),
'helptext' => t('Enter the ssh port to use for connecting to the remote icinga instance'),
'value' => 22,
'decorators' => array( // The order is important!
'ViewHelper',
'Errors',
new ElementWrapper(),
new HelpText()
)
)
);
$elements[] = $this->createElement(
'text',
'user',
array(
'required' => true,
'label' => t('Remote SSH User'),
'helptext' => t(
'Enter the username to use for connecting to the remote machine or leave blank for default'
)
)
);
} else {
// TODO(5967,5525): Without this element, switching the type to "local" causes
// the form to be processed as it's complete in that case.
$elements[] = $this->createElement(
'hidden',
'hitchhiker',
array(
'required' => true,
'ignore' => true,
'value' => 'Arthur'
)
);
}
$elements[] = $this->createElement(
'text',
'path',
array(
'required' => true,
'label' => t('Pipe Filepath'),
'value' => '/usr/local/icinga/var/rw/icinga.cmd',
'helptext' => t('The file path where the icinga commandpipe can be found')
)
);
return $elements;
}
/**
* Return the instance configuration values and its name
*
* The first value is the name and the second one the values as array.
*
* @return array
*/
public function getInstanceConfig()
{
$values = $this->getValues();
$name = $values['name'];
unset($values['name']);
return array($name, $values);
}
/**
* Populate the form with the given configuration values
*
* @param string $name The name of the instance
* @param array $config The configuration values
*/
public function setInstanceConfig($name, array $config)
{
$config['name'] = $name;
if (isset($config['host'])) {
// Necessary as we have no config directive for setting the instance's type
$config['type'] = 'remote';
}
$this->populate($config);
}
}

View File

@ -1,4 +1,4 @@
<h4><?= $this->translate('Remove Existing Instance'); ?></h4>
<p>Are you sure you want to remove the instance <?= $this->escape($name); ?>?</p>
<p><i class="icinga-icon-warning"></i> If you have still any environments or views referring to this instance, you won't be able to send commands anymore after deletion.</p>
<p><?= $this->translate('Are you sure you want to remove this instance?'); ?></p>
<p><?= $this->translate('If you have still any environments or views referring to this instance, you won\'t be able to send commands anymore after deletion.'); ?></p>
<?= $form; ?>