Skip to content

Commit fa4a4a3

Browse files
author
Dean Troyer
committed
Move get_client_class() to common.utils
* add constants for API_NAME Change-Id: I8ccf72f032227e0a452d96303181549b1b11a5d1
1 parent 5378322 commit fa4a4a3

4 files changed

Lines changed: 34 additions & 24 deletions

File tree

‎openstackclient/common/utils.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,29 @@ def env(*vars, **kwargs):
9595

9696

9797
def import_class(import_str):
98-
"""Returns a class from a string including module and class."""
98+
"""Returns a class from a string including module and class
99+
100+
:param import_str: a string representation of the class name
101+
:rtype: the requested class
102+
"""
99103
mod_str, _sep, class_str = import_str.rpartition('.')
100104
__import__(mod_str)
101105
return getattr(sys.modules[mod_str], class_str)
106+
107+
108+
def get_client_class(api_name, version, version_map):
109+
"""Returns the client class for the requested API version
110+
111+
:param api_name: the name of the API, e.g. 'compute', 'image', etc
112+
:param version: the requested API version
113+
:param version_map: a dict of client classes keyed by version
114+
:rtype: a client class for the requested API version
115+
"""
116+
try:
117+
client_path = version_map[str(version)]
118+
except (KeyError, ValueError):
119+
msg = "Invalid %s client version '%s'. must be one of: %s" % (
120+
(api_name, version, ', '.join(version_map.keys())))
121+
raise exc.UnsupportedVersion(msg)
122+
123+
return import_class(client_path)

‎openstackclient/compute/client.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
LOG = logging.getLogger(__name__)
2323

24+
API_NAME = 'compute'
25+
2426

2527
def make_client(instance):
2628
"""Returns a compute service client.
2729
"""
2830
LOG.debug('instantiating compute client')
2931
client = nova_client.Client(
30-
version=instance._api_version['compute'],
32+
version=instance._api_version[API_NAME],
3133
username=instance._username,
3234
api_key=instance._password,
3335
project_id=instance._tenant_name,
@@ -39,7 +41,7 @@ def make_client(instance):
3941
endpoint_type='publicURL',
4042
# FIXME(dhellmann): add extension discovery
4143
extensions=[],
42-
service_type='compute',
44+
service_type=API_NAME,
4345
# FIXME(dhellmann): what is service_name?
4446
service_name='',
4547
)
@@ -51,7 +53,7 @@ def make_client(instance):
5153
else:
5254
# password flow
5355
client.client.management_url = instance.get_endpoint_for_service_type(
54-
'compute')
56+
API_NAME)
5557
client.client.service_catalog = instance._service_catalog
5658
client.client.auth_token = instance._token
5759
return client

‎openstackclient/identity/client.py‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,20 @@
2222

2323
LOG = logging.getLogger(__name__)
2424

25+
API_NAME = 'identity'
2526
API_VERSIONS = {
2627
'2.0': 'keystoneclient.v2_0.client.Client',
2728
}
2829

2930

30-
def get_client_class(version):
31-
"""Returns the client class for the requested API version
32-
"""
33-
try:
34-
client_path = API_VERSIONS[str(version)]
35-
except (KeyError, ValueError):
36-
msg = "Invalid client version '%s'. must be one of: %s" % (
37-
(version, ', '.join(API_VERSIONS.keys())))
38-
raise exc.UnsupportedVersion(msg)
39-
40-
return utils.import_class(client_path)
41-
42-
4331
def make_client(instance):
4432
"""Returns an identity service client.
4533
"""
46-
identity_client = get_client_class(instance._api_version['identity'])
34+
identity_client = utils.get_client_class(
35+
API_NAME,
36+
instance._api_version[API_NAME],
37+
API_VERSIONS,
38+
)
4739
if instance._url:
4840
LOG.debug('instantiating identity client: token flow')
4941
client = identity_client(

‎openstackclient/shell.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ def initialize_app(self, argv):
201201
if cmd_name != 'help':
202202
self.authenticate_user()
203203

204-
self.log.debug("API: Identity=%s Compute=%s Image=%s" % (
205-
self.api_version['identity'],
206-
self.api_version['compute'],
207-
self.api_version['image'])
208-
)
209-
210204
def prepare_to_run_command(self, cmd):
211205
"""Set up auth and API versions"""
212206
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)

0 commit comments

Comments
 (0)