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]
|
adopt_data = self.data[engine_api.PARAM_ADOPT_STACK_DATA]
|
||||||
try:
|
try:
|
||||||
adopt_data = template_format.simple_parse(adopt_data)
|
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']
|
return adopt_data['template']
|
||||||
except (ValueError, KeyError) as ex:
|
except (ValueError, KeyError) as ex:
|
||||||
err_reason = _('Invalid data: %s') % ex
|
err_reason = _('Invalid adopt data: %s') % ex
|
||||||
raise exc.HTTPBadRequest(err_reason)
|
raise exc.HTTPBadRequest(err_reason)
|
||||||
elif self.PARAM_TEMPLATE in self.data:
|
elif self.PARAM_TEMPLATE in self.data:
|
||||||
template_data = self.data[self.PARAM_TEMPLATE]
|
template_data = self.data[self.PARAM_TEMPLATE]
|
||||||
|
@ -59,6 +59,11 @@ def simple_parse(tmpl_str):
|
|||||||
else:
|
else:
|
||||||
if tpl is None:
|
if tpl is None:
|
||||||
tpl = {}
|
tpl = {}
|
||||||
|
|
||||||
|
if not isinstance(tpl, dict):
|
||||||
|
raise ValueError(_('The template is not a JSON object '
|
||||||
|
'or YAML mapping.'))
|
||||||
|
|
||||||
return tpl
|
return tpl
|
||||||
|
|
||||||
|
|
||||||
@ -73,9 +78,6 @@ def parse(tmpl_str):
|
|||||||
cfg.CONF.max_template_size)
|
cfg.CONF.max_template_size)
|
||||||
raise exception.RequestLimitExceeded(message=msg)
|
raise exception.RequestLimitExceeded(message=msg)
|
||||||
tpl = simple_parse(tmpl_str)
|
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
|
# Looking for supported version keys in the loaded template
|
||||||
if not ('HeatTemplateFormatVersion' in tpl
|
if not ('HeatTemplateFormatVersion' in tpl
|
||||||
or 'heat_template_version' 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)
|
adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA)
|
||||||
if adopt_data:
|
if adopt_data:
|
||||||
adopt_data = template_format.simple_parse(adopt_data)
|
try:
|
||||||
if not isinstance(adopt_data, dict):
|
adopt_data = template_format.simple_parse(adopt_data)
|
||||||
raise ValueError(
|
except ValueError as exc:
|
||||||
_('Unexpected adopt data "%s". Adopt data must be a dict.')
|
raise ValueError(_('Invalid adopt data: %s') % exc)
|
||||||
% adopt_data)
|
|
||||||
kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data
|
kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
@ -804,7 +804,7 @@ class StackControllerTest(ControllerTest, HeatTestCase):
|
|||||||
body=body)
|
body=body)
|
||||||
self.assertEqual(400, resp.status_code)
|
self.assertEqual(400, resp.status_code)
|
||||||
self.assertEqual('400 Bad Request', resp.status)
|
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()
|
self.m.VerifyAll()
|
||||||
|
|
||||||
def test_create_with_files(self, mock_enforce):
|
def test_create_with_files(self, mock_enforce):
|
||||||
|
@ -944,11 +944,9 @@ class TestExtractArgs(HeatTestCase):
|
|||||||
self.assertTrue(args.get('adopt_stack_data'))
|
self.assertTrue(args.get('adopt_stack_data'))
|
||||||
|
|
||||||
def test_invalid_adopt_stack_data(self):
|
def test_invalid_adopt_stack_data(self):
|
||||||
p = {'adopt_stack_data': json.dumps("foo")}
|
params = {'adopt_stack_data': json.dumps("foo")}
|
||||||
error = self.assertRaises(ValueError, api.extract_args, p)
|
exc = self.assertRaises(ValueError, api.extract_args, params)
|
||||||
self.assertEqual(
|
self.assertIn('Invalid adopt data', six.text_type(exc))
|
||||||
'Unexpected adopt data "foo". Adopt data must be a dict.',
|
|
||||||
six.text_type(error))
|
|
||||||
|
|
||||||
def test_adopt_stack_data_extract_not_present(self):
|
def test_adopt_stack_data_extract_not_present(self):
|
||||||
args = api.extract_args({})
|
args = api.extract_args({})
|
||||||
|
Loading…
Reference in New Issue
Block a user