diff --git a/glance/api/v2/image_access.py b/glance/api/v2/image_access.py index 23438cb75b..726fbc7d04 100644 --- a/glance/api/v2/image_access.py +++ b/glance/api/v2/image_access.py @@ -37,7 +37,10 @@ class Controller(object): #TODO(bcwaldon): We have to filter on non-deleted members # manually. This should be done for us in the db api - return filter(lambda m: not m['deleted'], members) + return { + 'access_records': filter(lambda m: not m['deleted'], members), + 'image_id': image_id, + } def show(self, req, image_id, tenant_id): members = self.db_api.image_member_find(req.context, @@ -110,31 +113,28 @@ class ResponseSerializer(wsgi.JSONResponseSerializer): link = '%s/%s' % (link, tenant_id) return link - def _get_access_links(self, access): - self_link = self._get_access_href(access['image_id'], access['member']) - return [ - {'rel': 'self', 'href': self_link}, - {'rel': 'describedby', 'href': '/v2/schemas/image/access'}, - ] - def _format_access(self, access): + self_link = self._get_access_href(access['image_id'], access['member']) return { - 'tenant_id': access['member'], - 'can_share': access['can_share'], - 'links': self._get_access_links(access), - } - - def _get_container_links(self, image_id): - return [{'rel': 'self', 'href': self._get_access_href(image_id)}] + 'tenant_id': access['member'], + 'can_share': access['can_share'], + 'self': self_link, + 'schema': '/v2/schemas/image/access', + 'image': '/v2/images/%s' % access['image_id'], + } def show(self, response, access): record = {'access_record': self._format_access(access)} response.body = json.dumps(record) - def index(self, response, access_records): + def index(self, response, result): + access_records = result['access_records'] + first_link = '/v2/images/%s/access' % result['image_id'] body = { - 'access_records': [self._format_access(a) for a in access_records], - 'links': [], + 'access_records': [self._format_access(a) + for a in access_records], + 'first': first_link, + 'schema': '/v2/schemas/image/accesses', } response.body = json.dumps(body) diff --git a/glance/tests/unit/v2/test_image_access_resource.py b/glance/tests/unit/v2/test_image_access_resource.py index fde92c950a..9700acec10 100644 --- a/glance/tests/unit/v2/test_image_access_resource.py +++ b/glance/tests/unit/v2/test_image_access_resource.py @@ -35,26 +35,32 @@ class TestImageAccessController(test_utils.BaseTestCase): def test_index(self): req = unit_test_utils.get_fake_request() output = self.controller.index(req, unit_test_utils.UUID1) - expected = [ - { - 'image_id': unit_test_utils.UUID1, - 'member': unit_test_utils.TENANT1, - 'can_share': True, - 'deleted': False, - }, - { - 'image_id': unit_test_utils.UUID1, - 'member': unit_test_utils.TENANT2, - 'can_share': False, - 'deleted': False, - }, - ] + expected = { + 'access_records': [ + { + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT1, + 'can_share': True, + 'deleted': False, + }, + { + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT2, + 'can_share': False, + 'deleted': False, + }, + ], + 'image_id': unit_test_utils.UUID1, + } self.assertEqual(expected, output) def test_index_zero_records(self): req = unit_test_utils.get_fake_request() output = self.controller.index(req, unit_test_utils.UUID2) - expected = [] + expected = { + 'access_records': [], + 'image_id': unit_test_utils.UUID2, + } self.assertEqual(expected, output) def test_index_nonexistant_image(self): @@ -145,10 +151,9 @@ class TestImageAccessSerializer(test_utils.BaseTestCase): 'access_record': { 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, - 'links': [ - {'rel': 'self', 'href': self_href}, - {'rel': 'describedby', 'href': '/v2/schemas/image/access'}, - ], + 'self': self_href, + 'schema': '/v2/schemas/image/access', + 'image': '/v2/images/%s' % unit_test_utils.UUID1, }, } response = webob.Response() @@ -168,45 +173,52 @@ class TestImageAccessSerializer(test_utils.BaseTestCase): 'can_share': True, }, ] + result = { + 'access_records': fixtures, + 'image_id': unit_test_utils.UUID1, + } expected = { 'access_records': [ { 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, - 'links': [ - { - 'rel': 'self', - 'href': ('/v2/images/%s/access/%s' % + 'self': ('/v2/images/%s/access/%s' % (unit_test_utils.UUID1, - unit_test_utils.TENANT1)) - }, - { - 'rel': 'describedby', - 'href': '/v2/schemas/image/access', - }, - ], + unit_test_utils.TENANT1)), + 'schema': '/v2/schemas/image/access', + 'image': '/v2/images/%s' % unit_test_utils.UUID1, }, { 'tenant_id': unit_test_utils.TENANT2, 'can_share': True, - 'links': [ - { - 'rel': 'self', - 'href': ('/v2/images/%s/access/%s' % + 'self': ('/v2/images/%s/access/%s' % (unit_test_utils.UUID1, - unit_test_utils.TENANT2)) - }, - { - 'rel': 'describedby', - 'href': '/v2/schemas/image/access', - }, - ], + unit_test_utils.TENANT2)), + 'schema': '/v2/schemas/image/access', + 'image': '/v2/images/%s' % unit_test_utils.UUID1, }, ], - 'links': [], + 'first': '/v2/images/%s/access' % unit_test_utils.UUID1, + 'schema': '/v2/schemas/image/accesses', + } response = webob.Response() - self.serializer.index(response, fixtures) + self.serializer.index(response, result) + self.assertEqual(expected, json.loads(response.body)) + + def test_index_zero_access_records(self): + result = { + 'access_records': [], + 'image_id': unit_test_utils.UUID1, + } + response = webob.Response() + self.serializer.index(response, result) + first_link = '/v2/images/%s/access' % unit_test_utils.UUID1 + expected = { + 'access_records': [], + 'first': first_link, + 'schema': '/v2/schemas/image/accesses', + } self.assertEqual(expected, json.loads(response.body)) def test_create(self): @@ -221,10 +233,9 @@ class TestImageAccessSerializer(test_utils.BaseTestCase): 'access': { 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, - 'links': [ - {'rel': 'self', 'href': self_href}, - {'rel': 'describedby', 'href': '/v2/schemas/image/access'}, - ], + 'self': self_href, + 'schema': '/v2/schemas/image/access', + 'image': '/v2/images/%s' % unit_test_utils.UUID1, }, } response = webob.Response()