Always return the service name in the catalog

Fix implemention of service catalog creation in v2, to handle the case
when the service name is not available. Using service.get('name', '')
does not work because that doesn't invoke the __getitem__() from
sql.DictBase parent class of Service. Instead the service name have to
be directly read from the 'extra' attribute of the service object using
service.extra.get('name', '').
The name attribute of the service will now always be included in the
service catalog. The value will be defaulted to empty string if no
service name is available.

Change-Id: I305abd8446fee57af18aebf7873e3c67a1b83c92
Closes-Bug: #1393518
This commit is contained in:
lin-hua-cheng 2014-11-19 17:32:56 -08:00
parent 5d376ba9ad
commit ca3df19da4
3 changed files with 65 additions and 4 deletions

View File

@ -293,7 +293,7 @@ class Catalog(catalog.Driver):
service_type = endpoint.service['type']
default_service = {
'id': endpoint['id'],
'name': endpoint.service['name'],
'name': endpoint.service.extra.get('name', ''),
'publicURL': ''
}
catalog.setdefault(region, {})
@ -329,9 +329,7 @@ class Catalog(catalog.Driver):
def make_v3_service(svc):
eps = list(make_v3_endpoints(svc.endpoints))
service = {'endpoints': eps, 'id': svc.id, 'type': svc.type}
name = svc.extra.get('name')
if name:
service['name'] = name
service['name'] = svc.extra.get('name', '')
return service
return [make_v3_service(svc) for svc in services]

View File

@ -179,3 +179,33 @@ class TestV2CatalogAPISQL(tests.TestCase):
self.assertEqual(1, len(catalog))
# all three endpoints appear in the backend
self.assertEqual(3, len(self.catalog_api.list_endpoints()))
def test_get_catalog_always_returns_service_name(self):
user_id = uuid.uuid4().hex
tenant_id = uuid.uuid4().hex
# create a service, with a name
named_svc = {
'id': uuid.uuid4().hex,
'type': uuid.uuid4().hex,
'name': uuid.uuid4().hex,
}
self.catalog_api.create_service(named_svc['id'], named_svc)
endpoint = self.new_endpoint_ref(service_id=named_svc['id'])
self.catalog_api.create_endpoint(endpoint['id'], endpoint)
# create a service, with no name
unnamed_svc = {
'id': uuid.uuid4().hex,
'type': uuid.uuid4().hex
}
self.catalog_api.create_service(unnamed_svc['id'], unnamed_svc)
endpoint = self.new_endpoint_ref(service_id=unnamed_svc['id'])
self.catalog_api.create_endpoint(endpoint['id'], endpoint)
region = None
catalog = self.catalog_api.get_catalog(user_id, tenant_id)
self.assertEqual(named_svc['name'],
catalog[region][named_svc['type']]['name'])
self.assertEqual('', catalog[region][unnamed_svc['type']]['name'])

View File

@ -675,6 +675,39 @@ class TestCatalogAPISQL(tests.TestCase):
# all three appear in the backend
self.assertEqual(3, len(self.catalog_api.list_endpoints()))
def test_get_catalog_always_returns_service_name(self):
user_id = uuid.uuid4().hex
tenant_id = uuid.uuid4().hex
# create a service, with a name
named_svc = {
'id': uuid.uuid4().hex,
'type': uuid.uuid4().hex,
'name': uuid.uuid4().hex,
}
self.catalog_api.create_service(named_svc['id'], named_svc)
endpoint = self.new_endpoint_ref(service_id=named_svc['id'])
self.catalog_api.create_endpoint(endpoint['id'], endpoint)
# create a service, with no name
unnamed_svc = {
'id': uuid.uuid4().hex,
'type': uuid.uuid4().hex
}
self.catalog_api.create_service(unnamed_svc['id'], unnamed_svc)
endpoint = self.new_endpoint_ref(service_id=unnamed_svc['id'])
self.catalog_api.create_endpoint(endpoint['id'], endpoint)
catalog = self.catalog_api.get_v3_catalog(user_id, tenant_id)
named_endpoint = [ep for ep in catalog
if ep['type'] == named_svc['type']][0]
self.assertEqual(named_svc['name'], named_endpoint['name'])
unnamed_endpoint = [ep for ep in catalog
if ep['type'] == unnamed_svc['type']][0]
self.assertEqual('', unnamed_endpoint['name'])
# TODO(dstanek): this needs refactoring with the test above, but we are in a
# crunch so that will happen in a future patch.