From eb86e97cebbdbff17e591e5674c334501ced1279 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 30 Aug 2012 11:47:56 -0400 Subject: Fix xml metadata for volumes extension. Fixes bug 1040891 Change-Id: I3a5d46af18f764e86ab457071d2b3afafdcdaa24 Signed-off-by: Matthew Treinish --- nova/api/openstack/compute/contrib/volumes.py | 53 ++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/volumes.py b/nova/api/openstack/compute/contrib/volumes.py index 99d713cef..df3c14169 100644 --- a/nova/api/openstack/compute/contrib/volumes.py +++ b/nova/api/openstack/compute/contrib/volumes.py @@ -17,6 +17,7 @@ import webob from webob import exc +from xml.dom import minidom from nova.api.openstack import common from nova.api.openstack import extensions @@ -74,10 +75,8 @@ def _translate_volume_summary_view(context, vol): LOG.audit(_("vol=%s"), vol, context=context) if vol.get('volume_metadata'): - meta_dict = {} - for i in vol['volume_metadata']: - meta_dict[i['key']] = i['value'] - d['metadata'] = meta_dict + metadata = vol.get('volume_metadata') + d['metadata'] = dict((item['key'], item['value']) for item in metadata) else: d['metadata'] = {} @@ -100,8 +99,8 @@ def make_volume(elem): selector='attachments') make_attachment(attachment) - metadata = xmlutil.make_flat_dict('metadata') - elem.append(metadata) + # Attach metadata node + elem.append(common.MetadataTemplate()) class VolumeTemplate(xmlutil.TemplateBuilder): @@ -119,6 +118,47 @@ class VolumesTemplate(xmlutil.TemplateBuilder): return xmlutil.MasterTemplate(root, 1) +class CommonDeserializer(wsgi.MetadataXMLDeserializer): + """Common deserializer to handle xml-formatted volume requests. + + Handles standard volume attributes as well as the optional metadata + attribute + """ + + metadata_deserializer = common.MetadataXMLDeserializer() + + def _extract_volume(self, node): + """Marshal the volume attribute of a parsed request.""" + volume = {} + volume_node = self.find_first_child_named(node, 'volume') + + attributes = ['display_name', 'display_description', 'size', + 'volume_type', 'availability_zone'] + for attr in attributes: + if volume_node.getAttribute(attr): + volume[attr] = volume_node.getAttribute(attr) + + metadata_node = self.find_first_child_named(volume_node, 'metadata') + if metadata_node is not None: + volume['metadata'] = self.extract_metadata(metadata_node) + + return volume + + +class CreateDeserializer(CommonDeserializer): + """Deserializer to handle xml-formatted create volume requests. + + Handles standard volume attributes as well as the optional metadata + attribute + """ + + def default(self, string): + """Deserialize an xml-formatted volume create request.""" + dom = minidom.parseString(string) + volume = self._extract_volume(dom) + return {'body': {'volume': volume}} + + class VolumeController(object): """The Volumes API controller for the OpenStack API.""" @@ -174,6 +214,7 @@ class VolumeController(object): return {'volumes': res} @wsgi.serializers(xml=VolumeTemplate) + @wsgi.deserializers(xml=CreateDeserializer) def create(self, req, body): """Creates a new volume.""" context = req.environ['nova.context'] -- cgit