diff --git a/openstack_dashboard/api/base.py b/openstack_dashboard/api/base.py index ca40c9261..3b2fae684 100644 --- a/openstack_dashboard/api/base.py +++ b/openstack_dashboard/api/base.py @@ -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): diff --git a/openstack_dashboard/local/local_settings.py.example b/openstack_dashboard/local/local_settings.py.example index 083ed339e..a5537d120 100644 --- a/openstack_dashboard/local/local_settings.py.example +++ b/openstack_dashboard/local/local_settings.py.example @@ -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. diff --git a/openstack_dashboard/test/api_tests/base_tests.py b/openstack_dashboard/test/api_tests/base_tests.py index 6ff5f138d..9a0c140f5 100644 --- a/openstack_dashboard/test/api_tests/base_tests.py +++ b/openstack_dashboard/test/api_tests/base_tests.py @@ -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,