diff --git a/etc/heat/heat.conf.sample b/etc/heat/heat.conf.sample index d7e9f24c7f..6ecc493e7a 100644 --- a/etc/heat/heat.conf.sample +++ b/etc/heat/heat.conf.sample @@ -107,9 +107,9 @@ # Options defined in heat.engine.clients # -# Cloud module to use as a backend. Defaults to OpenStack. +# Fully qualified class name to use as a client backend. # (string value) -#cloud_backend= +#cloud_backend=heat.engine.clients.OpenStackClients # diff --git a/heat/engine/clients.py b/heat/engine/clients.py index 6deae5b15f..405533371a 100644 --- a/heat/engine/clients.py +++ b/heat/engine/clients.py @@ -46,11 +46,12 @@ except ImportError: ceilometerclient = None logger.info('ceilometerclient not available') +_default_backend = "heat.engine.clients.OpenStackClients" cloud_opts = [ cfg.StrOpt('cloud_backend', - default=None, - help="Cloud module to use as a backend. Defaults to OpenStack.") + default=_default_backend, + help="Fully qualified class name to use as a client backend.") ] cfg.CONF.register_opts(cloud_opts) @@ -59,7 +60,6 @@ class OpenStackClients(object): ''' Convenience class to create and cache client instances. ''' - def __init__(self, context): self.context = context self._nova = {} @@ -210,10 +210,18 @@ class OpenStackClients(object): return self._ceilometer -if cfg.CONF.cloud_backend: - cloud_backend_module = importutils.import_module(cfg.CONF.cloud_backend) - Clients = cloud_backend_module.Clients -else: - Clients = OpenStackClients +class ClientBackend(object): + '''Delay choosing the backend client module until the client's class needs + to be initialized. + ''' + def __new__(cls, context): + if cfg.CONF.cloud_backend == _default_backend: + return OpenStackClients(context) + else: + return importutils.import_object( + cfg.CONF.cloud_backend, + context + ) -logger.debug('Using backend %s' % Clients) + +Clients = ClientBackend diff --git a/heat/tests/test_clients.py b/heat/tests/test_clients.py new file mode 100644 index 0000000000..6aea475866 --- /dev/null +++ b/heat/tests/test_clients.py @@ -0,0 +1,23 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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 heat.engine import clients +from heat.tests.common import HeatTestCase + + +class ClientsTest(HeatTestCase): + + def test_clients_chosen_at_module_initilization(self): + self.assertFalse(hasattr(clients.Clients, 'nova')) + self.assertTrue(hasattr(clients.Clients('fakecontext'), 'nova'))