import yaml
import json
+HEAT_VERSIONS = (u'2012-12-12',)
+CFN_VERSIONS = (u'2010-09-09',)
+
def _construct_yaml_str(self, node):
# Override the default string handling function
JSON or YAML format.
'''
if tmpl_str.startswith('{'):
- return json.loads(tmpl_str)
- try:
- return yaml.load(tmpl_str)
- except yaml.scanner.ScannerError as e:
- raise ValueError(e)
+ tpl = json.loads(tmpl_str)
+ else:
+ try:
+ tpl = yaml.load(tmpl_str)
+ except yaml.scanner.ScannerError as e:
+ raise ValueError(e)
+ else:
+ if tpl == None:
+ tpl = {}
+ default_for_missing(tpl, u'HeatTemplateFormatVersion',
+ HEAT_VERSIONS)
+ return tpl
+
+
+def default_for_missing(tpl, version_param, versions):
+ '''
+ Checks a parsed template for missing version and sections.
+
+ This is currently only applied to YAML templates.
+ '''
+ # if version is missing, implicitly use the lastest one
+ if not version_param in tpl:
+ tpl[version_param] = versions[-1]
+
+ # create empty placeholders for any of the main dict sections
+ for param in (u'Parameters', u'Mappings', u'Resources', u'Outputs'):
+ if not param in tpl:
+ tpl[param] = {}
def convert_json_to_yaml(json_str):
template = '''foo: bar
blarg: wibble
'''
+ parsed = {u'HeatTemplateFormatVersion': u'2012-12-12',
+ u'Mappings': {},
+ u'Outputs': {},
+ u'Parameters': {},
+ u'Resources': {},
+ u'blarg': u'wibble',
+ u'foo': u'bar'}
+
body = {'template': template}
data = stacks.InstantiationData(body)
- self.assertEqual(data.template(), yaml.load(template))
+ self.assertEqual(data.template(), parsed)
def test_template_url(self):
template = {'foo': 'bar', 'blarg': 'wibble'}
import unittest
import yaml
+from heat.common import context
from heat.engine import format
from heat.engine import parser
class JsonToYamlTest(unittest.TestCase):
def setUp(self):
- self.expected_test_count = 29
+ self.expected_test_count = 10
self.longMessage = True
+ self.maxDiff = None
def test_convert_all_templates(self):
path = os.path.dirname(os.path.realpath(__file__)).\
self.compare_json_vs_yaml(json_str, yml_str, file_name)
template_test_count += 1
+ if template_test_count >= self.expected_test_count:
+ break
self.assertTrue(template_test_count >= self.expected_test_count,
'Expected at least %d templates to be tested' %
del(yml[u'HeatTemplateFormatVersion'])
jsn = format.parse_to_template(json_str)
+ format.default_for_missing(jsn, 'AWSTemplateFormatVersion',
+ format.CFN_VERSIONS)
+
if u'AWSTemplateFormatVersion' in jsn:
del(jsn[u'AWSTemplateFormatVersion'])
yml_str = format.convert_json_to_yaml(json_str)
yield (json_str, yml_str, f.name)
+
+
+@attr(tag=['unit'])
+class YamlMinimalTest(unittest.TestCase):
+
+ def test_minimal_yaml(self):
+ yaml1 = ''
+ yaml2 = '''HeatTemplateFormatVersion: '2012-12-12'
+Parameters: {}
+Mappings: {}
+Resources: {}
+Outputs: {}
+'''
+ tpl1 = format.parse_to_template(yaml1)
+ tpl2 = format.parse_to_template(yaml2)
+ self.assertEqual(tpl1, tpl2)