Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from tempest.common import rest_client |
| 16 | from tempest.services.baremetal.v1 import base_v1 as base |
| 17 | from tempest.services.compute.xml import common as xml |
| 18 | |
| 19 | |
| 20 | class BaremetalClientXML(rest_client.RestClientXML, base.BaremetalClientV1): |
| 21 | """Tempest REST client for Ironic XML API v1.""" |
| 22 | |
| 23 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 24 | super(BaremetalClientXML, self).__init__(config, username, password, |
| 25 | auth_url, tenant_name) |
| 26 | |
| 27 | self.serialize = self.json_to_xml |
| 28 | self.deserialize = xml.xml_to_json |
| 29 | |
| 30 | def json_to_xml(self, object_type, object_dict): |
| 31 | """ |
| 32 | Brainlessly converts a specification of an object to XML string. |
| 33 | |
| 34 | :param object_type: Kind of the object. |
| 35 | :param object_dict: Specification of the object attributes as a dict. |
| 36 | :return: An XML string that corresponds to the specification. |
| 37 | |
| 38 | """ |
| 39 | root = xml.Element(object_type) |
| 40 | |
| 41 | for attr_name, value in object_dict: |
| 42 | # Handle nested dictionaries |
| 43 | if isinstance(value, dict): |
| 44 | value = self.json_to_xml(attr_name, value) |
| 45 | |
| 46 | root.append(xml.Element(attr_name, value)) |
| 47 | |
| 48 | return str(xml.Document(root)) |
| 49 | |
| 50 | def _patch_request(self, resource_name, uuid, patch_object): |
| 51 | """Changes Content-Type header to application/json for jsonpatch.""" |
| 52 | |
| 53 | self.headers['Content-Type'] = 'application/json' |
| 54 | try: |
| 55 | super(self)._patch_request(self, resource_name, uuid, patch_object) |
| 56 | finally: |
| 57 | self.headers['Content-Type'] = 'application/xml' |