summaryrefslogtreecommitdiff
path: root/openstackclient
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
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')
-rw-r--r--openstackclient/common/utils.py24
-rw-r--r--openstackclient/compute/client.py8
-rw-r--r--openstackclient/identity/client.py20
-rw-r--r--openstackclient/shell.py6
4 files changed, 34 insertions, 24 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)
diff --git a/openstackclient/compute/client.py b/openstackclient/compute/client.py
index fa37ff55..a59b6e00 100644
--- a/openstackclient/compute/client.py
+++ b/openstackclient/compute/client.py
@@ -21,13 +21,15 @@ from novaclient import client as nova_client
LOG = logging.getLogger(__name__)
+API_NAME = 'compute'
+
def make_client(instance):
"""Returns a compute service client.
"""
LOG.debug('instantiating compute client')
client = nova_client.Client(
- version=instance._api_version['compute'],
+ version=instance._api_version[API_NAME],
username=instance._username,
api_key=instance._password,
project_id=instance._tenant_name,
@@ -39,7 +41,7 @@ def make_client(instance):
endpoint_type='publicURL',
# FIXME(dhellmann): add extension discovery
extensions=[],
- service_type='compute',
+ service_type=API_NAME,
# FIXME(dhellmann): what is service_name?
service_name='',
)
@@ -51,7 +53,7 @@ def make_client(instance):
else:
# password flow
client.client.management_url = instance.get_endpoint_for_service_type(
- 'compute')
+ API_NAME)
client.client.service_catalog = instance._service_catalog
client.client.auth_token = instance._token
return client
diff --git a/openstackclient/identity/client.py b/openstackclient/identity/client.py
index 318bfe32..b7066e5e 100644
--- a/openstackclient/identity/client.py
+++ b/openstackclient/identity/client.py
@@ -22,28 +22,20 @@ from openstackclient.common import utils
LOG = logging.getLogger(__name__)
+API_NAME = 'identity'
API_VERSIONS = {
'2.0': 'keystoneclient.v2_0.client.Client',
}
-def get_client_class(version):
- """Returns the client class for the requested API version
- """
- try:
- client_path = API_VERSIONS[str(version)]
- except (KeyError, ValueError):
- msg = "Invalid client version '%s'. must be one of: %s" % (
- (version, ', '.join(API_VERSIONS.keys())))
- raise exc.UnsupportedVersion(msg)
-
- return utils.import_class(client_path)
-
-
def make_client(instance):
"""Returns an identity service client.
"""
- identity_client = get_client_class(instance._api_version['identity'])
+ identity_client = utils.get_client_class(
+ API_NAME,
+ instance._api_version[API_NAME],
+ API_VERSIONS,
+ )
if instance._url:
LOG.debug('instantiating identity client: token flow')
client = identity_client(
diff --git a/openstackclient/shell.py b/openstackclient/shell.py
index 94d7c496..c6242220 100644
--- a/openstackclient/shell.py
+++ b/openstackclient/shell.py
@@ -201,12 +201,6 @@ class OpenStackShell(App):
if cmd_name != 'help':
self.authenticate_user()
- self.log.debug("API: Identity=%s Compute=%s Image=%s" % (
- self.api_version['identity'],
- self.api_version['compute'],
- self.api_version['image'])
- )
-
def prepare_to_run_command(self, cmd):
"""Set up auth and API versions"""
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)