Skip to content

Commit 712a8c7

Browse files
author
Dean Troyer
committed
Add API versioning support
* Specific versions supported are managed in XXXXXX.client.py with a mapping from version to client class. This is based on the scheme that is included in novaclient; none of the other client libs have that capability. Change-Id: I930b197f1189e7f52c3b0096e73e0773cf925542
1 parent 9d224b3 commit 712a8c7

6 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎openstackclient/common/clientmanager.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ def __init__(self, token=None, url=None,
5454
tenant_name=None, tenant_id=None,
5555
username=None, password=None,
5656
region_name=None,
57-
identity_api_version=None,
58-
compute_api_version=None,
59-
image_api_version=None,
57+
api_version=None,
6058
):
6159
self._token = token
6260
self._url = url
@@ -66,9 +64,7 @@ def __init__(self, token=None, url=None,
6664
self._username = username
6765
self._password = password
6866
self._region_name = region_name
69-
self._identity_api_version = identity_api_version
70-
self._compute_api_version = compute_api_version
71-
self._image_api_version = image_api_version
67+
self._api_version = api_version
7268
self._service_catalog = None
7369

7470
if not self._url:

‎openstackclient/common/exceptions.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class EndpointNotFound(Exception):
3939
pass
4040

4141

42+
class UnsupportedVersion(Exception):
43+
"""Indicates that the user is trying to use an unsupported
44+
version of the API"""
45+
pass
46+
47+
4248
class ClientException(Exception):
4349
"""
4450
The base exception class for all exceptions this library raises.

‎openstackclient/common/utils.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222
import os
23+
import sys
2324
import uuid
2425

2526
import prettytable
@@ -67,3 +68,10 @@ def env(*vars, **kwargs):
6768
if value:
6869
return value
6970
return kwargs.get('default', '')
71+
72+
73+
def import_class(import_str):
74+
"""Returns a class from a string including module and class."""
75+
mod_str, _sep, class_str = import_str.rpartition('.')
76+
__import__(mod_str)
77+
return getattr(sys.modules[mod_str], class_str)

‎openstackclient/compute/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def make_client(instance):
2727
"""
2828
LOG.debug('instantiating compute client')
2929
client = nova_client.Client(
30-
version=instance._compute_api_version,
30+
version=instance._api_version['compute'],
3131
username=instance._username,
3232
api_key=instance._password,
3333
project_id=instance._tenant_name,

‎openstackclient/identity/client.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,42 @@
1717

1818
import logging
1919

20-
from keystoneclient.v2_0 import client as identity_client
20+
from openstackclient.common import exceptions as exc
21+
from openstackclient.common import utils
2122

2223
LOG = logging.getLogger(__name__)
2324

25+
API_VERSIONS = {
26+
'2.0': 'keystoneclient.v2_0.client.Client',
27+
}
28+
29+
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+
2442

2543
def make_client(instance):
2644
"""Returns an identity service client.
2745
"""
46+
identity_client = get_client_class(instance._api_version['identity'])
2847
if instance._url:
2948
LOG.debug('instantiating identity client: token flow')
30-
client = identity_client.Client(
49+
client = identity_client(
3150
endpoint=instance._url,
3251
token=instance._token,
3352
)
3453
else:
3554
LOG.debug('instantiating identity client: password flow')
36-
client = identity_client.Client(
55+
client = identity_client(
3756
username=instance._username,
3857
password=instance._password,
3958
tenant_name=instance._tenant_name,

‎openstackclient/shell.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ def authenticate_user(self):
172172
username=self.options.os_username,
173173
password=self.options.os_password,
174174
region_name=self.options.os_region_name,
175-
identity_api_version=self.options.os_identity_api_version,
176-
compute_api_version=self.options.os_compute_api_version,
177-
image_api_version=self.options.os_image_api_version,
175+
api_version=self.api_version,
178176
)
179177
return
180178

0 commit comments

Comments
 (0)