diff --git a/heat/common/template_format.py b/heat/common/template_format.py index bd64c1ae72..0e9be77f64 100644 --- a/heat/common/template_format.py +++ b/heat/common/template_format.py @@ -108,7 +108,7 @@ def convert_json_to_yaml(json_str): next(key_order), matchobj.group(2)) return key - key_re = re.compile('^(\s*)"([^"]+)"\s*:', re.M) + key_re = re.compile('(\s*)"([^"]+)"\s*:') json_str = key_re.sub(order_key, json_str) # parse the string as json to a python structure @@ -120,4 +120,7 @@ def convert_json_to_yaml(json_str): # remove ordering from key names yml = re.sub('__\d*__order__', '', yml) + + # convert integer keys back to string + yml = re.sub('([\s,{])(\d+)(\s*):', r"\1'\2'\3:", yml) return yml diff --git a/heat/tests/templates/WordPress_Single_Instance.template b/heat/tests/templates/WordPress_Single_Instance.template index 3d79430261..1c40ef1f4d 100644 --- a/heat/tests/templates/WordPress_Single_Instance.template +++ b/heat/tests/templates/WordPress_Single_Instance.template @@ -82,7 +82,10 @@ "cc1.4xlarge" : { "Arch" : "64" } }, "DistroArch2AMI": { - "F18" : { "32" : "F18-i386-cfntools", "64" : "F18-x86_64-cfntools" }, + "F18" : { + "32" : "F18-i386-cfntools", + "64" : "F18-x86_64-cfntools" + }, "F17" : { "32" : "F17-i386-cfntools", "64" : "F17-x86_64-cfntools" }, "U10" : { "32" : "U10-i386-cfntools", "64" : "U10-x86_64-cfntools" }, "RHEL-6.1" : { "32" : "rhel61-i386-cfntools", "64" : "rhel61-x86_64-cfntools" }, diff --git a/heat/tests/test_template_format.py b/heat/tests/test_template_format.py index 33692fb2ed..2ad212f38d 100644 --- a/heat/tests/test_template_format.py +++ b/heat/tests/test_template_format.py @@ -14,6 +14,7 @@ import os import mock +import re import six import yaml @@ -75,6 +76,17 @@ class JsonToYamlTest(common.HeatTestCase): yml_str = template_format.convert_json_to_yaml(json_str) yield (json_str, yml_str, f.name) + def test_integer_only_keys_get_translated_correctly(self): + path = os.path.join(os.path.dirname(os.path.realpath(__file__)), + 'templates/WordPress_Single_Instance.template') + with open(path, 'r') as f: + json_str = f.read() + yml_str = template_format.convert_json_to_yaml(json_str) + match = re.search('[\s,{]\d+\s*:', yml_str) + # Check that there are no matches of integer-only keys + # lacking explicit quotes + self.assertEqual(match, None) + class YamlMinimalTest(common.HeatTestCase):