Refactoring url_for to remove admin parameter

Removed unused admin parameter that was unused and removed tests
that were no longer necessary.

Added an option configuration setting SECONDARY_ENDPOINT_TYPE that
will be attempted if the OPENSTACK_ENDPOINT_TYPE does not exist
in the service catalog for the desired service.

The primary use case for this fix is in cloud configurations
where Keystone does not return all endpoint types for each
service, and only does so based on the user's privilege level.

Example use case would be set OPENSTACK_ENDPOINT_TYPE to 'adminURL'
and set SECONDARY_ENDPOINT_TYPE to 'publicURL'.  If adminURL is not
available to the user, then they get the publicURL back.

If SECONDARY_ENDPOINT_TYPE is not set in the settings, then the
current behavior is maintained.

Fixes: bug #1186379

Change-Id: Ieefb6ed5dd88e5c840ef6bad93ae87237a1b63f9
This commit is contained in:
David Lyle 2013-06-04 17:16:26 -06:00
parent 112df7c138
commit 3f19461a80
3 changed files with 37 additions and 29 deletions

View File

@ -198,28 +198,36 @@ ENDPOINT_TYPE_TO_INTERFACE = {
}
def url_for(request, service_type, admin=False, endpoint_type=None):
def get_url_for_service(service, endpoint_type):
identity_version = get_version_from_service(service)
for endpoint in service['endpoints']:
try:
if identity_version < 3:
return endpoint[endpoint_type]
else:
interface = ENDPOINT_TYPE_TO_INTERFACE.get(endpoint_type, '')
if endpoint['interface'] == interface:
return endpoint['url']
except (IndexError, KeyError):
pass
return None
def url_for(request, service_type, endpoint_type=None):
endpoint_type = endpoint_type or getattr(settings,
'OPENSTACK_ENDPOINT_TYPE',
'publicURL')
fallback_endpoint_type = getattr(settings, 'SECONDARY_ENDPOINT_TYPE', None)
catalog = request.user.service_catalog
service = get_service_from_catalog(catalog, service_type)
identity_version = get_version_from_service(service)
if admin:
endpoint_type = 'adminURL'
if service:
try:
if identity_version < 3:
return service['endpoints'][0][endpoint_type]
else:
interface = ENDPOINT_TYPE_TO_INTERFACE.get(endpoint_type, '')
for endpoint in service['endpoints']:
if endpoint['interface'] == interface:
return endpoint['url']
except (IndexError, KeyError):
raise exceptions.ServiceCatalogException(service_type)
else:
raise exceptions.ServiceCatalogException(service_type)
url = get_url_for_service(service, endpoint_type)
if not url and fallback_endpoint_type:
url = get_url_for_service(service, fallback_endpoint_type)
if url:
return url
raise exceptions.ServiceCatalogException(service_type)
def is_service_enabled(request, service_type, service_name=None):

View File

@ -163,9 +163,16 @@ OPENSTACK_QUANTUM_NETWORK = {
# OPENSTACK_ENDPOINT_TYPE specifies the endpoint type to use for the endpoints
# in the Keystone service catalog. Use this setting when Horizon is running
# external to the OpenStack environment. The default is 'internalURL'.
# external to the OpenStack environment. The default is 'publicURL'.
#OPENSTACK_ENDPOINT_TYPE = "publicURL"
# SECONDARY_ENDPOINT_TYPE specifies the fallback endpoint type to use in the
# case that OPENSTACK_ENDPOINT_TYPE is not present in the endpoints
# in the Keystone service catalog. Use this setting when Horizon is running
# external to the OpenStack environment. The default is None. This
# value should differ from OPENSTACK_ENDPOINT_TYPE if used.
#SECONDARY_ENDPOINT_TYPE = "publicURL"
# The number of objects (Swift containers/objects or images) to display
# on a single page before providing a paging element (a "more" link)
# to paginate results.

View File

@ -115,19 +115,14 @@ class ApiHelperTests(test.TestCase):
url = api_base.url_for(self.request, 'image')
self.assertEqual(url, 'http://public.glance.example.com:9292/v1')
url = api_base.url_for(self.request, 'image', admin=False)
self.assertEqual(url, 'http://public.glance.example.com:9292/v1')
url = api_base.url_for(self.request, 'image', admin=True)
url = api_base.url_for(self.request, 'image', endpoint_type='adminURL')
self.assertEqual(url, 'http://admin.glance.example.com:9292/v1')
url = api_base.url_for(self.request, 'compute')
self.assertEqual(url, 'http://public.nova.example.com:8774/v2')
url = api_base.url_for(self.request, 'compute', admin=False)
self.assertEqual(url, 'http://public.nova.example.com:8774/v2')
url = api_base.url_for(self.request, 'compute', admin=True)
url = api_base.url_for(self.request, 'compute',
endpoint_type='adminURL')
self.assertEqual(url, 'http://admin.nova.example.com:8774/v2')
url = api_base.url_for(self.request, 'volume')
@ -137,10 +132,8 @@ class ApiHelperTests(test.TestCase):
endpoint_type="internalURL")
self.assertEqual(url, 'http://int.nova.example.com:8776/v1')
url = api_base.url_for(self.request, 'volume', admin=False)
self.assertEqual(url, 'http://public.nova.example.com:8776/v1')
url = api_base.url_for(self.request, 'volume', admin=True)
url = api_base.url_for(self.request, 'volume',
endpoint_type='adminURL')
self.assertEqual(url, 'http://admin.nova.example.com:8776/v1')
self.assertNotIn('notAnApi', self.request.user.service_catalog,