From d389d8e0f0c8a8b72f795347decf7aaca31e5a6d Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 6 May 2014 15:01:43 +0000 Subject: [PATCH] Re-establish Backend::createBackend This partially reverts fcf7f7d6. The original intention was to have no library code reading config. Moving code snippets to another lib doesn't solve this. We need quick & easy access to a monitoring backend, on CLI, in other modules and so forth. Duplicating so much code again and again is not an option. I prefer the former way of doing things unless we find a better solution. --- .../monitoring/library/Monitoring/Backend.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Backend.php b/modules/monitoring/library/Monitoring/Backend.php index e28b36be5..502f04c4c 100644 --- a/modules/monitoring/library/Monitoring/Backend.php +++ b/modules/monitoring/library/Monitoring/Backend.php @@ -8,6 +8,10 @@ use Icinga\Exception\ProgrammingError; use Icinga\Data\Selectable; use Icinga\Data\Queryable; +use Icinga\Application\Config as IcingaConfig; +use Icinga\Data\ResourceFactory; +use Icinga\Exception\ConfigurationError; + /** * Data view and query loader tied to a backend type */ @@ -39,6 +43,53 @@ class Backend implements Selectable, Queryable $this->type = $type; } + /** + * Create a backend + * + * @param string $backendName Name of the backend or null for creating the default backend which is the first INI + * configuration entry not being disabled + * + * @return Backend + * @throws ConfigurationError When no backend has been configured or all backends are disabled or the + * configuration for the requested backend does either not exist or it's disabled + */ + public static function createBackend($backendName = null) + { + $allBackends = array(); + $defaultBackend = null; + foreach (IcingaConfig::module('monitoring', 'backends') as $name => $config) { + if (!(bool) $config->get('disabled', false) && $defaultBackend === null) { + $defaultBackend = $config; + } + $allBackends[$name] = $config; + } + if (empty($allBackends)) { + throw new ConfigurationError('No backend has been configured'); + } + if ($defaultBackend === null) { + throw new ConfigurationError('All backends are disabled'); + } + if ($backendName === null) { + $backendConfig = $defaultBackend; + } else { + if (!array_key_exists($backendName, $allBackends)) { + throw new ConfigurationError('No configuration for backend ' . $backendName); + } + $backendConfig = $allBackends[$backendName]; + if ((bool) $backendConfig->get('disabled', false)) { + throw new ConfigurationError( + 'Configuration for backend ' . $backendName . ' available but backend is disabled' + ); + } + } + $resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource)); + if ($backendConfig->type === 'ido' && $resource->getDbType() !== 'oracle') { + // TODO(el): The resource should set the table prefix + $resource->setTablePrefix('icinga_'); + } + return new Backend($resource, $backendConfig->type); + } + /** * Backend entry point *