Merge "Refactoring url_for to remove admin parameter"
This commit is contained in:
commit
ead648382a
@ -198,27 +198,35 @@ ENDPOINT_TYPE_TO_INTERFACE = {
|
||||
}
|
||||
|
||||
|
||||
def url_for(request, service_type, admin=False, endpoint_type=None):
|
||||
endpoint_type = endpoint_type or getattr(settings,
|
||||
'OPENSTACK_ENDPOINT_TYPE',
|
||||
'publicURL')
|
||||
catalog = request.user.service_catalog
|
||||
service = get_service_from_catalog(catalog, service_type)
|
||||
def get_url_for_service(service, endpoint_type):
|
||||
identity_version = get_version_from_service(service)
|
||||
if admin:
|
||||
endpoint_type = 'adminURL'
|
||||
if service:
|
||||
for endpoint in service['endpoints']:
|
||||
try:
|
||||
if identity_version < 3:
|
||||
return service['endpoints'][0][endpoint_type]
|
||||
return endpoint[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:
|
||||
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)
|
||||
if service:
|
||||
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)
|
||||
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user