Commit 3847c7b6 authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼

fix xml2dixt and dict2xml

software releases expects _ parameter to be a string with JSON inside
not a python dict so we partially revert 2acef14b
and we do the nice printing of JSON parameters in getInformation
parent e0b4339e
......@@ -41,6 +41,7 @@ import logging
import re
from functools import wraps
import ast
import six
from .exception import ResourceNotReady, ServerError, NotFoundError, \
......@@ -279,13 +280,17 @@ class OpenOrder(SlapRequester):
setattr(software_instance, '_%s' % key, value)
if raw_information["data"].get("text_content", None) is not None:
setattr(software_instance, '_parameter_dict', xml2dict(unicode2str(raw_information["data"]['text_content'])))
result_dict = xml2dict(unicode2str(raw_information["data"]['text_content']))
if len(result_dict) == 1 and '_' in result_dict:
# use ast.literal_eval instead of json.loads because the string contains single quote
result_dict['_'] = ast.literal_eval(result_dict['_'])
software_instance._parameter_dict = result_dict
else:
setattr(software_instance, '_parameter_dict', {})
software_instance._parameter_dict = {}
setattr(software_instance, '_requested_state', raw_information["data"]['slap_state'])
setattr(software_instance, '_connection_dict', raw_information["data"]['connection_parameter_list'])
setattr(software_instance, '_software_release_url', raw_information["data"]['url_string'])
software_instance._requested_state = raw_information["data"]['slap_state']
software_instance._connection_dict = raw_information["data"]['connection_parameter_list']
software_instance._software_release_url = raw_information["data"]['url_string']
return software_instance
def requestComputer(self, computer_reference):
......
......@@ -191,17 +191,20 @@ class TestUtil(unittest.TestCase):
"""
xml2dict1_dict = {
"_": {
"param1": "value1",
"param2_dict": {
"param2_param1": "",
"param2_param2_dict": {},
"param2_param3_dict": {"param": "value"}
}
"_": str(b'''{
"param1": "value1",
"param2_dict": {
"param2_param1": "",
"param2_param2_dict": {},
"param2_param3_dict": {
"param": "value"
}
}
}''')
}
def test_xml2dict1(self):
self.maxDiff = None
self.assertEqual(
self.xml2dict1_dict,
slapos.util.xml2dict(self.xml2dict1_xml)
......
......@@ -33,7 +33,6 @@ import socket
import struct
import subprocess
import sqlite3
import json
from xml_marshaller.xml_marshaller import dumps, loads
from lxml import etree
import six
......@@ -163,22 +162,14 @@ else:
def dict2xml(dictionary):
instance = etree.Element('instance')
if len(dictionary) == 1 and '_' in dictionary:
for k, v in sorted(six.iteritems(dictionary)):
if isinstance(k, bytes):
k = k.decode('utf-8')
if isinstance(v, bytes):
v = v.decode('utf-8')
elif not isinstance(v, six.text_type):
v = str(v)
etree.SubElement(instance, "parameter",
attrib={'id': '_'}).text = json.dumps(
dictionary['_'],
separators=(',', ': '),
sort_keys=True,
indent=4)
else:
for k, v in sorted(six.iteritems(dictionary)):
if isinstance(k, bytes):
k = k.decode('utf-8')
if isinstance(v, bytes):
v = v.decode('utf-8')
elif not isinstance(v, six.text_type):
v = str(v)
etree.SubElement(instance, "parameter",
attrib={'id': k}).text = v
return bytes2str(etree.tostring(instance,
pretty_print=True,
......@@ -198,8 +189,6 @@ def xml2dict(xml):
else:
value = element.text
result_dict[key] = value
if len(result_dict) == 1 and '_' in result_dict:
result_dict['_'] = json.loads(result_dict['_'])
return result_dict
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment