From b59a840e177dff0d38173ee81e8c97028a0f7825 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Tue, 18 Nov 2014 21:12:01 -0600 Subject: [PATCH] Support regionless/global services In order to not fail while reading service catalogs which contain services which are configured to be global, meaning without a configured region, parse and normalize the catalog without mentioning a region for that service. Change-Id: Idf84dc0f44fecb41845bb8d57617d10cc6e0d77d --- openstack/auth/service_catalog.py | 32 ++++++++--------- openstack/tests/auth/common.py | 37 ++++++++++++++++++++ openstack/tests/auth/test_service_catalog.py | 5 +-- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/openstack/auth/service_catalog.py b/openstack/auth/service_catalog.py index 748f5e59..8a4c4fbc 100644 --- a/openstack/auth/service_catalog.py +++ b/openstack/auth/service_catalog.py @@ -74,7 +74,7 @@ class ServiceCatalog(object): if not filtration.match_service_name(service.get('name')): continue for endpoint in service.get('endpoints', []): - if not filtration.match_region(endpoint.get('region')): + if not filtration.match_region(endpoint.get('region', None)): continue if not filtration.match_visibility(endpoint.get('interface')): continue @@ -137,6 +137,18 @@ class ServiceCatalog(object): class ServiceCatalogV2(ServiceCatalog): """The V2 service catalog from Keystone.""" + + def _extract_details(self, endpoint, interface): + value = { + 'interface': interface, + 'url': endpoint['%sURL' % interface] + } + region = endpoint.get('region', None) + if region: + value['region'] = region + + return value + def _normalize(self): """Handle differences in the way v2 and v3 catalogs specify endpoints. @@ -146,21 +158,9 @@ class ServiceCatalogV2(ServiceCatalog): eps = [] for endpoint in service['endpoints']: if 'publicURL' in endpoint: - eps += [{ - 'interface': 'public', - 'region': endpoint['region'], - 'url': endpoint['publicURL'], - }] + eps += [self._extract_details(endpoint, "public")] if 'internalURL' in endpoint: - eps += [{ - 'interface': 'internal', - 'region': endpoint['region'], - 'url': endpoint['internalURL'], - }] + eps += [self._extract_details(endpoint, "internal")] if 'adminURL' in endpoint: - eps += [{ - 'interface': 'admin', - 'region': endpoint['region'], - 'url': endpoint['adminURL'], - }] + eps += [self._extract_details(endpoint, "admin")] service['endpoints'] = eps diff --git a/openstack/tests/auth/common.py b/openstack/tests/auth/common.py index 676d0e7e..458ec79c 100644 --- a/openstack/tests/auth/common.py +++ b/openstack/tests/auth/common.py @@ -27,6 +27,14 @@ TEST_USER_ID = 'youid' TEST_SERVICE_CATALOG_V2 = [ { + "endpoints": [{ + "adminURL": "http://compute.region0.admin/v1.1/", + "internalURL": "http://compute.region0.internal/v1.1/", + "publicURL": "http://compute.region0.public/v1.1/", + }], + "type": "compute", + "name": "nova0" + }, { "endpoints": [{ "adminURL": "http://compute.region2.admin/v1/", "region": "RegionTwo", @@ -74,6 +82,22 @@ TEST_SERVICE_CATALOG_V2 = [ }] TEST_SERVICE_CATALOG_NORMALIZED = [ { + "endpoints": [{ + "interface": "public", + "url": "http://compute.region0.public/%(version)s", + 'version': 'v1.1', + }, { + "interface": "internal", + "url": "http://compute.region0.internal/%(version)s", + 'version': 'v1.1', + }, { + "interface": "admin", + "url": "http://compute.region0.admin/%(version)s", + 'version': 'v1.1', + }], + "type": "compute", + "name": "nova0" + }, { "endpoints": [{ "interface": "public", "region": "RegionTwo", @@ -185,6 +209,19 @@ TEST_RESPONSE_DICT_V2 = { TEST_SERVICE_CATALOG_V3 = [ { + "endpoints": [{ + "url": "http://compute.region0.public/v1.1/", + "interface": "public" + }, { + "url": "http://compute.region0.internal/v1.1/", + "interface": "internal" + }, { + "url": "http://compute.region0.admin/v1.1/", + "interface": "admin" + }], + "type": "compute", + "name": "nova0", + }, { "endpoints": [{ "url": "http://compute.region2.public/v1/", "region": "RegionTwo", diff --git a/openstack/tests/auth/test_service_catalog.py b/openstack/tests/auth/test_service_catalog.py index 5b98709f..3a7e7452 100644 --- a/openstack/tests/auth/test_service_catalog.py +++ b/openstack/tests/auth/test_service_catalog.py @@ -26,7 +26,8 @@ from openstack.tests.auth import common class TestServiceCatalog(testtools.TestCase): def get_urls(self, sot): sf = service_filter.ServiceFilter(service_type='compute') - exp = ["http://compute.region2.public/v1", + exp = ["http://compute.region0.public/v1.1", + "http://compute.region2.public/v1", "http://compute.region1.public/v2.0"] self.assertEqual(exp, sot.get_urls(sf)) sf = service_filter.ServiceFilter(service_type='image') @@ -130,7 +131,7 @@ class TestServiceCatalogV3(TestServiceCatalog): def test_get_versions(self): sot = catalog.ServiceCatalog(common.TEST_SERVICE_CATALOG_V3) service = compute_service.ComputeService() - self.assertEqual(['v1', 'v2.0'], sot.get_versions(service)) + self.assertEqual(['v1.1', 'v1', 'v2.0'], sot.get_versions(service)) service = identity_service.IdentityService() self.assertEqual(['v1.1'], sot.get_versions(service)) service = image_service.ImageService()