IdoResourcePage: Validate the version of a PostgreSQL server

refs #9460
This commit is contained in:
Johannes Meyer 2015-08-19 14:42:18 +02:00
parent 9282e1bce2
commit b7a389601d
2 changed files with 61 additions and 46 deletions

View File

@ -8,6 +8,7 @@ use Icinga\Forms\Config\ResourceConfigForm;
use Icinga\Forms\Config\Resource\DbResourceForm; use Icinga\Forms\Config\Resource\DbResourceForm;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm; use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm;
use Icinga\Module\Setup\Utils\DbTool;
class IdoResourcePage extends Form class IdoResourcePage extends Form
{ {
@ -73,23 +74,8 @@ class IdoResourcePage extends Form
} }
if (! isset($formData['skip_validation']) || !$formData['skip_validation']) { if (! isset($formData['skip_validation']) || !$formData['skip_validation']) {
$inspection = ResourceConfigForm::inspectResource($this); if (! $this->validateConfiguration()) {
if ($inspection !== null && $inspection->hasError()) { $this->addSkipValidationCheckbox();
$this->error($inspection->getError());
$this->addSkipValidationCheckbox($this->translate(
'Check this to not to validate connectivity with the given database server.'
));
return false;
}
$configObject = new ConfigObject($this->getValues());
if (
! BackendConfigForm::isValidIdoSchema($this, $configObject)
|| !BackendConfigForm::isValidIdoInstance($this, $configObject)
) {
$this->addSkipValidationCheckbox($this->translate(
'Check this to not to validate the IDO schema in the given database.'
));
return false; return false;
} }
} }
@ -109,8 +95,31 @@ class IdoResourcePage extends Form
public function isValidPartial(array $formData) public function isValidPartial(array $formData)
{ {
if (isset($formData['backend_validation']) && parent::isValid($formData)) { if (isset($formData['backend_validation']) && parent::isValid($formData)) {
if (! $this->validateConfiguration(true)) {
return false;
}
$this->info($this->translate('The configuration has been successfully validated.'));
} elseif (! isset($formData['backend_validation'])) {
// This is usually done by isValid(Partial), but as we're not calling any of these...
$this->populate($formData);
}
return true;
}
/**
* Return whether the configuration is valid
*
* @param bool $showLog Whether to show the validation log
*
* @return bool
*/
protected function validateConfiguration($showLog = false)
{
$inspection = ResourceConfigForm::inspectResource($this); $inspection = ResourceConfigForm::inspectResource($this);
if ($inspection !== null) { if ($inspection !== null) {
if ($showLog) {
$join = function ($e) use (& $join) { $join = function ($e) use (& $join) {
return is_string($e) ? $e : join("\n", array_map($join, $e)); return is_string($e) ? $e : join("\n", array_map($join, $e));
}; };
@ -127,9 +136,10 @@ class IdoResourcePage extends Form
) )
) )
); );
}
if ($inspection->hasError()) { if ($inspection->hasError()) {
$this->warning(sprintf( $this->error(sprintf(
$this->translate('Failed to successfully validate the configuration: %s'), $this->translate('Failed to successfully validate the configuration: %s'),
$inspection->getError() $inspection->getError()
)); ));
@ -137,35 +147,42 @@ class IdoResourcePage extends Form
} }
} }
$this->info($this->translate('The configuration has been successfully validated.')); $configObject = new ConfigObject($this->getValues());
} elseif (! isset($formData['backend_validation'])) { if (
// This is usually done by isValid(Partial), but as we're not calling any of these... ! BackendConfigForm::isValidIdoSchema($this, $configObject)
$this->populate($formData); || !BackendConfigForm::isValidIdoInstance($this, $configObject)
) {
return false;
}
if ($this->getValue('db') === 'pgsql') {
$db = new DbTool($this->getValues());
$version = $db->connectToDb()->getServerVersion();
if (version_compare($version, '9.1', '<')) {
$this->error($this->translate(sprintf(
'The server\'s version %s is too old. The minimum required version is %s.',
$version,
'9.1'
)));
return false;
}
} }
return true; return true;
} }
/** /**
* Add a checkbox to the form by which the user can skip the resource validation * Add a checkbox to the form by which the user can skip the configuration validation
*
* @param string $description
*/ */
protected function addSkipValidationCheckbox($description = null) protected function addSkipValidationCheckbox()
{ {
if (empty($description)) {
$description = $this->translate(
'Proceed without any further (custom) validation.'
);
}
$this->addElement( $this->addElement(
'checkbox', 'checkbox',
'skip_validation', 'skip_validation',
array( array(
'required' => true, 'required' => true,
'label' => $this->translate('Skip Validation'), 'label' => $this->translate('Skip Validation'),
'description' => $description 'description' => $this->translate('Check this to not to validate the configuration')
) )
); );
} }

View File

@ -151,7 +151,7 @@ class DbResourcePage extends Form
} }
/** /**
* Add a checkbox to the form by which the user can skip the connection validation * Add a checkbox to the form by which the user can skip the configuration validation
*/ */
protected function addSkipValidationCheckbox() protected function addSkipValidationCheckbox()
{ {
@ -161,9 +161,7 @@ class DbResourcePage extends Form
array( array(
'required' => true, 'required' => true,
'label' => $this->translate('Skip Validation'), 'label' => $this->translate('Skip Validation'),
'description' => $this->translate( 'description' => $this->translate('Check this to not to validate the configuration')
'Check this to not to validate connectivity with the given database server'
)
) )
); );
} }