Add identity API version discovery

This patch adds a utility to the keystone API manager to retrieve the
identity endpoint's version data and retrieve the current version as a
tuple. As part of this work, this patch converts the deprecated usage of
keystoneclient alone to using keystoneclient in combination with a
keystoneauth session.

Change-Id: I37031b67ab2681e81022a75afcb4f41c5700c47b
This commit is contained in:
Colleen Murphy 2018-04-05 23:01:50 +02:00
parent ee97327186
commit 656490fee2
2 changed files with 32 additions and 9 deletions

View File

@ -25,6 +25,8 @@ from django.utils.translation import ugettext_lazy as _
import six import six
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
from keystoneauth1 import session
from keystoneauth1 import token_endpoint
from keystoneclient import exceptions as keystone_exceptions from keystoneclient import exceptions as keystone_exceptions
from openstack_auth import backend from openstack_auth import backend
@ -154,7 +156,7 @@ def keystoneclient(request, admin=False):
The client is cached so that subsequent API calls during the same The client is cached so that subsequent API calls during the same
request/response cycle don't have to be re-authenticated. request/response cycle don't have to be re-authenticated.
""" """
api_version = VERSIONS.get_active_version() client_version = VERSIONS.get_active_version()
user = request.user user = request.user
token_id = user.token.id token_id = user.token.id
@ -184,21 +186,29 @@ def keystoneclient(request, admin=False):
conn = getattr(request, cache_attr) conn = getattr(request, cache_attr)
else: else:
endpoint = _get_endpoint_url(request, endpoint_type) endpoint = _get_endpoint_url(request, endpoint_type)
insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) verify = not getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
verify = verify and cacert
LOG.debug("Creating a new keystoneclient connection to %s.", endpoint) LOG.debug("Creating a new keystoneclient connection to %s.", endpoint)
remote_addr = request.environ.get('REMOTE_ADDR', '') remote_addr = request.environ.get('REMOTE_ADDR', '')
conn = api_version['client'].Client(token=token_id, token_auth = token_endpoint.Token(endpoint=endpoint,
endpoint=endpoint, token=token_id)
original_ip=remote_addr, keystone_session = session.Session(auth=token_auth,
insecure=insecure, original_ip=remote_addr,
cacert=cacert, verify=verify)
auth_url=endpoint, conn = client_version['client'].Client(session=keystone_session,
debug=settings.DEBUG) debug=settings.DEBUG)
setattr(request, cache_attr, conn) setattr(request, cache_attr, conn)
return conn return conn
@profiler.trace
def get_identity_api_version(request):
client = keystoneclient(request)
endpoint_data = client.session.get_endpoint_data(service_type='identity')
return endpoint_data.api_version
@profiler.trace @profiler.trace
def domain_create(request, name, description=None, enabled=None): def domain_create(request, name, description=None, enabled=None):
manager = keystoneclient(request, admin=True).domains manager = keystoneclient(request, admin=True).domains

View File

@ -104,3 +104,16 @@ class ServiceAPITests(test.APIMockTestCase):
self.assertEqual("http://public.nova2.example.com:8774/v2", self.assertEqual("http://public.nova2.example.com:8774/v2",
service.public_url) service.public_url)
self.assertEqual("int.nova2.example.com", service.host) self.assertEqual("int.nova2.example.com", service.host)
class APIVersionTests(test.APIMockTestCase):
@mock.patch.object(api.keystone, 'keystoneclient')
def test_get_identity_api_version(self, mock_keystoneclient):
keystoneclient = mock_keystoneclient.return_value
endpoint_data = mock.Mock()
endpoint_data.api_version = (3, 10)
keystoneclient.session.get_endpoint_data.return_value = endpoint_data
api_version = api.keystone.get_identity_api_version(self.request)
keystoneclient.session.get_endpoint_data.assert_called_once_with(
service_type='identity')
self.assertEqual((3, 10), api_version)