diff --git a/keystoneclient/service_catalog.py b/keystoneclient/service_catalog.py index 50587c270..5b179d969 100644 --- a/keystoneclient/service_catalog.py +++ b/keystoneclient/service_catalog.py @@ -23,8 +23,9 @@ from keystoneclient import exceptions class ServiceCatalog(object): """Helper methods for dealing with a Keystone Service Catalog.""" - def __init__(self, resource_dict): + def __init__(self, resource_dict, region_name=None): self.catalog = resource_dict + self.region_name = region_name def get_token(self): """Fetch token details from service catalog. @@ -70,6 +71,9 @@ class ServiceCatalog(object): endpoints = service['endpoints'] for endpoint in endpoints: + if self.region_name and \ + endpoint.get('region') != self.region_name: + continue if not filter_value or endpoint.get(attr) == filter_value: return endpoint[endpoint_type] diff --git a/keystoneclient/v2_0/client.py b/keystoneclient/v2_0/client.py index e98f7a2c4..c378e3018 100644 --- a/keystoneclient/v2_0/client.py +++ b/keystoneclient/v2_0/client.py @@ -208,7 +208,8 @@ class Client(client.HTTPClient): # associated methods def _extract_service_catalog(self, url, body): """ Set the client's service catalog from the response data. """ - self.service_catalog = service_catalog.ServiceCatalog(body) + self.service_catalog = service_catalog.ServiceCatalog( + body, region_name=self.region_name) try: sc = self.service_catalog.get_token() # Save these since we have them and they'll be useful later diff --git a/tests/test_service_catalog.py b/tests/test_service_catalog.py index 9397405f2..0f5620a47 100644 --- a/tests/test_service_catalog.py +++ b/tests/test_service_catalog.py @@ -74,7 +74,20 @@ SERVICE_CATALOG = { "href":"https://identity.north.host/v2.0/" "endpoints?marker=2" }] - } + }, + { + "name": "Image Servers", + "type": "image", + "endpoints": [ + {"publicURL": "https://image.north.host/v1/", + "internalURL": "https://image-internal.north.host/v1/", + "region": "North"}, + {"publicURL": "https://image.south.host/v1/", + "internalURL": "https://image-internal.south.host/v1/", + "region": "South"} + ], + "endpoints_links": [] + }, ], "serviceCatalog_links": [{ "rel": "next", @@ -108,6 +121,16 @@ class ServiceCatalogTest(utils.TestCase): self.assertEquals(public_ep['compute'][1]['internalURL'], "https://compute.north.host/v1.1/3456") + def test_service_catalog_regions(self): + sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'], + region_name="North") + url = sc.url_for(service_type='image', endpoint_type='publicURL') + self.assertEquals(url, "https://image.north.host/v1/") + sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'], + region_name="South") + url = sc.url_for(service_type='image', endpoint_type='internalURL') + self.assertEquals(url, "https://image-internal.south.host/v1/") + def test_token(self): sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'])