2014-09-04 08:47:16 +02:00
|
|
|
<?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');
|
2014-10-21 17:22:16 +02:00
|
|
|
$this->setSubmitLabel(mt('monitoring', 'Save Changes'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(sprintf(mt('monitoring', 'Invalid instance type "%s" provided'), $type));
|
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
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the resource already exists
|
|
|
|
*/
|
|
|
|
public function add(array $values)
|
|
|
|
{
|
|
|
|
$name = isset($values['name']) ? $values['name'] : '';
|
|
|
|
if (! $name) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif ($this->config->get($name) !== null) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Instance already exists'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Old instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif (! ($newName = isset($values['name']) ? $values['name'] : '')) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'New instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif (! ($instanceConfig = $this->config->get($name))) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Unknown instance name provided'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif (! ($instanceConfig = $this->config->get($name))) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new InvalidArgumentException(mt('monitoring', 'Unknown instance name provided'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2014-10-21 17:22:16 +02:00
|
|
|
$message = mt('monitoring', 'Instance "%s" created successfully.');
|
2014-09-04 08:47:16 +02:00
|
|
|
} else { // edit existing instance
|
|
|
|
$this->edit($instanceName, $this->getValues());
|
2014-10-21 17:22:16 +02:00
|
|
|
$message = mt('monitoring', '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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new ConfigurationError(mt('monitoring', 'Instance name missing'));
|
2014-09-04 08:47:16 +02:00
|
|
|
} elseif (false === isset($this->config->{$instanceName})) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new ConfigurationError(mt('monitoring', 'Unknown instance name provided'));
|
2014-09-04 08:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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,
|
2014-10-21 17:22:16 +02:00
|
|
|
'label' => mt('monitoring', 'Instance Name')
|
2014-09-04 08:47:16 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'type',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'ignore' => true,
|
|
|
|
'autosubmit' => true,
|
2014-10-21 17:22:16 +02:00
|
|
|
'label' => mt('monitoring', 'Instance Type'),
|
|
|
|
'description' => mt('monitoring',
|
2014-09-04 08:47:16 +02:00
|
|
|
'When configuring a remote host, you need to setup passwordless key authentication'
|
|
|
|
),
|
|
|
|
'multiOptions' => array(
|
2014-10-21 17:22:16 +02:00
|
|
|
'local' => mt('monitoring', 'Local Command Pipe'),
|
|
|
|
'remote' => mt('monitoring', 'Remote Command Pipe')
|
2014-09-04 08:47:16 +02:00
|
|
|
),
|
|
|
|
'value' => $instanceType
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElements($this->getInstanceForm($instanceType)->createElements($formData)->getElements());
|
|
|
|
}
|
|
|
|
}
|