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.
This commit is contained in:
parent
fcf7f7d6dc
commit
d389d8e0f0
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue