2013-09-24 15:26:10 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-09-24 15:26:10 +02:00
|
|
|
|
|
|
|
namespace Icinga\Data;
|
|
|
|
|
2014-10-20 13:40:35 +02:00
|
|
|
use Icinga\Application\Config;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Util\ConfigAwareFactory;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-06-06 08:43:13 +02:00
|
|
|
use Icinga\Data\Db\DbConnection;
|
2013-10-22 22:21:03 +02:00
|
|
|
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
|
2015-06-24 09:05:29 +02:00
|
|
|
use Icinga\Protocol\Ldap\LdapConnection;
|
2014-09-04 15:29:11 +02:00
|
|
|
use Icinga\Protocol\File\FileReader;
|
2013-09-24 15:26:10 +02:00
|
|
|
|
2014-08-28 11:08:57 +02:00
|
|
|
/**
|
|
|
|
* Create resources from names or resource configuration
|
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
class ResourceFactory implements ConfigAwareFactory
|
|
|
|
{
|
|
|
|
/**
|
2014-08-28 11:08:57 +02:00
|
|
|
* Resource configuration
|
|
|
|
*
|
2014-11-07 13:53:03 +01:00
|
|
|
* @var Config
|
2013-09-24 15:26:10 +02:00
|
|
|
*/
|
|
|
|
private static $resources;
|
|
|
|
|
2014-08-28 11:08:57 +02:00
|
|
|
/**
|
|
|
|
* Set resource configurations
|
|
|
|
*
|
2014-11-07 13:53:03 +01:00
|
|
|
* @param Config $config
|
2014-08-28 11:08:57 +02:00
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public static function setConfig($config)
|
|
|
|
{
|
|
|
|
self::$resources = $config;
|
|
|
|
}
|
|
|
|
|
2013-11-06 13:31:07 +01:00
|
|
|
/**
|
|
|
|
* Get the configuration for a specific resource
|
|
|
|
*
|
2014-08-28 11:08:57 +02:00
|
|
|
* @param $resourceName String The resource's name
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @return ConfigObject The configuration of the resource
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2014-08-28 11:08:57 +02:00
|
|
|
* @throws ConfigurationError
|
2013-11-06 13:31:07 +01:00
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public static function getResourceConfig($resourceName)
|
2013-11-06 13:31:07 +01:00
|
|
|
{
|
|
|
|
self::assertResourcesExist();
|
2014-11-18 13:11:52 +01:00
|
|
|
$resourceConfig = self::$resources->getSection($resourceName);
|
|
|
|
if ($resourceConfig->isEmpty()) {
|
2014-02-21 10:16:16 +01:00
|
|
|
throw new ConfigurationError(
|
2014-08-22 11:46:11 +02:00
|
|
|
'Cannot load resource config "%s". Resource does not exist',
|
|
|
|
$resourceName
|
2014-02-21 10:16:16 +01:00
|
|
|
);
|
2013-11-06 13:31:07 +01:00
|
|
|
}
|
|
|
|
return $resourceConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-17 10:14:45 +01:00
|
|
|
* Get the configuration of all existing resources, or all resources of the given type
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2016-11-17 10:14:45 +01:00
|
|
|
* @param string $type Filter for resource type
|
|
|
|
*
|
|
|
|
* @return Config The resources configuration
|
2013-11-06 13:31:07 +01:00
|
|
|
*/
|
2016-11-17 10:14:45 +01:00
|
|
|
public static function getResourceConfigs($type = null)
|
2013-11-06 13:31:07 +01:00
|
|
|
{
|
|
|
|
self::assertResourcesExist();
|
2016-11-17 10:14:45 +01:00
|
|
|
if ($type === null) {
|
|
|
|
return self::$resources;
|
|
|
|
}
|
|
|
|
$resources = array();
|
|
|
|
foreach (self::$resources as $name => $resource) {
|
|
|
|
if ($resource->get('type') === $type) {
|
|
|
|
$resources[$name] = $resource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Config::fromArray($resources);
|
2013-11-06 13:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-26 12:29:30 +01:00
|
|
|
* Check if the existing resources are set. If not, load them from resources.ini
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2015-04-21 13:59:35 +02:00
|
|
|
* @throws ConfigurationError
|
2013-11-06 13:31:07 +01:00
|
|
|
*/
|
|
|
|
private static function assertResourcesExist()
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
2015-04-21 13:59:35 +02:00
|
|
|
if (self::$resources === null) {
|
2016-02-26 12:29:30 +01:00
|
|
|
self::$resources = Config::app('resources');
|
2013-11-06 10:20:15 +01:00
|
|
|
}
|
2013-10-07 16:46:20 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 13:31:07 +01:00
|
|
|
/**
|
2016-02-12 14:18:19 +01:00
|
|
|
* Create and return a resource based on the given configuration
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2016-02-12 14:18:19 +01:00
|
|
|
* @param ConfigObject $config The configuration of the resource to create
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2016-02-12 14:18:19 +01:00
|
|
|
* @return Selectable The resource
|
|
|
|
* @throws ConfigurationError In case of an unsupported type or invalid configuration
|
2013-11-06 13:31:07 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public static function createResource(ConfigObject $config)
|
2013-10-07 16:46:20 +02:00
|
|
|
{
|
|
|
|
switch (strtolower($config->type)) {
|
2013-09-24 15:26:10 +02:00
|
|
|
case 'db':
|
2013-10-07 16:46:20 +02:00
|
|
|
$resource = new DbConnection($config);
|
|
|
|
break;
|
2013-11-06 10:20:15 +01:00
|
|
|
case 'ldap':
|
2016-02-12 14:18:19 +01:00
|
|
|
if (empty($config->root_dn)) {
|
|
|
|
throw new ConfigurationError('LDAP root DN missing');
|
|
|
|
}
|
|
|
|
|
2013-11-06 10:20:15 +01:00
|
|
|
$resource = new LdapConnection($config);
|
|
|
|
break;
|
2013-10-22 22:21:03 +02:00
|
|
|
case 'livestatus':
|
|
|
|
$resource = new LivestatusConnection($config->socket);
|
|
|
|
break;
|
2014-03-19 16:57:11 +01:00
|
|
|
case 'file':
|
|
|
|
$resource = new FileReader($config);
|
|
|
|
break;
|
2014-10-20 13:40:35 +02:00
|
|
|
case 'ini':
|
|
|
|
$resource = Config::fromIni($config->ini);
|
|
|
|
break;
|
2013-09-24 15:26:10 +02:00
|
|
|
default:
|
2014-08-22 11:46:11 +02:00
|
|
|
throw new ConfigurationError(
|
|
|
|
'Unsupported resource type "%s"',
|
|
|
|
$config->type
|
|
|
|
);
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
2016-02-12 14:18:19 +01:00
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
return $resource;
|
|
|
|
}
|
2014-08-28 11:22:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a resource from name
|
|
|
|
*
|
|
|
|
* @param string $resourceName
|
2014-10-07 13:22:07 +02:00
|
|
|
* @return DbConnection|LdapConnection|LivestatusConnection
|
2014-08-28 11:22:02 +02:00
|
|
|
*/
|
|
|
|
public static function create($resourceName)
|
|
|
|
{
|
|
|
|
return self::createResource(self::getResourceConfig($resourceName));
|
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|