Refactor template simple_parse
The places that use simple_parse all have to check if the returned value is a dict, so that logic can be safely moved into simple_parse itself to avoid duplication. Co-Authored-By: Asif Choudhury <choudhury.asif@gmail.com> Change-Id: I973f97fa5ce46e7492611555759d20131b98ab07
This commit is contained in:
parent
0661277f11
commit
5ff4489314
@ -101,15 +101,9 @@ class InstantiationData(object):
|
||||
adopt_data = self.data[engine_api.PARAM_ADOPT_STACK_DATA]
|
||||
try:
|
||||
adopt_data = template_format.simple_parse(adopt_data)
|
||||
|
||||
if not isinstance(adopt_data, dict):
|
||||
raise exc.HTTPBadRequest(
|
||||
_('Adopt data %s invalid. Adopt data must be a dict.')
|
||||
% adopt_data)
|
||||
|
||||
return adopt_data['template']
|
||||
except (ValueError, KeyError) as ex:
|
||||
err_reason = _('Invalid data: %s') % ex
|
||||
err_reason = _('Invalid adopt data: %s') % ex
|
||||
raise exc.HTTPBadRequest(err_reason)
|
||||
elif self.PARAM_TEMPLATE in self.data:
|
||||
template_data = self.data[self.PARAM_TEMPLATE]
|
||||
|
@ -59,6 +59,11 @@ def simple_parse(tmpl_str):
|
||||
else:
|
||||
if tpl is None:
|
||||
tpl = {}
|
||||
|
||||
if not isinstance(tpl, dict):
|
||||
raise ValueError(_('The template is not a JSON object '
|
||||
'or YAML mapping.'))
|
||||
|
||||
return tpl
|
||||
|
||||
|
||||
@ -73,9 +78,6 @@ def parse(tmpl_str):
|
||||
cfg.CONF.max_template_size)
|
||||
raise exception.RequestLimitExceeded(message=msg)
|
||||
tpl = simple_parse(tmpl_str)
|
||||
if not isinstance(tpl, dict):
|
||||
raise ValueError(_('The template is not a JSON object '
|
||||
'or YAML mapping.'))
|
||||
# Looking for supported version keys in the loaded template
|
||||
if not ('HeatTemplateFormatVersion' in tpl
|
||||
or 'heat_template_version' in tpl
|
||||
|
@ -53,11 +53,10 @@ def extract_args(params):
|
||||
|
||||
adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA)
|
||||
if adopt_data:
|
||||
try:
|
||||
adopt_data = template_format.simple_parse(adopt_data)
|
||||
if not isinstance(adopt_data, dict):
|
||||
raise ValueError(
|
||||
_('Unexpected adopt data "%s". Adopt data must be a dict.')
|
||||
% adopt_data)
|
||||
except ValueError as exc:
|
||||
raise ValueError(_('Invalid adopt data: %s') % exc)
|
||||
kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data
|
||||
|
||||
return kwargs
|
||||
|
@ -804,7 +804,7 @@ class StackControllerTest(ControllerTest, HeatTestCase):
|
||||
body=body)
|
||||
self.assertEqual(400, resp.status_code)
|
||||
self.assertEqual('400 Bad Request', resp.status)
|
||||
self.assertIn('Adopt data must be a dict.', resp.text)
|
||||
self.assertIn('Invalid adopt data', resp.text)
|
||||
self.m.VerifyAll()
|
||||
|
||||
def test_create_with_files(self, mock_enforce):
|
||||
|
@ -944,11 +944,9 @@ class TestExtractArgs(HeatTestCase):
|
||||
self.assertTrue(args.get('adopt_stack_data'))
|
||||
|
||||
def test_invalid_adopt_stack_data(self):
|
||||
p = {'adopt_stack_data': json.dumps("foo")}
|
||||
error = self.assertRaises(ValueError, api.extract_args, p)
|
||||
self.assertEqual(
|
||||
'Unexpected adopt data "foo". Adopt data must be a dict.',
|
||||
six.text_type(error))
|
||||
params = {'adopt_stack_data': json.dumps("foo")}
|
||||
exc = self.assertRaises(ValueError, api.extract_args, params)
|
||||
self.assertIn('Invalid adopt data', six.text_type(exc))
|
||||
|
||||
def test_adopt_stack_data_extract_not_present(self):
|
||||
args = api.extract_args({})
|
||||
|
Loading…
Reference in New Issue
Block a user