Skip to content

Commit

Permalink
Fix HostDeserializer to enable multiple line xml
Browse files Browse the repository at this point in the history
Fixes bug: 1082132

The current HostDeserializer wasn't able to deal with multiple line xml inputs.
Since the HostDeserializer is used only on the update operation this patch
renames it to HostUpdateDeserializer, and parses only the valid options (status
and maintenance_mode).

Change-Id: I9ec3b296a07e0fc903da1c005cf7bd5dcb08fd1b
  • Loading branch information
maurorodrigues committed Nov 22, 2012
1 parent e6ba584 commit f58f2b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 12 additions & 4 deletions nova/api/openstack/compute/contrib/hosts.py
Expand Up @@ -70,7 +70,7 @@ def construct(self):
return xmlutil.MasterTemplate(root, 1)


class HostDeserializer(wsgi.XMLDeserializer):
class HostUpdateDeserializer(wsgi.XMLDeserializer):
def default(self, string):
try:
node = minidom.parseString(string)
Expand All @@ -79,8 +79,16 @@ def default(self, string):
raise exception.MalformedRequestBody(reason=msg)

updates = {}
for child in node.childNodes[0].childNodes:
updates[child.tagName] = self.extract_text(child)
updates_node = self.find_first_child_named(node, 'updates')
if updates_node is not None:
maintenance = self.find_first_child_named(updates_node,
'maintenance_mode')
if maintenance is not None:
updates[maintenance.tagName] = self.extract_text(maintenance)

status = self.find_first_child_named(updates_node, 'status')
if status is not None:
updates[status.tagName] = self.extract_text(status)

return dict(body=updates)

Expand Down Expand Up @@ -131,7 +139,7 @@ def index(self, req):
return {'hosts': _list_hosts(req)}

@wsgi.serializers(xml=HostUpdateTemplate)
@wsgi.deserializers(xml=HostDeserializer)
@wsgi.deserializers(xml=HostUpdateDeserializer)
@check_host
def update(self, req, id, body):
authorize(req.environ['nova.context'])
Expand Down
11 changes: 7 additions & 4 deletions nova/tests/api/openstack/compute/contrib/test_hosts.py
Expand Up @@ -272,7 +272,7 @@ def test_show_works_correctly(self):
class HostSerializerTest(test.TestCase):
def setUp(self):
super(HostSerializerTest, self).setUp()
self.deserializer = os_hosts.HostDeserializer()
self.deserializer = os_hosts.HostUpdateDeserializer()

def test_index_serializer(self):
serializer = os_hosts.HostIndexTemplate()
Expand Down Expand Up @@ -336,9 +336,12 @@ def test_action_serializer(self):
self.assertEqual(value, tree.get(key))

def test_update_deserializer(self):
exemplar = dict(status='enabled', foo='bar')
intext = ("<?xml version='1.0' encoding='UTF-8'?>\n"
'<updates><status>enabled</status><foo>bar</foo></updates>')
exemplar = dict(status='enabled', maintenance_mode='disable')
intext = """<?xml version='1.0' encoding='UTF-8'?>
<updates>
<status>enabled</status>
<maintenance_mode>disable</maintenance_mode>
</updates>"""
result = self.deserializer.deserialize(intext)

self.assertEqual(dict(body=exemplar), result)

0 comments on commit f58f2b4

Please sign in to comment.