[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
|
return services_data
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register(cls, client_name, client_func):
|
def register(cls, client_name):
|
||||||
"""Add new OpenStack client dynamically.
|
"""Decorator that adds new OpenStack client dynamically.
|
||||||
|
|
||||||
:param client_name: str name how client will be named in Rally clients
|
: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
|
Decorated function will be added to Clients in runtime, so its sole
|
||||||
sole argument is Clients instance
|
argument is a Clients instance.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> def supernova(self):
|
>>> from rally import osclients
|
||||||
|
>>> @osclients.Clients.register("supernova")
|
||||||
|
... def another_nova_client(self):
|
||||||
... from novaclient import client as nova
|
... from novaclient import client as nova
|
||||||
... return nova.Client("2", auth_token=self.keystone().auth_token,
|
... return nova.Client("2", auth_token=self.keystone().auth_token,
|
||||||
... **self._get_auth_info(password_key="key"))
|
... **self._get_auth_info(password_key="key"))
|
||||||
...
|
...
|
||||||
>>> from rally import osclients
|
|
||||||
>>> osclients.Clients.register("supernova", supernova)
|
|
||||||
>>> clients = osclients.Clients.create_from_env()
|
>>> clients = osclients.Clients.create_from_env()
|
||||||
>>> clients.supernova().services.list()[:2]
|
>>> clients.supernova().services.list()[:2]
|
||||||
[<Service: nova-conductor>, <Service: nova-cert>]
|
[<Service: nova-conductor>, <Service: nova-cert>]
|
||||||
"""
|
"""
|
||||||
if hasattr(cls, client_name):
|
def wrap(client_func):
|
||||||
raise ValueError(
|
if hasattr(cls, client_name):
|
||||||
_("Can not register client: name already exists: %s")
|
raise ValueError(
|
||||||
% client_name)
|
_("Can not register client: name already exists: %s")
|
||||||
setattr(cls, client_name, cached(client_func))
|
% 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"))
|
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)
|
mock_cached.assert_called_once_with(client_func)
|
||||||
self.assertEqual("cached_foo_client", clients.foo())
|
self.assertEqual("cached_foo_client", clients.foo())
|
||||||
|
self.assertEqual(client_func, func)
|
||||||
self.assertEqual(cached_client_func, clients.foo)
|
self.assertEqual(cached_client_func, clients.foo)
|
||||||
|
|
||||||
# Call second time with same name
|
# Call second time with same name
|
||||||
self.assertRaises(ValueError,
|
self.assertRaises(ValueError,
|
||||||
osclients.Clients.register, "foo", client_func)
|
osclients.Clients.register("foo"), client_func)
|
||||||
|
Loading…
Reference in New Issue
Block a user