From 6d2693a13e9b33da6b13e13f587f99a20b2fe2d0 Mon Sep 17 00:00:00 2001 From: Fei Long Wang Date: Sun, 3 Mar 2013 16:48:22 +0800 Subject: [PATCH] Fixes issues found in /os-hosts API There are some issues found in Cinder /os-hosts API: 1) Bracket in the wrong spot in Cinder HostController show method There is an erroneous bracket on the following line in the show method that should be at the end of the response: 'total_volume_gb': str(sum)}, 2) XML Attributes Missing for List/Index method in Cinder HostController The HostIndexTemplate class specifies mapping for "host" and "topic" attributes rather than any of the ones that are actually returned from the index method. 3) XML Serialization is commented for Cinder HostController show method Fixes Bug: 1139984 Change-Id: I881e80b5e109ed6ddcc4f7c4bf5749ab6dca563d --- cinder/api/contrib/hosts.py | 20 ++++++++++---------- cinder/tests/api/contrib/test_hosts.py | 22 +++++++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cinder/api/contrib/hosts.py b/cinder/api/contrib/hosts.py index 2c68c71c9..0fc85481d 100644 --- a/cinder/api/contrib/hosts.py +++ b/cinder/api/contrib/hosts.py @@ -36,14 +36,14 @@ authorize = extensions.extension_authorizer('volume', 'hosts') class HostIndexTemplate(xmlutil.TemplateBuilder): def construct(self): - def shimmer(obj, do_raise=False): - # A bare list is passed in; we need to wrap it in a dict - return dict(hosts=obj) - - root = xmlutil.TemplateElement('hosts', selector=shimmer) + root = xmlutil.TemplateElement('hosts') elem = xmlutil.SubTemplateElement(root, 'host', selector='hosts') - elem.set('host') - elem.set('topic') + elem.set('service-status') + elem.set('service') + elem.set('zone') + elem.set('service-state') + elem.set('host_name') + elem.set('last-update') return xmlutil.MasterTemplate(root, 1) @@ -183,7 +183,7 @@ class HostController(object): raise webob.exc.HTTPBadRequest(explanation=result) return {"host": host, "status": result} - #@wsgi.serializers(xml=HostShowTemplate) + @wsgi.serializers(xml=HostShowTemplate) def show(self, req, id): """Shows the volume usage info given by hosts. @@ -219,9 +219,9 @@ class HostController(object): snap_sum_total = 0 resources = [{'resource': {'host': host, 'project': '(total)', 'volume_count': str(count), - 'total_volume_gb': str(sum)}, + 'total_volume_gb': str(sum), 'snapshot_count': str(snap_count_total), - 'total_snapshot_gb': str(snap_sum_total)}] + 'total_snapshot_gb': str(snap_sum_total)}}] project_ids = [v['project_id'] for v in volume_refs] project_ids = list(set(project_ids)) diff --git a/cinder/tests/api/contrib/test_hosts.py b/cinder/tests/api/contrib/test_hosts.py index ff8f9bed0..525f96305 100644 --- a/cinder/tests/api/contrib/test_hosts.py +++ b/cinder/tests/api/contrib/test_hosts.py @@ -161,18 +161,26 @@ class HostSerializerTest(test.TestCase): def test_index_serializer(self): serializer = os_hosts.HostIndexTemplate() - text = serializer.serialize(SERVICE_LIST) + text = serializer.serialize({"hosts": LIST_RESPONSE}) tree = etree.fromstring(text) self.assertEqual('hosts', tree.tag) - self.assertEqual(len(SERVICE_LIST), len(tree)) - for i in range(len(SERVICE_LIST)): + self.assertEqual(len(LIST_RESPONSE), len(tree)) + for i in range(len(LIST_RESPONSE)): self.assertEqual('host', tree[i].tag) - self.assertEqual(SERVICE_LIST[i]['host'], - tree[i].get('host')) - self.assertEqual(SERVICE_LIST[i]['topic'], - tree[i].get('topic')) + self.assertEqual(LIST_RESPONSE[i]['service-status'], + tree[i].get('service-status')) + self.assertEqual(LIST_RESPONSE[i]['service'], + tree[i].get('service')) + self.assertEqual(LIST_RESPONSE[i]['zone'], + tree[i].get('zone')) + self.assertEqual(LIST_RESPONSE[i]['service-state'], + tree[i].get('service-state')) + self.assertEqual(LIST_RESPONSE[i]['host_name'], + tree[i].get('host_name')) + self.assertEqual(str(LIST_RESPONSE[i]['last-update']), + tree[i].get('last-update')) def test_update_serializer_with_status(self): exemplar = dict(host='test.host.1', status='enabled') -- 2.45.2