Add Identity to ClientManager

* Make the Identity client in identity.client.make_client()
* Auth via ClientManager.identity
* Skip extra auth roundtrip in compute client

Change-Id: I0190639e38f83997c233195f6cc27ff3afdfba10
This commit is contained in:
Dean Troyer 2012-05-04 12:28:35 -05:00
parent 5e4032150d
commit 70b3246a19
4 changed files with 39 additions and 17 deletions
openstackclient
tools

@ -5,8 +5,7 @@ import logging
from openstackclient.common import exceptions as exc from openstackclient.common import exceptions as exc
from openstackclient.compute import client as compute_client from openstackclient.compute import client as compute_client
from openstackclient.identity import client as identity_client
from keystoneclient.v2_0 import client as keystone_client
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -31,6 +30,9 @@ class ClientManager(object):
"""Manages access to API clients, including authentication. """Manages access to API clients, including authentication.
""" """
# Identity client is instantiated in init_token()
# otherwise we have a recursion problem
identity = None
compute = ClientCache(compute_client.make_client) compute = ClientCache(compute_client.make_client)
def __init__(self, token=None, url=None, def __init__(self, token=None, url=None,
@ -82,15 +84,11 @@ class ClientManager(object):
"You must provide an auth url via" "You must provide an auth url via"
" either --os-auth-url or via env[OS_AUTH_URL]") " either --os-auth-url or via env[OS_AUTH_URL]")
kwargs = { # Get an Identity client and keep a token and catalog
'username': self._username, if not self.identity:
'password': self._password, self.identity = identity_client.make_client(self)
'tenant_id': self._tenant_id, self._token = self.identity.auth_token
'tenant_name': self._tenant_name, self._service_catalog = self.identity.service_catalog
'auth_url': self._auth_url
}
self._auth_client = keystone_client.Client(**kwargs)
self._token = self._auth_client.auth_token
return return
def get_endpoint_for_service_type(self, service_type): def get_endpoint_for_service_type(self, service_type):
@ -98,8 +96,8 @@ class ClientManager(object):
""" """
# See if we are using password flow auth, i.e. we have a # See if we are using password flow auth, i.e. we have a
# service catalog to select endpoints from # service catalog to select endpoints from
if self._auth_client and self._auth_client.service_catalog: if self._service_catalog:
endpoint = self._auth_client.service_catalog.url_for( endpoint = self._service_catalog.url_for(
service_type=service_type) service_type=service_type)
else: else:
# Hope we were given the correct URL. # Hope we were given the correct URL.

@ -9,8 +9,6 @@ def make_client(instance):
"""Returns a compute service client. """Returns a compute service client.
""" """
LOG.debug('instantiating compute client') LOG.debug('instantiating compute client')
# FIXME(dhellmann): Where is the endpoint value used?
# url = instance.get_endpoint_for_service_type('compute')
client = nova_client.Client( client = nova_client.Client(
version=instance._compute_api_version, version=instance._compute_api_version,
username=instance._username, username=instance._username,
@ -28,5 +26,10 @@ def make_client(instance):
# FIXME(dhellmann): what is service_name? # FIXME(dhellmann): what is service_name?
service_name='', service_name='',
) )
client.authenticate()
# Populate the Nova client to skip another auth query to Identity
client.client.management_url = instance.get_endpoint_for_service_type(
'compute')
client.client.service_catalog = instance._service_catalog
client.client.auth_token = instance._token
return client return client

@ -0,0 +1,20 @@
import logging
from keystoneclient.v2_0 import client as identity_client
LOG = logging.getLogger(__name__)
def make_client(instance):
"""Returns an identity service client.
"""
LOG.debug('instantiating identity client')
client = identity_client.Client(
username=instance._username,
password=instance._password,
tenant_name=instance._tenant_name,
tenant_id=instance._tenant_id,
auth_url=instance._auth_url,
region_name=instance._region_name,
)
return client

@ -6,4 +6,5 @@ mock
prettytable prettytable
simplejson simplejson
-e git://github.com/openstack/python-keystoneclient.git#egg=python-keystoneclient -e git://github.com/openstack/python-keystoneclient.git#egg=python-keystoneclient
-e git+https://github.com/openstack/python-novaclient.git#egg=python_novaclient -e git+https://github.com/openstack/python-novaclient.git#egg=python_novaclient