Move get_client_class() to common.utils

* add constants for API_NAME

Change-Id: I8ccf72f032227e0a452d96303181549b1b11a5d1
This commit is contained in:
Dean Troyer 2012-05-10 16:25:31 -05:00
parent 5378322906
commit fa4a4a37d6
4 changed files with 34 additions and 24 deletions
openstackclient

@ -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)

@ -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

@ -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(

@ -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__)