From 5ff44893144e27249f24574e2016ce6c12d46abf Mon Sep 17 00:00:00 2001 From: Anderson Mesquita Date: Wed, 10 Sep 2014 09:14:59 -0700 Subject: [PATCH] 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 Change-Id: I973f97fa5ce46e7492611555759d20131b98ab07 --- heat/api/openstack/v1/stacks.py | 8 +------- heat/common/template_format.py | 8 +++++--- heat/engine/api.py | 9 ++++----- heat/tests/test_api_openstack_v1.py | 2 +- heat/tests/test_engine_api_utils.py | 8 +++----- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/heat/api/openstack/v1/stacks.py b/heat/api/openstack/v1/stacks.py index 63944f3f1b..b5fa731270 100644 --- a/heat/api/openstack/v1/stacks.py +++ b/heat/api/openstack/v1/stacks.py @@ -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] diff --git a/heat/common/template_format.py b/heat/common/template_format.py index dbc9270391..7c7b6f2e8a 100644 --- a/heat/common/template_format.py +++ b/heat/common/template_format.py @@ -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 diff --git a/heat/engine/api.py b/heat/engine/api.py index 2ffad97de5..f5cf8b3cfa 100644 --- a/heat/engine/api.py +++ b/heat/engine/api.py @@ -53,11 +53,10 @@ def extract_args(params): adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA) if adopt_data: - 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) + try: + adopt_data = template_format.simple_parse(adopt_data) + except ValueError as exc: + raise ValueError(_('Invalid adopt data: %s') % exc) kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data return kwargs diff --git a/heat/tests/test_api_openstack_v1.py b/heat/tests/test_api_openstack_v1.py index 0352a8f149..23647ec573 100644 --- a/heat/tests/test_api_openstack_v1.py +++ b/heat/tests/test_api_openstack_v1.py @@ -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): diff --git a/heat/tests/test_engine_api_utils.py b/heat/tests/test_engine_api_utils.py index 0f69ccfc60..cc340597c7 100644 --- a/heat/tests/test_engine_api_utils.py +++ b/heat/tests/test_engine_api_utils.py @@ -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({})