Move template-format parsing code into common

This is used mostly by the API (but also the engine for composed
templates), so it doesn't belong in the engine package.

Change-Id: I79cf8cc619a6988013a58eb4d46c014c3dcac4a3
Signed-off-by: Zane Bitter <zbitter@redhat.com>
This commit is contained in:
Zane Bitter 2012-11-30 14:12:38 +01:00
parent b35cc6d06e
commit cd2fd324a0
24 changed files with 66 additions and 66 deletions

View File

@ -26,7 +26,7 @@ from heat.api.aws import exception
from heat.api.aws import utils as api_utils
from heat.common import wsgi
from heat.engine import rpcapi as engine_rpcapi
from heat.engine import format
from heat.common import template_format
import heat.engine.api as engine_api
from heat.common import identifier
@ -316,7 +316,7 @@ class StackController(object):
return exception.HeatMissingParameterError(detail=msg)
try:
stack = format.parse_to_template(templ)
stack = template_format.parse(templ)
except ValueError:
msg = _("The Template must be a JSON document.")
return exception.HeatInvalidParameterValueError(detail=msg)

View File

@ -26,7 +26,7 @@ from webob import exc
from heat.api.openstack.v1 import util
from heat.common import wsgi
from heat.engine import format
from heat.common import template_format
from heat.engine import api as engine_api
from heat.engine import rpcapi as engine_rpcapi
@ -65,7 +65,7 @@ class InstantiationData(object):
"""
try:
return format.parse_to_template(data)
return template_format.parse(data)
except ValueError:
err_reason = "%s not in valid format" % data_type
raise exc.HTTPBadRequest(explanation=err_reason)

View File

@ -29,7 +29,7 @@ yaml.Loader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
def parse_to_template(tmpl_str):
def parse(tmpl_str):
'''
Takes a string and returns a dict containing the parsed structure.
This includes determination of whether the string is using the

View File

@ -430,7 +430,7 @@ def resolve_static_data(template, parameters, snippet):
Example:
>>> template = Template(format.parse_to_template(template_path))
>>> template = Template(template_format.parse(template_path))
>>> parameters = Parameters('stack', template, {'KeyName': 'my_key'})
>>> resolve_static_data(template, parameters, {'Ref': 'KeyName'})
'my_key'

View File

@ -15,7 +15,7 @@
from heat.common import exception
from heat.engine.resources import stack
from heat.engine import format
from heat.common import template_format
from heat.openstack.common import log as logging
logger = logging.getLogger(__file__)
@ -219,7 +219,7 @@ class DBInstance(stack.Stack):
return p
def handle_create(self):
templ = format.parse_to_template(mysql_template)
templ = template_format.parse(mysql_template)
self.create_with_template(templ)
def FnGetAtt(self, key):

View File

@ -14,7 +14,7 @@
# under the License.
from heat.common import exception
from heat.engine import format
from heat.common import template_format
from heat.engine.resources import stack
from novaclient.exceptions import NotFound
@ -283,7 +283,7 @@ class LoadBalancer(stack.Stack):
return '%s%s%s%s\n' % (gl, frontend, backend, '\n'.join(servers))
def handle_create(self):
templ = format.parse_to_template(lb_template)
templ = template_format.parse(lb_template)
if self.properties['Instances']:
md = templ['Resources']['LB_instance']['Metadata']
@ -313,7 +313,7 @@ class LoadBalancer(stack.Stack):
save it to the db.
rely on the cfn-hup to reconfigure HAProxy
'''
templ = format.parse_to_template(lb_template)
templ = template_format.parse(lb_template)
cfg = self._haproxy_config(templ, inst_list)
md = self.nested()['LB_instance'].metadata

View File

@ -17,7 +17,7 @@ import urllib2
import json
from heat.common import exception
from heat.engine import format
from heat.common import template_format
from heat.engine import resource
from heat.engine import parser
@ -76,7 +76,7 @@ class Stack(resource.Resource):
def handle_create(self):
response = urllib2.urlopen(self.properties[PROP_TEMPLATE_URL])
template = format.parse_to_template(response)
template = template_format.parse(response)
self.create_with_template(template)

View File

@ -21,7 +21,7 @@ from nose.plugins.attrib import attr
import unittest
import json
from heat.engine import format
from heat.common import template_format
@attr(speed='slow')
@ -282,7 +282,7 @@ class CfnApiFunctionalTest(unittest.TestCase):
# Extract the JSON TemplateBody and prove it parses
template = self.stack.response_xml_item(response, prefix,
"TemplateBody")
json_load = format.parse_to_template(template)
json_load = template_format.parse(template)
self.assertTrue(json_load != None)
# Then sanity check content - I guess we could diff

View File

@ -41,7 +41,7 @@ except ImportError:
from novaclient.v1_1 import client as nova_client
import heat
from heat import utils
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat import client as heat_client
from heat import boto_client as heat_client_boto
@ -249,7 +249,7 @@ class Instance(object):
# time.sleep(1) # necessary for sendall to complete
f = open(basepath + '/templates/' + template_file)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
template = parser.Template(t)

View File

@ -24,7 +24,7 @@ import json
from nose.plugins.attrib import attr
from heat.common import context
from heat.engine import format
from heat.common import template_format
from heat.engine.resources import autoscaling as asc
from heat.engine.resources import loadbalancer
from heat.engine import parser
@ -45,7 +45,7 @@ class AutoScalingTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/AutoScalingMultiAZSample.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -25,7 +25,7 @@ from nose.plugins.attrib import attr
from heat.common import context
from heat.common import exception
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine.resources import stack
from heat.engine.resources import dbinstance as dbi
@ -47,7 +47,7 @@ class DBInstanceTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/WordPress_With_RDS.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -24,7 +24,7 @@ import json
from nose.plugins.attrib import attr
from heat.common import context
from heat.engine import format
from heat.common import template_format
from heat.engine.resources import eip
from heat.engine import parser
from heat.tests.v1_1 import fakes
@ -48,7 +48,7 @@ class EIPTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/WordPress_Single_Instance_With_EIP.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -27,7 +27,7 @@ from heat.common import context
from heat.tests.v1_1 import fakes
import heat.engine.api as engine_api
import heat.db as db_api
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine import service
from heat.engine.resources import instance as instances
@ -55,7 +55,7 @@ def get_wordpress_stack(stack_name, ctx):
tmpl_path = os.path.join(templates_dir,
'WordPress_Single_Instance_gold.template')
with open(tmpl_path) as f:
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
template = parser.Template(t)
parameters = parser.Parameters(stack_name, template,

View File

@ -28,7 +28,7 @@ from nose import with_setup
from heat.tests.v1_1 import fakes
from heat.engine.resources import instance as instances
import heat.db as db_api
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
@ -47,7 +47,7 @@ class instancesTest(unittest.TestCase):
def test_instance_create(self):
f = open("%s/WordPress_Single_Instance_gold.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
template = parser.Template(t)
@ -89,7 +89,7 @@ class instancesTest(unittest.TestCase):
def test_instance_create_delete(self):
f = open("%s/WordPress_Single_Instance_gold.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
template = parser.Template(t)

View File

@ -27,7 +27,7 @@ from nose.plugins.attrib import attr
from heat.common import exception
from heat.common import context
from heat.common import config
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine.resources import instance
from heat.engine.resources import loadbalancer as lb
@ -66,7 +66,7 @@ class LoadBalancerTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/WordPress_With_LB.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t
@ -124,7 +124,7 @@ class LoadBalancerTest(unittest.TestCase):
self.assertEqual('LoadBalancer', resource.FnGetRefId())
templ = format.parse_to_template(lb.lb_template)
templ = template_format.parse(lb.lb_template)
ha_cfg = resource._haproxy_config(templ,
resource.properties['Instances'])
self.assertRegexpMatches(ha_cfg, 'bind \*:80')

View File

@ -22,7 +22,7 @@ import sys
from heat.common import context
from heat.common import exception
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine import parameters
from heat.engine import template
@ -96,7 +96,7 @@ class ParserTest(unittest.TestCase):
self.assertEqual(join(raw), 'foo bar\nbaz')
mapping_template = format.parse_to_template('''{
mapping_template = template_format.parse('''{
"Mappings" : {
"ValidMapping" : {
"TestKey" : { "TestValue" : "wibble" }

View File

@ -25,7 +25,7 @@ from nose.plugins.attrib import attr
from heat.common import context
from heat.common import exception
from heat.engine import format
from heat.common import template_format
from heat.engine import properties
from heat.engine.resources.quantum import net
from heat.engine.resources.quantum.quantum import QuantumResource as qr
@ -82,7 +82,7 @@ class QuantumTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/Quantum.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -25,7 +25,7 @@ import json
from nose.plugins.attrib import attr
from heat.common import context
from heat.engine import format
from heat.common import template_format
from heat.engine.resources import s3
from heat.engine import parser
from utils import skip_if
@ -59,7 +59,7 @@ class s3Test(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/S3_Single_Instance.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -22,7 +22,7 @@ import unittest
import yaml
from heat.common import context
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
@ -53,16 +53,16 @@ class JsonToYamlTest(unittest.TestCase):
self.expected_test_count)
def compare_json_vs_yaml(self, json_str, yml_str, file_name):
yml = format.parse_to_template(yml_str)
yml = template_format.parse(yml_str)
self.assertEqual(u'2012-12-12', yml[u'HeatTemplateFormatVersion'],
file_name)
self.assertFalse(u'AWSTemplateFormatVersion' in yml, file_name)
del(yml[u'HeatTemplateFormatVersion'])
jsn = format.parse_to_template(json_str)
format.default_for_missing(jsn, 'AWSTemplateFormatVersion',
format.CFN_VERSIONS)
jsn = template_format.parse(json_str)
template_format.default_for_missing(jsn, 'AWSTemplateFormatVersion',
template_format.CFN_VERSIONS)
if u'AWSTemplateFormatVersion' in jsn:
del(jsn[u'AWSTemplateFormatVersion'])
@ -76,7 +76,7 @@ class JsonToYamlTest(unittest.TestCase):
f = open(os.path.join(dirpath, path), 'r')
json_str = f.read()
yml_str = format.convert_json_to_yaml(json_str)
yml_str = template_format.convert_json_to_yaml(json_str)
yield (json_str, yml_str, f.name)
@ -91,8 +91,8 @@ Mappings: {}
Resources: {}
Outputs: {}
'''
tpl1 = format.parse_to_template(yaml1)
tpl2 = format.parse_to_template(yaml2)
tpl1 = template_format.parse(yaml1)
tpl2 = template_format.parse(yaml2)
self.assertEqual(tpl1, tpl2)
@ -107,7 +107,7 @@ class JsonYamlResolvedCompareTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/%s" % (self.path, file_name))
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t
@ -126,8 +126,8 @@ class JsonYamlResolvedCompareTest(unittest.TestCase):
def compare_stacks(self, json_file, yaml_file, parameters):
t1 = self.load_template(json_file)
format.default_for_missing(t1, 'AWSTemplateFormatVersion',
format.CFN_VERSIONS)
template_format.default_for_missing(t1, 'AWSTemplateFormatVersion',
template_format.CFN_VERSIONS)
del(t1[u'AWSTemplateFormatVersion'])
t2 = self.load_template(yaml_file)

View File

@ -27,7 +27,7 @@ from nose.plugins.attrib import attr
from heat.common import context
from heat.common import exception
from heat.common import config
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine.resources import user
from heat.tests import fakes
@ -51,7 +51,7 @@ class UserTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/Rails_Single_Instance.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -20,7 +20,7 @@ import json
from nose.plugins.attrib import attr
from heat.tests.v1_1 import fakes
from heat.engine import format
from heat.common import template_format
from heat.engine.resources import instance as instances
from heat.engine import service
import heat.db as db_api
@ -220,7 +220,7 @@ class validateTest(unittest.TestCase):
print "volumeTest teardown complete"
def test_validate_volumeattach_valid(self):
t = format.parse_to_template(test_template_volumeattach % 'vdq')
t = template_format.parse(test_template_volumeattach % 'vdq')
stack = parser.Stack(None, 'test_stack', parser.Template(t))
self.m.StubOutWithMock(db_api, 'resource_get_by_name_and_stack')
@ -232,7 +232,7 @@ class validateTest(unittest.TestCase):
self.assertTrue(volumeattach.validate() is None)
def test_validate_volumeattach_invalid(self):
t = format.parse_to_template(test_template_volumeattach % 'sda')
t = template_format.parse(test_template_volumeattach % 'sda')
stack = parser.Stack(None, 'test_stack', parser.Template(t))
self.m.StubOutWithMock(db_api, 'resource_get_by_name_and_stack')
@ -244,7 +244,7 @@ class validateTest(unittest.TestCase):
self.assertTrue(volumeattach.validate())
def test_validate_ref_valid(self):
t = format.parse_to_template(test_template_ref % 'WikiDatabase')
t = template_format.parse(test_template_ref % 'WikiDatabase')
self.m.StubOutWithMock(instances.Instance, 'nova')
instances.Instance.nova().AndReturn(self.fc)
@ -257,7 +257,7 @@ class validateTest(unittest.TestCase):
self.assertEqual(res['Description'], 'test.')
def test_validate_ref_invalid(self):
t = format.parse_to_template(test_template_ref % 'WikiDatabasez')
t = template_format.parse(test_template_ref % 'WikiDatabasez')
self.m.StubOutWithMock(instances.Instance, 'nova')
instances.Instance.nova().AndReturn(self.fc)
@ -269,7 +269,7 @@ class validateTest(unittest.TestCase):
self.assertNotEqual(res['Description'], 'Successfully validated')
def test_validate_findinmap_valid(self):
t = format.parse_to_template(test_template_findinmap_valid)
t = template_format.parse(test_template_findinmap_valid)
self.m.StubOutWithMock(instances.Instance, 'nova')
instances.Instance.nova().AndReturn(self.fc)
@ -281,7 +281,7 @@ class validateTest(unittest.TestCase):
self.assertEqual(res['Description'], 'test.')
def test_validate_findinmap_invalid(self):
t = format.parse_to_template(test_template_findinmap_invalid)
t = template_format.parse(test_template_findinmap_invalid)
self.m.StubOutWithMock(instances.Instance, 'nova')
instances.Instance.nova().AndReturn(self.fc)

View File

@ -25,7 +25,7 @@ import unittest
from nose.plugins.attrib import attr
from heat.common import context
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine.resources import volume as vol
from heat.tests.v1_1 import fakes
@ -54,7 +54,7 @@ class VolumeTest(unittest.TestCase):
self.path = os.path.dirname(os.path.realpath(__file__)).\
replace('heat/tests', 'templates')
f = open("%s/WordPress_2_Instances_With_EBS.template" % self.path)
t = format.parse_to_template(f.read())
t = template_format.parse(f.read())
f.close()
return t

View File

@ -27,7 +27,7 @@ from nose.plugins.attrib import attr
from heat.tests import fakes
import heat.db as db_api
from heat.engine import format
from heat.common import template_format
from heat.engine import parser
from heat.engine.resources import wait_condition as wc
from heat.common import context
@ -88,7 +88,7 @@ class WaitConditionTest(unittest.TestCase):
def test_post_success_to_handle(self):
t = format.parse_to_template(test_template_waitcondition)
t = template_format.parse(test_template_waitcondition)
stack = self.create_stack('test_stack', t, {})
wc.WaitCondition._create_timeout().AndReturn(eventlet.Timeout(5))
@ -120,7 +120,7 @@ class WaitConditionTest(unittest.TestCase):
def test_timeout(self):
t = format.parse_to_template(test_template_waitcondition)
t = template_format.parse(test_template_waitcondition)
stack = self.create_stack('test_stack', t, {})
tmo = eventlet.Timeout(6)
@ -166,7 +166,7 @@ class WaitConditionHandleTest(unittest.TestCase):
self.m.UnsetStubs()
def create_stack(self, stack_name='test_stack2', params={}):
temp = format.parse_to_template(test_template_waitcondition)
temp = template_format.parse(test_template_waitcondition)
template = parser.Template(temp)
parameters = parser.Parameters(stack_name, template, params)
stack = parser.Stack(context.get_admin_context(), stack_name,

View File

@ -19,7 +19,7 @@ import os
import yaml
import json
import re
from heat.engine import format
from heat.common import template_format
def main():
path = sys.argv[1]
@ -32,7 +32,7 @@ def main():
def convert_file(path):
f = open(path, 'r')
print format.convert_json_to_yaml(f.read())
print template_format.convert_json_to_yaml(f.read())
def convert_directory(dirpath):
for path in os.listdir(dirpath):
@ -42,7 +42,7 @@ def convert_directory(dirpath):
print 'Writing to %s' % yamlpath
f = open(os.path.join(dirpath, path), 'r')
out = open(os.path.join(dirpath, yamlpath), 'w')
yml = format.convert_json_to_yaml(f.read())
yml = template_format.convert_json_to_yaml(f.read())
out.write(yml)
out.close()