Merge EditInstanceForm with CreateInstanceForm and rename it

As with the backend form, a user might like to change the name of an
instance. The new form and its actions are also adjusted to suit the new form interface.

refs #5525
This commit is contained in:
Johannes Meyer 2014-08-11 14:49:42 +02:00
parent a627571b87
commit be14844fa8
7 changed files with 224 additions and 265 deletions

View File

@ -9,8 +9,7 @@ use Icinga\Web\Controller\ModuleActionController;
use Icinga\Web\Notification;
use Icinga\Form\Config\ConfirmRemovalForm;
use Icinga\Module\Monitoring\Form\Config\BackendForm;
use Icinga\Module\Monitoring\Form\Config\Instance\EditInstanceForm;
use Icinga\Module\Monitoring\Form\Config\Instance\CreateInstanceForm;
use Icinga\Module\Monitoring\Form\Config\InstanceForm;
use Icinga\Exception\NotReadableError;
/**
@ -95,6 +94,7 @@ class Monitoring_ConfigController extends ModuleActionController
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
}
}
@ -123,6 +123,7 @@ class Monitoring_ConfigController extends ModuleActionController
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
}
}
@ -135,26 +136,24 @@ class Monitoring_ConfigController extends ModuleActionController
public function removeinstanceAction()
{
$instance = $this->getParam('instance');
if (!$this->isExistingInstance($instance)) {
$this->view->error = 'Unknown instance ' . $instance;
return;
$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');
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('instance', $instance);
if ($form->isSubmittedAndValid()) {
$configArray = $this->Config('instances')->toArray();
unset($configArray[$instance]);
if ($this->writeConfiguration(new Zend_Config($configArray), 'instances')) {
Notification::success('Instance "' . $instance . '" Removed');
$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');
return;
}
return;
}
$this->view->form = $form;
@ -167,24 +166,36 @@ class Monitoring_ConfigController extends ModuleActionController
public function editinstanceAction()
{
$instance = $this->getParam('instance');
if (!$this->isExistingInstance($instance)) {
$this->view->error = 'Unknown instance ' . htmlentities($instance);
return;
$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 EditInstanceForm();
$form->setInstanceConfiguration($this->Config('instances')->get($instance));
$form->setRequest($this->getRequest());
if ($form->isSubmittedAndValid()) {
$instanceConfig = $this->Config('instances')->toArray();
$instanceConfig[$instance] = $form->getConfig();
if ($this->writeConfiguration(new Zend_Config($instanceConfig), 'instances')) {
Notification::success('Instance Modified');
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
$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]);
}
$this->view->form = $form;
}
@ -193,26 +204,22 @@ class Monitoring_ConfigController extends ModuleActionController
*/
public function createinstanceAction()
{
$form = new CreateInstanceForm();
$form->setRequest($this->getRequest());
if ($form->isSubmittedAndValid()) {
$instanceConfig = $this->Config('instances');
if ($instanceConfig === null) {
$instanceConfig = array();
} else {
$instanceConfig = $instanceConfig->toArray();
}
$instanceConfig[$form->getInstanceName()] = $form->getConfig()->toArray();
if ($this->writeConfiguration(new Zend_Config($instanceConfig), 'instances')) {
Notification::success('Instance Creation Succeeded');
$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;
}
return;
}
$this->view->form = $form;
$this->render('editinstance');
}
/**

View File

@ -1,46 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Instance;
use \Icinga\Web\Form;
use \Zend_Config;
/**
* Form for creating new instances
*
* @see EditInstanceForm
*/
class CreateInstanceForm extends EditInstanceForm
{
/**
* Create the form elements
*
* @see EditInstanceForm::create()
*/
public function create()
{
$this->setInstanceConfiguration(new Zend_Config(array()));
$this->addElement(
'text',
'instance_name',
array(
'label' => 'Instance Name',
'helptext' => 'Please enter the name for the instance to create'
)
);
parent::create();
}
/**
* Return the name of the instance to be created
*
* @return string The name of the instance as entered in the form
*/
public function getInstanceName()
{
return $this->getValue('instance_name');
}
}

View File

