From b79ad1a950116b6eb0e0f7be48e9ca215681ee1e Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Tue, 17 Jul 2012 04:06:19 +0100 Subject: [PATCH] Remove unused metadata serialization Only used by the compute API in Nova for e.g. images and server metadata. Change-Id: I223aca1a1c8e35c9339107e5c11bdd8a39968681 --- cinder/api/openstack/common.py | 72 -------- cinder/api/openstack/wsgi.py | 12 -- cinder/tests/api/openstack/test_common.py | 197 ---------------------- 3 files changed, 281 deletions(-) diff --git a/cinder/api/openstack/common.py b/cinder/api/openstack/common.py index 0fb79d4b9..c3e09133e 100644 --- a/cinder/api/openstack/common.py +++ b/cinder/api/openstack/common.py @@ -211,78 +211,6 @@ def dict_to_query_str(params): return param_str.rstrip('&') -class MetadataDeserializer(wsgi.MetadataXMLDeserializer): - def deserialize(self, text): - dom = minidom.parseString(text) - metadata_node = self.find_first_child_named(dom, "metadata") - metadata = self.extract_metadata(metadata_node) - return {'body': {'metadata': metadata}} - - -class MetaItemDeserializer(wsgi.MetadataXMLDeserializer): - def deserialize(self, text): - dom = minidom.parseString(text) - metadata_item = self.extract_metadata(dom) - return {'body': {'meta': metadata_item}} - - -class MetadataXMLDeserializer(wsgi.XMLDeserializer): - - def extract_metadata(self, metadata_node): - """Marshal the metadata attribute of a parsed request""" - if metadata_node is None: - return {} - metadata = {} - for meta_node in self.find_children_named(metadata_node, "meta"): - key = meta_node.getAttribute("key") - metadata[key] = self.extract_text(meta_node) - return metadata - - def _extract_metadata_container(self, datastring): - dom = minidom.parseString(datastring) - metadata_node = self.find_first_child_named(dom, "metadata") - metadata = self.extract_metadata(metadata_node) - return {'body': {'metadata': metadata}} - - def create(self, datastring): - return self._extract_metadata_container(datastring) - - def update_all(self, datastring): - return self._extract_metadata_container(datastring) - - def update(self, datastring): - dom = minidom.parseString(datastring) - metadata_item = self.extract_metadata(dom) - return {'body': {'meta': metadata_item}} - - -metadata_nsmap = {None: xmlutil.XMLNS_V11} - - -class MetaItemTemplate(xmlutil.TemplateBuilder): - def construct(self): - sel = xmlutil.Selector('meta', xmlutil.get_items, 0) - root = xmlutil.TemplateElement('meta', selector=sel) - root.set('key', 0) - root.text = 1 - return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap) - - -class MetadataTemplateElement(xmlutil.TemplateElement): - def will_render(self, datum): - return True - - -class MetadataTemplate(xmlutil.TemplateBuilder): - def construct(self): - root = MetadataTemplateElement('metadata', selector='metadata') - elem = xmlutil.SubTemplateElement(root, 'meta', - selector=xmlutil.get_items) - elem.set('key', 0) - elem.text = 1 - return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap) - - def check_snapshots_enabled(f): @functools.wraps(f) def inner(*args, **kwargs): diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index bb309056e..ec1ec2193 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -215,18 +215,6 @@ class XMLDeserializer(TextDeserializer): return {'body': self._from_xml(datastring)} -class MetadataXMLDeserializer(XMLDeserializer): - - def extract_metadata(self, metadata_node): - """Marshal the metadata attribute of a parsed request""" - metadata = {} - if metadata_node is not None: - for meta_node in self.find_children_named(metadata_node, "meta"): - key = meta_node.getAttribute("key") - metadata[key] = self.extract_text(meta_node) - return metadata - - class DictSerializer(ActionDispatcher): """Default request body serialization""" diff --git a/cinder/tests/api/openstack/test_common.py b/cinder/tests/api/openstack/test_common.py index 162691364..a004ff568 100644 --- a/cinder/tests/api/openstack/test_common.py +++ b/cinder/tests/api/openstack/test_common.py @@ -299,200 +299,3 @@ class MiscFunctionsTest(test.TestCase): expected = '2' actual = common.get_version_from_href(fixture) self.assertEqual(actual, expected) - - -class MetadataXMLDeserializationTest(test.TestCase): - - deserializer = common.MetadataXMLDeserializer() - - def test_create(self): - request_body = """ - - asdf - jkl; - """ - output = self.deserializer.deserialize(request_body, 'create') - expected = {"body": {"metadata": {"123": "asdf", "567": "jkl;"}}} - self.assertEquals(output, expected) - - def test_create_empty(self): - request_body = """ - """ - output = self.deserializer.deserialize(request_body, 'create') - expected = {"body": {"metadata": {}}} - self.assertEquals(output, expected) - - def test_update_all(self): - request_body = """ - - asdf - jkl; - """ - output = self.deserializer.deserialize(request_body, 'update_all') - expected = {"body": {"metadata": {"123": "asdf", "567": "jkl;"}}} - self.assertEquals(output, expected) - - def test_update(self): - request_body = """ - asdf""" - output = self.deserializer.deserialize(request_body, 'update') - expected = {"body": {"meta": {"123": "asdf"}}} - self.assertEquals(output, expected) - - -class MetadataXMLSerializationTest(test.TestCase): - - def test_xml_declaration(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - 'one': 'two', - 'three': 'four', - }, - } - - output = serializer.serialize(fixture) - print output - has_dec = output.startswith("") - self.assertTrue(has_dec) - - def test_index(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - 'one': 'two', - 'three': 'four', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - xmlutil.validate_schema(root, 'metadata') - metadata_dict = fixture['metadata'] - metadata_elems = root.findall('{0}meta'.format(NS)) - self.assertEqual(len(metadata_elems), 2) - for i, metadata_elem in enumerate(metadata_elems): - (meta_key, meta_value) = metadata_dict.items()[i] - self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) - self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) - - def test_index_null(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - None: None, - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - xmlutil.validate_schema(root, 'metadata') - metadata_dict = fixture['metadata'] - metadata_elems = root.findall('{0}meta'.format(NS)) - self.assertEqual(len(metadata_elems), 1) - for i, metadata_elem in enumerate(metadata_elems): - (meta_key, meta_value) = metadata_dict.items()[i] - self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) - self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) - - def test_index_unicode(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - u'three': u'Jos\xe9', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - xmlutil.validate_schema(root, 'metadata') - metadata_dict = fixture['metadata'] - metadata_elems = root.findall('{0}meta'.format(NS)) - self.assertEqual(len(metadata_elems), 1) - for i, metadata_elem in enumerate(metadata_elems): - (meta_key, meta_value) = metadata_dict.items()[i] - self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) - self.assertEqual(metadata_elem.text.strip(), meta_value) - - def test_show(self): - serializer = common.MetaItemTemplate() - fixture = { - 'meta': { - 'one': 'two', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - meta_dict = fixture['meta'] - (meta_key, meta_value) = meta_dict.items()[0] - self.assertEqual(str(root.get('key')), str(meta_key)) - self.assertEqual(root.text.strip(), meta_value) - - def test_update_all(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - 'key6': 'value6', - 'key4': 'value4', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - xmlutil.validate_schema(root, 'metadata') - metadata_dict = fixture['metadata'] - metadata_elems = root.findall('{0}meta'.format(NS)) - self.assertEqual(len(metadata_elems), 2) - for i, metadata_elem in enumerate(metadata_elems): - (meta_key, meta_value) = metadata_dict.items()[i] - self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) - self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) - - def test_update_item(self): - serializer = common.MetaItemTemplate() - fixture = { - 'meta': { - 'one': 'two', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - meta_dict = fixture['meta'] - (meta_key, meta_value) = meta_dict.items()[0] - self.assertEqual(str(root.get('key')), str(meta_key)) - self.assertEqual(root.text.strip(), meta_value) - - def test_create(self): - serializer = common.MetadataTemplate() - fixture = { - 'metadata': { - 'key9': 'value9', - 'key2': 'value2', - 'key1': 'value1', - }, - } - output = serializer.serialize(fixture) - print output - root = etree.XML(output) - xmlutil.validate_schema(root, 'metadata') - metadata_dict = fixture['metadata'] - metadata_elems = root.findall('{0}meta'.format(NS)) - self.assertEqual(len(metadata_elems), 3) - for i, metadata_elem in enumerate(metadata_elems): - (meta_key, meta_value) = metadata_dict.items()[i] - self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) - self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - value2 - value9 - value1 - - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) -- 2.45.2