2014-09-04 08:47:16 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-04 08:47:16 +02:00
|
|
|
|
2014-11-14 11:17:22 +01:00
|
|
|
namespace Icinga\Module\Monitoring\Forms\Config;
|
2014-09-04 08:47:16 +02:00
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-11-14 10:57:14 +01:00
|
|
|
use Icinga\Forms\ConfigForm;
|
2014-10-29 13:36:09 +01:00
|
|
|
use Icinga\Module\Monitoring\Command\Transport\LocalCommandFile;
|
|
|
|
use Icinga\Module\Monitoring\Command\Transport\RemoteCommandFile;
|
2014-11-14 11:17:22 +01:00
|
|
|
use Icinga\Module\Monitoring\Forms\Config\Instance\LocalInstanceForm;
|
|
|
|
use Icinga\Module\Monitoring\Forms\Config\Instance\RemoteInstanceForm;
|
2014-10-29 13:36:09 +01:00
|
|
|
use Icinga\Web\Notification;
|
2014-09-04 08:47:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form for modifying/creating monitoring instances
|
|
|
|
*/
|
|
|
|
class InstanceConfigForm extends ConfigForm
|
|
|
|
{
|
|
|
|
/**
|
2014-10-29 13:36:09 +01:00
|
|
|
* (non-PHPDoc)
|
|
|
|
* @see Form::init() For the method documentation.
|
2014-09-04 08:47:16 +02:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_monitoring_instance');
|
2015-01-19 13:47:01 +01:00
|
|
|
$this->setSubmitLabel($this->translate('Save Changes'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-29 13:36:09 +01:00
|
|
|
* Get a form object for the given instance type
|
2014-09-04 08:47:16 +02:00
|
|
|
*
|
2014-10-29 13:36:09 +01:00
|
|
|
* @param string $type The instance type for which to return a form
|
2014-09-04 08:47:16 +02:00
|
|
|
*
|
2014-10-29 13:36:09 +01:00
|
|
|
* @return LocalInstanceForm|RemoteInstanceForm
|
2014-09-04 08:47:16 +02:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the given instance type is invalid
|
|
|
|
*/
|
|
|
|
public function getInstanceForm($type)
|
|
|
|
{
|
2014-10-29 13:36:09 +01:00
|
|
|
switch (strtolower($type)) {
|
|
|
|
case LocalCommandFile::TRANSPORT:
|
|
|
|
$form = new LocalInstanceForm();
|
|
|
|
break;
|
|
|
|
case RemoteCommandFile::TRANSPORT;
|
|
|
|
$form = new RemoteInstanceForm();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException(
|
2015-01-19 13:47:01 +01:00
|
|
|
sprintf($this->translate('Invalid instance type "%s" given'), $type)
|
2014-10-29 13:36:09 +01:00
|
|
|
);
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
2014-10-29 13:36:09 +01:00
|
|
|
return $form;
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2014-09-04 08:47:16 +02:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the resource already exists
|
|
|
|
*/
|
|
|
|
public function add(array $values)
|
|
|
|
{
|
|
|
|
$name = isset($values['name']) ? $values['name'] : '';
|
|
|
|
if (! $name) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Instance name missing'));
|
2014-10-29 13:36:09 +01:00
|
|
|
}
|
2014-11-18 13:11:52 +01:00
|
|
|
if ($this->config->hasSection($name)) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Instance already exists'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($values['name']);
|
2014-11-18 13:11:52 +01:00
|
|
|
$this->config->setSection($name, $values);
|
2014-09-04 08:47:16 +02:00
|
|
|
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) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Old instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif (! ($newName = isset($values['name']) ? $values['name'] : '')) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('New instance name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif (! $this->config->hasSection($name)) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Unknown instance name provided'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($values['name']);
|
2014-11-18 13:11:52 +01:00
|
|
|
$this->config->setSection($name, $values);
|
|
|
|
return $this->config->getSection($name);
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a instance
|
|
|
|
*
|
|
|
|
* @param string $name The name of the resource to remove
|
|
|
|
*
|
2014-10-29 13:36:09 +01:00
|
|
|
* @return array The removed resource configuration
|
2014-09-04 08:47:16 +02:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the resource name is missing or invalid
|
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
|
|
|
if (! $name) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Instance name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif (! $this->config->hasSection($name)) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Unknown instance name provided'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$instanceConfig = $this->config->getSection($name);
|
|
|
|
$this->config->removeSection($name);
|
2014-09-04 08:47:16 +02:00
|
|
|
return $instanceConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-29 13:36:09 +01:00
|
|
|
* @see Form::onRequest() For the method documentation.
|
|
|
|
* @throws ConfigurationError In case the instance name is missing or invalid
|
2014-09-04 08:47:16 +02:00
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onRequest()
|
2014-09-04 08:47:16 +02:00
|
|
|
{
|
2014-11-14 14:59:12 +01:00
|
|
|
$instanceName = $this->request->getQuery('instance');
|
2014-10-29 13:36:09 +01:00
|
|
|
if ($instanceName !== null) {
|
|
|
|
if (! $instanceName) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new ConfigurationError($this->translate('Instance name missing'));
|
2014-10-29 13:36:09 +01:00
|
|
|
}
|
2014-11-18 13:11:52 +01:00
|
|
|
if (! $this->config->hasSection($instanceName)) {
|
2015-01-19 13:47:01 +01:00
|
|
|
throw new ConfigurationError($this->translate('Unknown instance name given'));
|
2014-10-29 13:36:09 +01:00
|
|
|
}
|
2014-09-04 08:47:16 +02:00
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$instanceConfig = $this->config->getSection($instanceName)->toArray();
|
2014-10-29 13:36:09 +01:00
|
|
|
$instanceConfig['name'] = $instanceName;
|
|
|
|
$this->populate($instanceConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPDoc)
|
|
|
|
* @see Form::onSuccess() For the method documentation.
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2014-10-29 13:36:09 +01:00
|
|
|
{
|
2014-11-14 14:59:12 +01:00
|
|
|
$instanceName = $this->request->getQuery('instance');
|
2014-09-04 08:47:16 +02:00
|
|
|
try {
|
|
|
|
if ($instanceName === null) { // create new instance
|
|
|
|
$this->add($this->getValues());
|
2015-01-19 13:47:01 +01:00
|
|
|
$message = $this->translate('Instance "%s" created successfully.');
|
2014-09-04 08:47:16 +02:00
|
|
|
} else { // edit existing instance
|
|
|
|
$this->edit($instanceName, $this->getValues());
|
2015-01-19 13:47:01 +01:00
|
|
|
$message = $this->translate('Instance "%s" edited successfully.');
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
Notification::error($e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->save()) {
|
|
|
|
Notification::success(sprintf($message, $this->getElement('name')->getValue()));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-29 13:36:09 +01:00
|
|
|
* (non-PHPDoc)
|
|
|
|
* @see Form::createElements() For the method documentation.
|
2014-09-04 08:47:16 +02:00
|
|
|
*/
|
2014-10-29 13:36:09 +01:00
|
|
|
public function createElements(array $formData = array())
|
2014-09-04 08:47:16 +02:00
|
|
|
{
|
2014-10-29 13:36:09 +01:00
|
|
|
$instanceType = isset($formData['transport']) ? $formData['transport'] : LocalCommandFile::TRANSPORT;
|
2014-09-04 08:47:16 +02:00
|
|
|
|
2014-10-29 13:36:09 +01:00
|
|
|
$this->addElements(array(
|
2014-09-04 08:47:16 +02:00
|
|
|
array(
|
2014-10-29 13:36:09 +01:00
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 13:47:01 +01:00
|
|
|
'label' => $this->translate('Instance Name')
|
2014-10-29 13:36:09 +01:00
|
|
|
)
|
|
|
|
),
|
2014-09-04 08:47:16 +02:00
|
|
|
array(
|
2014-10-29 13:36:09 +01:00
|
|
|
'select',
|
|
|
|
'transport',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'autosubmit' => true,
|
2015-01-19 13:47:01 +01:00
|
|
|
'label' => $this->translate('Instance Type'),
|
2014-10-29 13:36:09 +01:00
|
|
|
'multiOptions' => array(
|
2015-01-19 13:47:01 +01:00
|
|
|
LocalCommandFile::TRANSPORT => $this->translate('Local Command File'),
|
|
|
|
RemoteCommandFile::TRANSPORT => $this->translate('Remote Command File')
|
2014-10-29 13:36:09 +01:00
|
|
|
),
|
|
|
|
'value' => $instanceType
|
|
|
|
)
|
2014-09-04 08:47:16 +02:00
|
|
|
)
|
2014-10-29 13:36:09 +01:00
|
|
|
));
|
2014-09-04 08:47:16 +02:00
|
|
|
|
|
|
|
$this->addElements($this->getInstanceForm($instanceType)->createElements($formData)->getElements());
|
|
|
|
}
|
|
|
|
}
|