Replace Backend::createBackend() with Monitoring/Controller::createBackend()

Library code should not read configuration files. The new createBackend() method
from Monitoring/Controller takes care of reading backend configuration and error
handling. The Backend constructor now requires a resource instance.
This commit is contained in:
Eric Lippmann 2014-04-17 15:53:23 +02:00
parent 026145356b
commit fcf7f7d6dc
2 changed files with 98 additions and 160 deletions

View File

@ -1,90 +1,48 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring; namespace Icinga\Module\Monitoring;
use Icinga\Module\Monitoring\Exception\UnsupportedBackendException; use Icinga\Exception\ProgrammingError;
use Zend_Config; use Icinga\Data\Selectable;
use Icinga\Application\Config as IcingaConfig; use Icinga\Data\Queryable;
use Icinga\Exception\ConfigurationError;
use Icinga\Data\DatasourceInterface;
use Icinga\Data\ResourceFactory;
use Icinga\Util\ConfigAwareFactory;
class Backend implements ConfigAwareFactory, DatasourceInterface /**
* Data view and query loader tied to a backend type
*/
class Backend implements Selectable, Queryable
{ {
/** /**
* Resource config * Resource
*
* @var Zend_config
*/
private $config;
/**
* The resource the backend utilizes
* *
* @var mixed * @var mixed
*/ */
private $resource; protected $resource;
private static $backendInstances = array();
private static $backendConfigs = array();
/** /**
* Create a new backend from the given resource config * Type
* *
* @param Zend_Config $backendConfig * @var string
* @param Zend_Config $resourceConfig
*/ */
public function __construct(Zend_Config $backendConfig, Zend_Config $resourceConfig) protected $type;
{
$this->config = $backendConfig;
$this->resource = ResourceFactory::createResource($resourceConfig);
}
/** /**
* Set backend configs * Create a new backend
* *
* @param Zend_Config $backendConfigs * @param mixed $resource
* @param string $type
*/ */
public static function setConfig($backendConfigs) public function __construct($resource, $type)
{ {
foreach ($backendConfigs as $name => $config) { $this->resource = $resource;
self::$backendConfigs[$name] = $config; $this->type = $type;
}
} }
/** /**
* Backend entry point * Backend entry point
* *
* return self * @return self
*/ */
public function select() public function select()
{ {
@ -92,125 +50,57 @@ class Backend implements ConfigAwareFactory, DatasourceInterface
} }
/** /**
* Create query to retrieve columns and rows from the the given table * Create a data view to fetch data from
* *
* @param string $table * @param string $viewName
* @param array $columns * @param array $columns
* *
* @return Query * @return DataView
*/ */
public function from($table, array $columns = null) public function from($viewName, array $columns = null)
{ {
$queryClass = '\\Icinga\\Module\\Monitoring\\Backend\\' $viewClass = $this->resolveDataViewName($viewName);
. ucfirst($this->config->type) $queryClass = $this->resolveQueryName($viewClass::getQueryName());
. '\\Query\\' return new $viewClass(new $queryClass($this->resource), $columns);
. ucfirst($table)
. 'Query';
if (!class_exists($queryClass)) {
throw new UnsupportedBackendException('Query '
. ucfirst($table)
. ' Is Not Available For Backend '
. ucfirst($this->config->type)
);
}
return new $queryClass($this->resource, $columns);
} }
/** /**
* Get the resource which was created in the constructor * View name to class name resolution
* *
* @return mixed * @param string $viewName
*/
public function getResource()
{
return $this->resource;
}
/**
* Get backend configs
*
* @return Zend_Config
*/
public static function getBackendConfigs()
{
if (empty(self::$backendConfigs)) {
self::setConfig(IcingaConfig::module('monitoring', 'backends'));
}
return self::$backendConfigs;
}
/**
* Retrieve the name of the default backend which is the INI's first entry
* *
* @return string * @return string
* @throws ConfigurationError When no backend has been configured * @throws ProgrammingError When the view does not exist
*/ */
public static function getDefaultBackendName() protected function resolveDataViewName($viewName)
{ {
$configs = self::getBackendConfigs(); $viewClass = '\\Icinga\\Module\\Monitoring\\DataView\\' . ucfirst($viewName);
if (empty($configs)) { if (!class_exists($viewClass)) {
throw new ConfigurationError( throw new ProgrammingError('DataView ' . ucfirst($viewName) . ' does not exist');
'Cannot get default backend as no backend has been configured'
);
} }
return $viewClass;
// We won't have disabled backends
foreach ($configs as $name => $config) {
if (!$config->get('disabled') == '1') {
return $name;
}
}
throw new ConfigurationError(
'All backends are disabled'
);
} }
/** /**
* Create the backend with the given name * Query name to class name resolution
* *
* @param $name * @param string $queryName
* *
* @return Backend * @return string
* @throws ProgrammingError When the query does not exist for this backend
*/ */
public static function createBackend($name) protected function resolveQueryName($queryName)
{ {
if (array_key_exists($name, self::$backendInstances)) { $queryClass = '\\Icinga\\Module\\Monitoring\\Backend\\'
return self::$backendInstances[$name]; . ucfirst($this->type)
} . '\\Query\\'
. ucfirst($queryName)
if ($name === null) { . 'Query';
$name = self::getDefaultBackendName(); if (!class_exists($queryClass)) {
} throw new ProgrammingError(
'Query ' . ucfirst($queryName) . ' does not exist for backend ' . ucfirst($this->type)
$config = null;
self::getBackendConfigs();
if (isset(self::$backendConfigs[$name])) {
/** @var Zend_Config $config */
$config = self::$backendConfigs[$name];
if ($config->get('disabled') == '1') {
$config = null;
}
}
if ($config === null) {
throw new ConfigurationError(
'No configuration for backend:' . $name
); );
} }
return $queryClass;
self::$backendInstances[$name] = $backend = new self(
$config,
ResourceFactory::getResourceConfig($config->resource)
);
switch (strtolower($config->type)) {
case 'ido':
if ($backend->getResource()->getDbType() !== 'oracle') {
$backend->getResource()->setTablePrefix('icinga_');
}
break;
}
return $backend;
} }
} }

View File

@ -30,9 +30,10 @@
namespace Icinga\Module\Monitoring; namespace Icinga\Module\Monitoring;
use Icinga\Application\Config as IcingaConfig; use Icinga\Application\Config as IcingaConfig;
use Icinga\Module\Monitoring\DataView\ServiceStatus as ServiceStatusView; use Icinga\Data\ResourceFactory;
use Icinga\Web\Controller\ActionController; use Icinga\Exception\ConfigurationError;
use Icinga\File\Csv; use Icinga\File\Csv;
use Icinga\Web\Controller\ActionController;
/** /**
* Base class for all monitoring action controller * Base class for all monitoring action controller
@ -73,5 +74,52 @@ class Controller extends ActionController
exit; exit;
} }
} }
/**
* 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
*/
protected 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);
}
} }
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd