Take region_name into account when calling url_for
Fixes bug 1103282 When creating a Client object with region_name specified, url_for will return an endpoint url for that region. Change-Id: Ia5c95503fe2ed5e53de93040d70ba08a9c09f286
This commit is contained in:
@@ -23,8 +23,9 @@ from keystoneclient import exceptions
|
|||||||
class ServiceCatalog(object):
|
class ServiceCatalog(object):
|
||||||
"""Helper methods for dealing with a Keystone Service Catalog."""
|
"""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.catalog = resource_dict
|
||||||
|
self.region_name = region_name
|
||||||
|
|
||||||
def get_token(self):
|
def get_token(self):
|
||||||
"""Fetch token details from service catalog.
|
"""Fetch token details from service catalog.
|
||||||
@@ -70,6 +71,9 @@ class ServiceCatalog(object):
|
|||||||
|
|
||||||
endpoints = service['endpoints']
|
endpoints = service['endpoints']
|
||||||
for endpoint in 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:
|
if not filter_value or endpoint.get(attr) == filter_value:
|
||||||
return endpoint[endpoint_type]
|
return endpoint[endpoint_type]
|
||||||
|
|
||||||
|
@@ -208,7 +208,8 @@ class Client(client.HTTPClient):
|
|||||||
# associated methods
|
# associated methods
|
||||||
def _extract_service_catalog(self, url, body):
|
def _extract_service_catalog(self, url, body):
|
||||||
""" Set the client's service catalog from the response data. """
|
""" 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:
|
try:
|
||||||
sc = self.service_catalog.get_token()
|
sc = self.service_catalog.get_token()
|
||||||
# Save these since we have them and they'll be useful later
|
# Save these since we have them and they'll be useful later
|
||||||
|
@@ -74,7 +74,20 @@ SERVICE_CATALOG = {
|
|||||||
"href":"https://identity.north.host/v2.0/"
|
"href":"https://identity.north.host/v2.0/"
|
||||||
"endpoints?marker=2"
|
"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": [{
|
"serviceCatalog_links": [{
|
||||||
"rel": "next",
|
"rel": "next",
|
||||||
@@ -108,6 +121,16 @@ class ServiceCatalogTest(utils.TestCase):
|
|||||||
self.assertEquals(public_ep['compute'][1]['internalURL'],
|
self.assertEquals(public_ep['compute'][1]['internalURL'],
|
||||||
"https://compute.north.host/v1.1/3456")
|
"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):
|
def test_token(self):
|
||||||
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'])
|
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'])
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user