Reorganization of configuration modules, fix circular dependencies

The new module cartodb_services/config is intended for services configuration objects
Some legacy configuration objects remain under cartodb_services/metrics.
The refactored configuration backends are also not moved here
This commit is contained in:
Javier Goizueta 2017-03-21 15:42:53 +01:00
parent 945c6cd685
commit 73f97128ed
7 changed files with 66 additions and 48 deletions

View File

@ -0,0 +1,3 @@
from service_configuration import ServiceConfiguration
from rate_limits import RateLimitsConfig, RateLimitsConfigBuilder, RateLimitsConfigSetter
from legacy_rate_limits import RateLimitsConfigLegacyBuilder

View File

@ -1,4 +1,7 @@
import json
from service_configuration import ServiceConfiguration
class RateLimitsConfig(object):
"""
Value object that represents the configuration needed to rate-limit services
@ -14,6 +17,9 @@ class RateLimitsConfig(object):
self._limit = limit and int(limit)
self._period = period and int(period)
def __eq__(self, other):
return self.__dict__ == other.__dict__
# service this limit applies to
@property
def service(self):
@ -43,6 +49,7 @@ class RateLimitsConfigBuilder(object):
from the user/org/server configuration.
"""
# TODO: user->username, org->orgname
def __init__(self, server_conf, user_conf, org_conf, service, user, org):
self._server_conf = server_conf
self._user_conf = user_conf
@ -55,6 +62,7 @@ class RateLimitsConfigBuilder(object):
# Order of precedence is user_conf, org_conf, server_conf
rate_limit_key = "{0}_rate_limit".format(self._service)
rate_limit_json = self._user_conf.get(rate_limit_key, None) or self._org_conf.get(rate_limit_key, None)
if (rate_limit_json):
rate_limit = rate_limit_json and json.loads(rate_limit_json)
@ -71,34 +79,36 @@ class RateLimitsConfigSetter(object):
def __init__(self, service, username, orgname=None):
self._service = service
self._service_config = ServerConfiguration(service, username, orgname)
self._service_config = ServiceConfiguration(service, username, orgname)
def set_user_rate_limits(self, rate_limits_config):
# Note we allow copying a config from another user/service, so we
# ignore rate_limits:config.service and rate_limits:config.username
rate_limit_key = "{0}_rate_limit".format(service)
rate_limit_key = "{0}_rate_limit".format(self._service)
if rate_limits_config.is_limited():
rate_limit = {'limit': rate_limits_config.limit, 'period': rate_limits_config.period}
self.service_config.user.set(rate_limit_key, rate_limit)
else
self.service_config.user.remove(rate_limit_key)
rate_limit_json = json.dumps(rate_limit)
self._service_config.user.set(rate_limit_key, rate_limit_json)
else:
self._service_config.user.remove(rate_limit_key)
def set_org_rate_limits(self, rate_limits_config):
rate_limit_key = "{0}_rate_limit".format(service)
rate_limit_key = "{0}_rate_limit".format(self._service)
if rate_limits_config.is_limited():
rate_limit = {'limit': rate_limits_config.limit, 'period': rate_limits_config.period}
self.service_config.org.set(rate_limit_key, rate_limit)
else
self.service_config.org.remove(rate_limit_key)
rate_limit_json = json.dumps(rate_limit)
self._service_config.org.set(rate_limit_key, rate_limit_json)
else:
self._service_config.org.remove(rate_limit_key)
def set_server_rate_limits(self, rate_limits_config):
rate_limits = self.service_config.server.get('rate_limits', {})
rate_limits = self._service_config.server.get('rate_limits', {})
if rate_limits_config.is_limited():
rate_limits[self._service] = {'limit': rate_limits_config.limit, 'period': rate_limits_config.period}
else
else:
rate_limits.pop(self._service, None)
if rate_limits:
self.service_config.server.set('rate_limits', rate_limits)
else
self.service_config.server.remove('rate_limits')
self._service_config.server.set('rate_limits', rate_limits)
else:
self._service_config.server.remove('rate_limits')

View File

@ -0,0 +1,36 @@
from cartodb_services.refactor.core.environment import ServerEnvironmentBuilder
from cartodb_services.refactor.backend.server_config import ServerConfigBackendFactory
from cartodb_services.refactor.backend.user_config import UserConfigBackendFactory
from cartodb_services.refactor.backend.org_config import OrgConfigBackendFactory
class ServiceConfiguration(object):
"""
This class instantiates configuration backend objects for all the configuration levels of a service:
* environment
* server
* organization
* user
The configuration backends allow retrieval and modification of configuration parameters.
"""
def __init__(self, service, username, orgname):
self._server_config_backend = ServerConfigBackendFactory().get()
self._environment = ServerEnvironmentBuilder(self._server_config_backend ).get()
self._user_config_backend = UserConfigBackendFactory(username, self._environment, self._server_config_backend ).get()
self._org_config_backend = OrgConfigBackendFactory(orgname, self._environment, self._server_config_backend ).get()
@property
def environment(self):
return self._environment
@property
def server(self):
return self._server_config_backend
@property
def user(self):
return self._user_config_backend
@property
def org(self):
return self._org_config_backend

View File

@ -1,2 +0,0 @@
from rate_limits import RateLimitsConfig, RateLimitsConfigBuilder
from legacy_rate_limits import RateLimitsConfigLegacyBuilder

View File

@ -1,7 +1,7 @@
from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig
from cartodb_services.tools import RateLimiter
from cartodb_services.refactor.config import RateLimitsConfigLegacyBuilder
from cartodb_services.config import RateLimitsConfigLegacyBuilder
from cartodb_services.tools.service_manager import ServiceManagerBase
import plpy

View File

@ -2,12 +2,8 @@ from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger
from cartodb_services.tools import RateLimiter
from cartodb_services.refactor.tools.logger import LoggerConfigBuilder
from cartodb_services.refactor.core.environment import ServerEnvironmentBuilder
from cartodb_services.refactor.backend.server_config import ServerConfigBackendFactory
from cartodb_services.refactor.backend.user_config import UserConfigBackendFactory
from cartodb_services.refactor.backend.org_config import OrgConfigBackendFactory
from cartodb_services.refactor.backend.redis_metrics_connection import RedisMetricsConnectionFactory
from cartodb_services.refactor.config import RateLimitsConfigBuilder
from cartodb_services.config import ServiceConfiguration, RateLimitsConfigBuilder
class RateLimitExceeded(Exception):
def __str__(self):
@ -52,31 +48,6 @@ class ServiceManagerBase:
@property
def logger(self):
return self.logger
class ServiceConfiguration:
def __init__(self, service, username, orgname):
self._server_config_backend = ServerConfigBackendFactory().get()
self._environment = ServerEnvironmentBuilder(server_config_backend).get()
self._user_config_backend = UserConfigBackendFactory(username, environment, server_config_backend).get()
self._org_config_backend = OrgConfigBackendFactory(orgname, environment, server_config_backend).get()
@property
def environment(self):
return self._environment
@property
def server(self):
return self._server_config_backend
@property
def user(self):
return self._user_config_backend
@property
def org(self):
return self._org_config_backend
class ServiceManager(ServiceManagerBase):
"""
This service manager delegates the configuration parameter details,
@ -85,7 +56,7 @@ class ServiceManager(ServiceManagerBase):
"""
def __init__(self, service, config_builder, username, orgname):
service_config = ServerConfiguration(service, username, orgname)
service_config = ServiceConfiguration(service, username, orgname)
logger_config = LoggerConfigBuilder(service_config.environment, service_config.server).get()
self.logger = Logger(logger_config)