summaryrefslogtreecommitdiff
path: root/openstackclient/common/utils.py
diff options
context:
space:
mode:
authorDean Troyer <dtroyer@gmail.com>2012-05-10 16:25:31 -0500
committerDean Troyer <dtroyer@gmail.com>2012-05-11 13:49:45 -0500
commitfa4a4a37d6ce931a9832677dea1edffd228300a4 (patch)
treecc54f5d6db37cce400de5eab2128145135e8bfd9 /openstackclient/common/utils.py
parent5378322906a636bc2b9685e7403950549ef213f5 (diff)
downloadpython-openstackclient-fa4a4a37d6ce931a9832677dea1edffd228300a4.tar.gz
Move get_client_class() to common.utils
* add constants for API_NAME Change-Id: I8ccf72f032227e0a452d96303181549b1b11a5d1
Diffstat (limited to 'openstackclient/common/utils.py')
-rw-r--r--openstackclient/common/utils.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py
index 6c613d94..b37ff806 100644
--- a/openstackclient/common/utils.py
+++ b/openstackclient/common/utils.py
@@ -95,7 +95,29 @@ def env(*vars, **kwargs):
def import_class(import_str):
- """Returns a class from a string including module and class."""
+ """Returns a class from a string including module and class
+
+ :param import_str: a string representation of the class name
+ :rtype: the requested class
+ """
mod_str, _sep, class_str = import_str.rpartition('.')
__import__(mod_str)
return getattr(sys.modules[mod_str], class_str)
+
+
+def get_client_class(api_name, version, version_map):
+ """Returns the client class for the requested API version
+
+ :param api_name: the name of the API, e.g. 'compute', 'image', etc
+ :param version: the requested API version
+ :param version_map: a dict of client classes keyed by version
+ :rtype: a client class for the requested API version
+ """
+ try:
+ client_path = version_map[str(version)]
+ except (KeyError, ValueError):
+ msg = "Invalid %s client version '%s'. must be one of: %s" % (
+ (api_name, version, ', '.join(version_map.keys())))
+ raise exc.UnsupportedVersion(msg)
+
+ return import_class(client_path)