@ -1,148 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Config\Instance;
use \Zend_Config;
use \Icinga\Web\Form;
/**
* Form for editing existing instances
*/
class EditInstanceForm extends Form
{
/**
* The instance to edit
*
* @var Zend_Config
*/
private $instance;
/**
* The type of the instance
*
* 'local' when no host is given, otherwise 'remote'
*
* @var string
*/
private $instanceType = 'local';
/**
* Set instance configuration to be used for initial form population
*
* @param Zend_Config $config
*/
public function setInstanceConfiguration($config)
{
$this->instance = $config;
if (isset($this->instance->host)) {
$this->instanceType = 'remote';
}
}
/**
* Add a form field for selecting the command pipe type (local or remote)
*/
private function addTypeSelection()
{
$this->addElement(
'select',
'instance_type',
array(
'value' => $this->instanceType,
'multiOptions' => array(
'local' => 'Local Command Pipe',
'remote' => 'Remote Command Pipe'
)
)
);
$this->enableAutoSubmit(array('instance_type'));
}
/**
* Add form elements for remote instance
*/
private function addRemoteInstanceForm()
{
$this->addNote('When configuring a remote host, you need to setup passwordless key authentication');
$this->addElement(
'text',
'instance_remote_host',
array(
'label' => 'Remote Host',
'required' => true,
'value' => $this->instance->host,
'helptext' => 'Enter the hostname or address of the machine on which the icinga instance is running'
)
);
$this->addElement(
'text',
'instance_remote_port',
array(
'label' => 'Remote SSH Port',
'required' => true,
'value' => $this->instance->get('port', 22),
'helptext' => 'Enter the ssh port to use for connecting to the remote icigna instance'
)
);
$this->addElement(
'text',
'instance_remote_user',
array(
'label' => 'Remote SSH User',
'value' => $this->instance->user,
'helptext' => 'Enter the username to use for connecting '
. 'to the remote machine or leave blank for default'
)
);
}
/**
* Create this form
*
* @see Icinga\Web\Form::create
*/
public function create()
{
$this->addTypeSelection();
if ($this->getRequest()->getParam('instance_type', $this->instanceType) === 'remote') {
$this->addRemoteInstanceForm();
}
$this->addElement(
'text',
'instance_path',
array(
'label' => 'Remote Pipe Filepath',
'required' => true,
'value' => $this->instance->get('path', '/usr/local/icinga/var/rw/icinga.cmd'),
'helptext' => 'The file path where the icinga commandpipe can be found'
)
);
$this->setSubmitLabel('{{SAVE_ICON}} Save');
}
/**
* Return the configuration set by this form
*
* @return Zend_Config The configuration set in this form
*/
public function getConfig()
{
$values = $this->getValues();
$config = array(
'path' => $values['instance_path']
);
if ($values['instance_type'] === 'remote') {
$config['host'] = $values['instance_remote_host'];
$config['port'] = $values['instance_remote_port'];
if (isset($values['instance_remote_user']) && $values['instance_remote_user'] != '') {
$config['user'] = $values['instance_remote_user'];
}
}
return new Zend_Config($config);
}
}

View File

@ -0,0 +1,165 @@
<?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
{
/**
* @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,
'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,
'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;
}
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'label' => t('Save Changes')
)
);
return $this;
}
/**
* 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']);
unset($values['type']);
unset($values['hitchhiker']);
unset($values['btn_submit']);
unset($values[$this->getTokenElementName()]);
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

@ -0,0 +1,2 @@
<h4><?= $this->translate('Add New Instance'); ?></h4>
<?= $form; ?>

View File

@ -1,14 +1,2 @@
<?php if (isset($this->name)): ?>
<h4><i class="icinga-icon-edit"></i> } Edit Instance Configuration for "<?= $this->escape($this->name) ?>"</h4>
<?php else: ?>
<h4><i class="icinga-icon-create"></i> Configure New Icinga Instance</h4>
<?php endif; ?>
<?php if ($this->error): ?>
<div class="alert alert-danger">
<?= $this->escape($this->error); ?>
</div>
<?php endif; ?>
<?= $this->form; ?>
<h4><?= $this->translate('Edit Existing Instance'); ?></h4>
<?= $form; ?>

View File

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