From fa4a4a37d6ce931a9832677dea1edffd228300a4 Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Thu, 10 May 2012 16:25:31 -0500 Subject: Move get_client_class() to common.utils * add constants for API_NAME Change-Id: I8ccf72f032227e0a452d96303181549b1b11a5d1 --- openstackclient/common/utils.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'openstackclient/common/utils.py') 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) -- cgit v1.2.1