From 275a6dc27f82be36218128ab4a5811e13fce06ba Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 3 Oct 2016 13:33:46 +0200 Subject: [PATCH] Move redis_config related classes to separate file --- server/extension/sql/20_geocode_street.sql | 3 +- .../refactor/storage/redis_config.py | 38 +++++++++++++++++++ .../storage/redis_connection_config.py | 10 ----- .../refactor/storage/server_config.py | 36 ------------------ 4 files changed, 40 insertions(+), 47 deletions(-) create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_config.py diff --git a/server/extension/sql/20_geocode_street.sql b/server/extension/sql/20_geocode_street.sql index 9ab6900..e457206 100644 --- a/server/extension/sql/20_geocode_street.sql +++ b/server/extension/sql/20_geocode_street.sql @@ -143,7 +143,8 @@ RETURNS Geometry AS $$ from cartodb_services.mapzen.types import country_to_iso3 from cartodb_services.metrics import QuotaService from cartodb_services.tools import Logger - from cartodb_services.refactor.storage.server_config import InDbServerConfigStorage, UserConfigStorageFactory, OrgConfigStorageFactory + from cartodb_services.refactor.storage.server_config import InDbServerConfigStorage + from cartodb_services.refactor.storage.redis_config import UserConfigStorageFactory, OrgConfigStorageFactory from cartodb_services.refactor.tools.logger import LoggerConfigBuilder from cartodb_services.refactor.tools.redis_mock import RedisConnectionMock from cartodb_services.refactor.storage.redis_connection_config import RedisMetadataConnectionConfigBuilder, RedisMetricsConnectionConfigBuilder diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_config.py b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_config.py new file mode 100644 index 0000000..43e61bb --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_config.py @@ -0,0 +1,38 @@ +from interfaces import ConfigStorageInterface +from null_config import NullConfigStorage + + +class RedisConfigStorage(ConfigStorageInterface): + + def __init__(self, connection, config_key): + self._connection = connection + self._config_key = config_key + self._data = None + + def get(self, key): + if not self._data: + self._data = self._connection.hgetall(self._config_key) + return self._data[key] + + +class UserConfigStorageFactory(object): + # TODO rework to support onpremise and InDbStorage + def __init__(self, redis_connection, username): + self._redis_connection = redis_connection + self._username = username + + def get(self): + return RedisConfigStorage(self._redis_connection, 'rails:users:{0}'.format(self._username)) + + +class OrgConfigStorageFactory(object): + # TODO rework to support onpremise and InDbStorage + def __init__(self, redis_connection, orgname): + self._redis_connection = redis_connection + self._orgname = orgname + + def get(self): + if self._orgname: + return RedisConfigStorage(self._redis_connection, 'rails:orgs:{0}'.format(self._orgname)) + else: + return NullConfigStorage() diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_connection_config.py b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_connection_config.py index 96346eb..97eb25c 100644 --- a/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_connection_config.py +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/redis_connection_config.py @@ -1,15 +1,6 @@ from cartodb_services.refactor.config.exceptions import ConfigException from abc import ABCMeta, abstractmethod -""" -How to use this (just a draft, WIP): - - redis_connection_config = RedisConnectionConfigBuilder(server_config_storage).get() - connection = RedisConnectionBuilder(redis_connection_config) - - user_config_storage = RedisConfigStorage(connection, user) - user_config_storage = UserConfigStorageFactory(environment).get() -""" class RedisConnectionConfig(object): """ @@ -17,7 +8,6 @@ class RedisConnectionConfig(object): a connection to a redis server. """ - def __init__(self, host, port, timeout, db, sentinel_id): self._host = host self._port = port diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/storage/server_config.py b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/server_config.py index eb54bac..0597d2b 100644 --- a/server/lib/python/cartodb_services/cartodb_services/refactor/storage/server_config.py +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/storage/server_config.py @@ -1,7 +1,6 @@ import json import cartodb_services from interfaces import ConfigStorageInterface -from null_config import NullConfigStorage class InDbServerConfigStorage(ConfigStorageInterface): @@ -13,38 +12,3 @@ class InDbServerConfigStorage(ConfigStorageInterface): return json.loads(json_output) else: return None - - -# TODO move out of this file. In general this is config but either user or org config -class RedisConfigStorage(ConfigStorageInterface): - - def __init__(self, connection, config_key): - self._connection = connection - self._config_key = config_key - self._data = None - - def get(self, key): - if not self._data: - self._data = self._connection.hgetall(self._config_key) - return self._data[key] - -class UserConfigStorageFactory(object): - # TODO rework to support onpremise and InDbStorage - def __init__(self, redis_connection, username): - self._redis_connection = redis_connection - self._username = username - - def get(self): - return RedisConfigStorage(self._redis_connection, 'rails:users:{0}'.format(self._username)) - -class OrgConfigStorageFactory(object): - # TODO rework to support onpremise and InDbStorage - def __init__(self, redis_connection, orgname): - self._redis_connection = redis_connection - self._orgname = orgname - - def get(self): - if self._orgname: - return RedisConfigStorage(self._redis_connection, 'rails:orgs:{0}'.format(self._orgname)) - else: - return NullConfigStorage()