diff --git a/billingstack/biller/service.py b/billingstack/biller/service.py index 71cf27f..707bc14 100644 --- a/billingstack/biller/service.py +++ b/billingstack/biller/service.py @@ -16,7 +16,7 @@ from oslo.config import cfg from billingstack.openstack.common import log as logging from billingstack.openstack.common.rpc import service as rpc_service -from billingstack.biller import storage +from billingstack.storage.utils import get_connection cfg.CONF.import_opt('biller_topic', 'billingstack.biller.rpcapi') @@ -39,5 +39,5 @@ class Service(rpc_service.Service): super(Service, self).__init__(*args, **kwargs) def start(self): - self.storage_conn = storage.get_connection() + self.storage_conn = get_connection('biller') super(Service, self).start() diff --git a/billingstack/biller/storage/__init__.py b/billingstack/biller/storage/__init__.py index 5aef594..f9024d0 100644 --- a/billingstack/biller/storage/__init__.py +++ b/billingstack/biller/storage/__init__.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -from oslo.config import cfg from billingstack.storage import base @@ -25,9 +24,3 @@ class StorageEngine(base.StorageEngine): class Connection(base.Connection): """Define the base API for biller storage""" - - -def get_connection(): - name = cfg.CONF['service:biller'].storage_driver - plugin = StorageEngine.get_plugin(name, invoke_on_load=True) - return plugin.get_connection() diff --git a/billingstack/central/service.py b/billingstack/central/service.py index e03785f..8d97480 100644 --- a/billingstack/central/service.py +++ b/billingstack/central/service.py @@ -20,7 +20,7 @@ from oslo.config import cfg from billingstack.openstack.common import log as logging from billingstack.openstack.common.rpc import service as rpc_service from billingstack.openstack.common import service as os_service -from billingstack.central import storage +from billingstack.storage.utils import get_connection from billingstack import service as bs_service @@ -41,7 +41,7 @@ class Service(rpc_service.Service): super(Service, self).__init__(*args, **kwargs) def start(self): - self.storage_conn = storage.get_connection() + self.storage_conn = get_connection('central') super(Service, self).start() def __getattr__(self, name): diff --git a/billingstack/central/storage/__init__.py b/billingstack/central/storage/__init__.py index d58b705..1ebda20 100644 --- a/billingstack/central/storage/__init__.py +++ b/billingstack/central/storage/__init__.py @@ -15,7 +15,6 @@ # under the License. # # Copied: Moniker -from oslo.config import cfg from billingstack.openstack.common import log as logging from billingstack.storage import base @@ -30,29 +29,3 @@ class StorageEngine(base.StorageEngine): class Connection(base.Connection): pass - - -def get_engine(engine_name): - """ - Return the engine class from the provided engine name - """ - return StorageEngine.get_plugin(engine_name, invoke_on_load=True) - - -def get_connection(): - engine = get_engine(cfg.CONF['service:central'].storage_driver) - return engine.get_connection() - - -def setup_schema(): - """ Create the DB - Used for testing purposes """ - LOG.debug("Setting up Schema") - connection = get_connection() - connection.setup_schema() - - -def teardown_schema(): - """ Reset the DB to default - Used for testing purposes """ - LOG.debug("Tearing down Schema") - connection = get_connection() - connection.teardown_schema() diff --git a/billingstack/rater/service.py b/billingstack/rater/service.py index dbc4c63..351e80d 100644 --- a/billingstack/rater/service.py +++ b/billingstack/rater/service.py @@ -19,7 +19,7 @@ from oslo.config import cfg from billingstack.openstack.common import log as logging from billingstack.openstack.common import service as os_service from billingstack.openstack.common.rpc import service as rpc_service -from billingstack.rater import storage +from billingstack.storage.utils import get_connection from billingstack import service as bs_service @@ -47,7 +47,7 @@ class Service(rpc_service.Service): super(Service, self).__init__(*args, **kwargs) def start(self): - self.storage_conn = storage.get_connection() + self.storage_conn = get_connection('rater') super(Service, self).start() def create_usage(self, ctxt, values): diff --git a/billingstack/storage/utils.py b/billingstack/storage/utils.py new file mode 100644 index 0000000..992d892 --- /dev/null +++ b/billingstack/storage/utils.py @@ -0,0 +1,38 @@ +# -*- encoding: utf-8 -*- +# +# Author: Endre Karlson +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from oslo.config import cfg +from billingstack.openstack.common import importutils + + +def get_engine(service_name, driver_name): + """ + Return the engine class from the provided engine name + """ + path = 'billingstack.%s.storage.StorageEngine' % service_name + base = importutils.import_object(path) + return base.get_plugin(driver_name, invoke_on_load=True) + + +def get_connection(service_name, driver_name=None): + """ + Return a instance of a storage connection + """ + driver_name = driver_name or \ + cfg.CONF['service:%s' % service_name].storage_driver + engine = get_engine(service_name, driver_name) + return engine.get_connection() diff --git a/billingstack/tests/base.py b/billingstack/tests/base.py index f726ca9..83abc9f 100644 --- a/billingstack/tests/base.py +++ b/billingstack/tests/base.py @@ -14,6 +14,7 @@ from oslo.config import cfg from billingstack import exceptions from billingstack import paths from billingstack import samples +from billingstack.storage import utils as storage_utils from billingstack.openstack.common.context import RequestContext, \ get_admin_context from billingstack.openstack.common import importutils @@ -142,10 +143,6 @@ class StorageFixture(fixtures.Fixture): group=self.svc_group) set_config(storage_driver=self.driver, group=self.svc_group) - # FIXME: Move this to a generic get_storage() method instead? - self.module = importutils.import_module( - 'billingstack.%s.storage' % self.svc) - # FIXME: Workout a way to support the different storage types self.helper = SQLAlchemyHelper(self) @@ -175,11 +172,8 @@ class StorageFixture(fixtures.Fixture): """ Import the storage module for the service that we are going to act on, then return a connection object for that storage module. - - :param service: The service. """ - engine = self.module.get_engine(self.driver) - return engine.get_connection() + return storage_utils.get_connection(self.svc, self.driver) class ServiceFixture(fixtures.Fixture):