[Clients] Rework osclients.Clients.register into decorator
Using this method as decorator looks better. Change-Id: Iaa12d3c5dbc5093d18dac04698a6cbe58adee3fc
This commit is contained in:
parent
1834286973
commit
97a6976e05
@ -431,27 +431,32 @@ class Clients(object):
|
||||
return services_data
|
||||
|
||||
@classmethod
|
||||
def register(cls, client_name, client_func):
|
||||
"""Add new OpenStack client dynamically.
|
||||
def register(cls, client_name):
|
||||
"""Decorator that adds new OpenStack client dynamically.
|
||||
|
||||
:param client_name: str name how client will be named in Rally clients
|
||||
:param client_func: function that implememnts client spawning.
|
||||
It will be added to Clients in runtime, so its
|
||||
sole argument is Clients instance
|
||||
|
||||
Decorated function will be added to Clients in runtime, so its sole
|
||||
argument is a Clients instance.
|
||||
|
||||
Example:
|
||||
>>> def supernova(self):
|
||||
>>> from rally import osclients
|
||||
>>> @osclients.Clients.register("supernova")
|
||||
... def another_nova_client(self):
|
||||
... from novaclient import client as nova
|
||||
... return nova.Client("2", auth_token=self.keystone().auth_token,
|
||||
... **self._get_auth_info(password_key="key"))
|
||||
...
|
||||
>>> from rally import osclients
|
||||
>>> osclients.Clients.register("supernova", supernova)
|
||||
>>> clients = osclients.Clients.create_from_env()
|
||||
>>> clients.supernova().services.list()[:2]
|
||||
[<Service: nova-conductor>, <Service: nova-cert>]
|
||||
"""
|
||||
if hasattr(cls, client_name):
|
||||
raise ValueError(
|
||||
_("Can not register client: name already exists: %s")
|
||||
% client_name)
|
||||
setattr(cls, client_name, cached(client_func))
|
||||
def wrap(client_func):
|
||||
if hasattr(cls, client_name):
|
||||
raise ValueError(
|
||||
_("Can not register client: name already exists: %s")
|
||||
% client_name)
|
||||
setattr(cls, client_name, cached(client_func))
|
||||
return client_func
|
||||
|
||||
return wrap
|
||||
|
@ -475,12 +475,13 @@ class OSClientsTestCase(test.TestCase):
|
||||
|
||||
self.assertFalse(hasattr(clients, "foo"))
|
||||
|
||||
osclients.Clients.register("foo", client_func)
|
||||
func = osclients.Clients.register("foo")(client_func)
|
||||
|
||||
mock_cached.assert_called_once_with(client_func)
|
||||
self.assertEqual("cached_foo_client", clients.foo())
|
||||
self.assertEqual(client_func, func)
|
||||
self.assertEqual(cached_client_func, clients.foo)
|
||||
|
||||
# Call second time with same name
|
||||
self.assertRaises(ValueError,
|
||||
osclients.Clients.register, "foo", client_func)
|
||||
osclients.Clients.register("foo"), client_func)
|
||||
|
Loading…
Reference in New Issue
Block a user