from heat.common import identifier
from heat.common import wsgi
from heat.common import template_format
+from heat.common import environment_format
from heat.rpc import api as engine_api
from heat.rpc import client as rpc_client
from heat.common import urlfetch
self.data = data
@staticmethod
- def format_parse(data, data_type, add_template_sections=True):
+ def format_parse(data, data_type):
"""
Parse the supplied data as JSON or YAML, raising the appropriate
exception if it is in the wrong format.
"""
try:
- return template_format.parse(data,
- add_template_sections)
+ if data_type == 'Environment':
+ return environment_format.parse(data)
+ else:
+ return template_format.parse(data)
except ValueError:
err_reason = _("%s not in valid format") % data_type
raise exc.HTTPBadRequest(err_reason)
env = env_data
else:
env = self.format_parse(env_data,
- 'Environment',
- add_template_sections=False)
-
- for field in env:
- if field not in ('parameters', 'resource_registry'):
- reason = _("%s not in valid in the environment") % field
- raise exc.HTTPBadRequest(reason)
-
- if not env.get(self.PARAM_USER_PARAMS):
- env[self.PARAM_USER_PARAMS] = {}
+ 'Environment')
+ environment_format.default_for_missing(env)
parameters = self.data.get(self.PARAM_USER_PARAMS, {})
env[self.PARAM_USER_PARAMS].update(parameters)
return env
--- /dev/null
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from heat.common.template_format import yaml
+
+
+SECTIONS = (PARAMETERS, RESOURCE_REGISTRY) = \
+ ('parameters', 'resource_registry')
+
+
+def parse(env_str):
+ '''
+ Takes a string and returns a dict containing the parsed structure.
+ This includes determination of whether the string is using the
+ JSON or YAML format.
+ '''
+ try:
+ env = yaml.safe_load(env_str)
+ except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e:
+ raise ValueError(e)
+ else:
+ if env is None:
+ env = {}
+
+ for param in env:
+ if param not in SECTIONS:
+ raise ValueError('environment has wrong section "%s"' % param)
+
+ return env
+
+
+def default_for_missing(env):
+ '''
+ Checks a parsed environment for missing sections.
+ '''
+ for param in SECTIONS:
+ if param not in env:
+ env[param] = {}
def test_parameters(self):
params = {'foo': 'bar', 'blarg': 'wibble'}
- body = {'parameters': params}
+ body = {'parameters': params,
+ 'resource_registry': {}}
data = stacks.InstantiationData(body)
self.assertEqual(data.environment(), body)
body = {'parameters': {'foo': 'bar'},
'environment': {'parameters': {'blarg': 'wibble'}}}
expect = {'parameters': {'blarg': 'wibble',
- 'foo': 'bar'}}
+ 'foo': 'bar'},
+ 'resource_registry': {}}
data = stacks.InstantiationData(body)
self.assertEqual(data.environment(), expect)
'tester': 'fail'}}}
expect = {'parameters': {'blarg': 'wibble',
'foo': 'bar',
- 'tester': 'Yes'}}
+ 'tester': 'Yes'},
+ 'resource_registry': {}}
data = stacks.InstantiationData(body)
self.assertEqual(data.environment(), expect)
def test_environment_bad_format(self):
- body = {'environment': {'somethingnotsupported': {'blarg': 'wibble'}}}
+ env = {'somethingnotsupported': {'blarg': 'wibble'}}
+ body = {'environment': json.dumps(env)}
data = stacks.InstantiationData(body)
self.assertRaises(webob.exc.HTTPBadRequest, data.environment)
env = {'foo': 'bar', 'blarg': 'wibble'}
body = {'not the environment': env}
data = stacks.InstantiationData(body)
- self.assertEqual(data.environment(), {'parameters': {}})
+ self.assertEqual(data.environment(),
+ {'parameters': {},
+ 'resource_registry': {}})
def test_args(self):
body = {
'method': 'create_stack',
'args': {'stack_name': identity.stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': identity.stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {'my.yaml': 'This is the file contents.'},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'create_stack',
'args': {'stack_name': stack_name,
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'update_stack',
'args': {'stack_identity': dict(identity),
'template': template,
- 'params': {'parameters': parameters},
+ 'params': {'parameters': parameters,
+ 'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
'method': 'update_stack',
'args': {'stack_identity': dict(identity),
'template': template,
- 'params': {u'parameters': parameters},
+ 'params': {u'parameters': parameters,
+ u'resource_registry': {}},
'files': {},
'args': {'timeout_mins': 30}},
'version': self.api_version},
--- /dev/null
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from heat.common import environment_format
+from heat.tests import common
+
+
+class YamlEnvironmentTest(common.HeatTestCase):
+
+ def test_minimal_yaml(self):
+ yaml1 = ''
+ yaml2 = '''
+parameters: {}
+resource_registry: {}
+'''
+ tpl1 = environment_format.parse(yaml1)
+ environment_format.default_for_missing(tpl1)
+ tpl2 = environment_format.parse(yaml2)
+ self.assertEqual(tpl1, tpl2)
+
+ def test_wrong_sections(self):
+ env = '''
+parameters: {}
+resource_regis: {}
+'''
+ self.assertRaises(ValueError, environment_format.parse, env)
+
+ def test_bad_yaml(self):
+ env = '''
+parameters: }
+'''
+ self.assertRaises(ValueError, environment_format.parse, env)