From c3b4ea71d3a65ef1cb454141f80bb55acae91ed4 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Mon, 11 May 2015 10:32:34 +0200 Subject: [PATCH] Add ido schema and instance availability validation refs #9203 --- .../forms/Config/BackendConfigForm.php | 63 +++++++++++++++++++ .../forms/Setup/IdoResourcePage.php | 11 +++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/modules/monitoring/application/forms/Config/BackendConfigForm.php b/modules/monitoring/application/forms/Config/BackendConfigForm.php index d89c6623f..567f7a575 100644 --- a/modules/monitoring/application/forms/Config/BackendConfigForm.php +++ b/modules/monitoring/application/forms/Config/BackendConfigForm.php @@ -3,6 +3,10 @@ namespace Icinga\Module\Monitoring\Forms\Config; +use Exception; +use Icinga\Data\ConfigObject; +use Icinga\Data\ResourceFactory; +use Icinga\Web\Form; use InvalidArgumentException; use Icinga\Application\Config; use Icinga\Exception\ConfigurationError; @@ -271,4 +275,63 @@ class BackendConfigForm extends ConfigForm ) ); } + + /** + * Validate the ido instance schema resource + * + * @param Form $form + * @param ConfigObject $resourceConfig + * + * @return bool Whether validation succeeded or not + */ + public static function isValidIdoSchema(Form $form, ConfigObject $resourceConfig) + { + try { + $resource = ResourceFactory::createResource($resourceConfig); + $result = $resource->select()->from('icinga_dbversion', array('version')); + $result->fetchOne(); + } catch (Exception $e) { + $form->addError( + $form->translate( + 'IDO schema validation failed, it looks like that the IDO schema is missing in the given database.' + ) + ); + return false; + } + return true; + } + + /** + * Validate the ido instance availability + * + * @param Form $form + * @param ConfigObject $resourceConfig + * + * @return bool Whether validation succeeded or not + */ + public static function isValidIdoInstance(Form $form, ConfigObject $resourceConfig) + { + $resource = ResourceFactory::createResource($resourceConfig); + $result = $resource->select()->from('icinga_instances', array('instance_name')); + $instances = $result->fetchAll(); + + if (count($instances) === 1) { + return true; + } elseif (count($instances) > 1) { + $form->addError( + $form->translate( + 'WARNING: IDO instance validation failed, because there are multiple instances available.' + ) + ); + return false; + } + + $form->addError( + $form->translate( + 'IDO instance validation failed, because there is no IDO instance available.' + ) + ); + + return false; + } } diff --git a/modules/monitoring/application/forms/Setup/IdoResourcePage.php b/modules/monitoring/application/forms/Setup/IdoResourcePage.php index f52601359..b856c590c 100644 --- a/modules/monitoring/application/forms/Setup/IdoResourcePage.php +++ b/modules/monitoring/application/forms/Setup/IdoResourcePage.php @@ -3,6 +3,8 @@ namespace Icinga\Module\Monitoring\Forms\Setup; +use Icinga\Data\ConfigObject; +use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm; use Icinga\Web\Form; use Icinga\Forms\Config\Resource\DbResourceForm; @@ -53,7 +55,14 @@ class IdoResourcePage extends Form } if (false === isset($data['skip_validation']) || $data['skip_validation'] == 0) { - if (false === DbResourceForm::isValidResource($this)) { + $configObject = new ConfigObject($this->getValues()); + if (false === DbResourceForm::isValidResource($this, $configObject)) { + $this->addSkipValidationCheckbox(); + return false; + } elseif (false === BackendConfigForm::isValidIdoSchema($this, $configObject)) { + $this->addSkipValidationCheckbox(); + return false; + } elseif (false === BackendConfigForm::isValidIdoInstance($this, $configObject)) { $this->addSkipValidationCheckbox(); return false; }