diff --git a/heat/api/middleware/fault.py b/heat/api/middleware/fault.py index 1821a25ad6..774d3f1b35 100644 --- a/heat/api/middleware/fault.py +++ b/heat/api/middleware/fault.py @@ -61,6 +61,7 @@ class FaultWrapper(wsgi.Middleware): 'ResourceActionNotSupported': webob.exc.HTTPBadRequest, 'ResourceNotFound': webob.exc.HTTPNotFound, 'ResourceTypeNotFound': webob.exc.HTTPNotFound, + 'InvalidGlobalResource': webob.exc.HTTPInternalServerError, 'SnapshotNotFound': webob.exc.HTTPNotFound, 'ResourceNotAvailable': webob.exc.HTTPNotFound, 'PhysicalResourceNotFound': webob.exc.HTTPNotFound, diff --git a/heat/common/exception.py b/heat/common/exception.py index eb3fc2bad0..118eb8d103 100644 --- a/heat/common/exception.py +++ b/heat/common/exception.py @@ -264,16 +264,13 @@ class SnapshotNotFound(HeatException): "could not be found.") -class TemplateNotFound(HeatException): - msg_fmt = _("%(message)s") - - class ResourceTypeNotFound(HeatException): msg_fmt = _("The Resource Type (%(type_name)s) could not be found.") -class InvalidResourceType(HeatException): - msg_fmt = _("%(message)s") +class InvalidGlobalResource(HeatException): + msg_fmt = _("There was an error loading the definition of the global " + "resource type %(type_name)s.") class InvalidBreakPointHook(HeatException): diff --git a/heat/engine/environment.py b/heat/engine/environment.py index 4b450bf5e0..0fd51b3130 100644 --- a/heat/engine/environment.py +++ b/heat/engine/environment.py @@ -420,19 +420,20 @@ class ResourceRegistry(object): def get_class(self, resource_type, resource_name=None, files=None): if resource_type == "": msg = _('Resource "%s" has no type') % resource_name - raise exception.InvalidResourceType(message=msg) + raise exception.StackValidationFailed(message=msg) elif resource_type is None: msg = _('Non-empty resource type is required ' 'for resource "%s"') % resource_name - raise exception.InvalidResourceType(message=msg) + raise exception.StackValidationFailed(message=msg) elif not isinstance(resource_type, six.string_types): msg = _('Resource "%s" type is not a string') % resource_name - raise exception.InvalidResourceType(message=msg) + raise exception.StackValidationFailed(message=msg) info = self.get_resource_info(resource_type, resource_name=resource_name) if info is None: - raise exception.ResourceTypeNotFound(type_name=resource_type) + msg = _("Unknown resource Type : %s") % resource_type + raise exception.StackValidationFailed(message=msg) return info.get_class(files=files) def as_dict(self): diff --git a/heat/engine/resource.py b/heat/engine/resource.py index 2388e24e09..cc401f76f8 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -135,7 +135,7 @@ class Resource(object): ResourceClass = registry.get_class(definition.resource_type, resource_name=name, files=stack.t.files) - except exception.TemplateNotFound: + except exception.NotFound: ResourceClass = template_resource.TemplateResource assert issubclass(ResourceClass, Resource) diff --git a/heat/engine/resources/openstack/heat/resource_group.py b/heat/engine/resources/openstack/heat/resource_group.py index 7d2fc328b0..c0c5555461 100644 --- a/heat/engine/resources/openstack/heat/resource_group.py +++ b/heat/engine/resources/openstack/heat/resource_group.py @@ -284,7 +284,7 @@ class ResourceGroup(stack_resource.StackResource): # make sure we can resolve the nested resource type try: self.stack.env.get_class(res_def.resource_type) - except exception.TemplateNotFound: + except exception.NotFound: # its a template resource pass diff --git a/heat/engine/resources/template_resource.py b/heat/engine/resources/template_resource.py index 027e76e9b1..fddb81390b 100644 --- a/heat/engine/resources/template_resource.py +++ b/heat/engine/resources/template_resource.py @@ -99,7 +99,7 @@ class TemplateResource(stack_resource.StackResource): args = {'name': template_name, 'exc': six.text_type(r_exc)} msg = _('Could not fetch remote template ' '"%(name)s": %(exc)s') % args - raise exception.TemplateNotFound(message=msg) + raise exception.NotFound(msg_fmt=msg) @staticmethod def get_schemas(tmpl, param_defaults): @@ -112,7 +112,7 @@ class TemplateResource(stack_resource.StackResource): self._parsed_nested = None try: tmpl = template.Template(self.child_template()) - except (exception.TemplateNotFound, ValueError) as download_error: + except (exception.NotFound, ValueError) as download_error: self.validation_exception = download_error tmpl = template.Template( {"HeatTemplateFormatVersion": "2012-12-12"}) @@ -199,7 +199,7 @@ class TemplateResource(stack_resource.StackResource): try: t_data = self.get_template_file(self.template_name, self.allowed_schemes) - except exception.TemplateNotFound as err: + except exception.NotFound as err: if self.action == self.UPDATE: raise reported_excp = err diff --git a/heat/engine/service.py b/heat/engine/service.py index 4fedc8e082..de2697712e 100644 --- a/heat/engine/service.py +++ b/heat/engine/service.py @@ -1206,10 +1206,13 @@ class EngineService(service.Service): self.resource_enforcer.enforce(cnxt, type_name) try: resource_class = resources.global_env().get_class(type_name) - except (exception.InvalidResourceType, - exception.ResourceTypeNotFound, - exception.TemplateNotFound) as ex: - raise ex + except exception.StackValidationFailed: + raise exception.ResourceTypeNotFound(type_name=type_name) + except exception.NotFound: + LOG.exception(_LE('Error loading resource type %s ' + 'from global environment.'), + type_name) + raise exception.InvalidGlobalResource(type_name=type_name) if resource_class.support_status.status == support.HIDDEN: raise exception.NotSupported(type_name) @@ -1255,10 +1258,13 @@ class EngineService(service.Service): raise exception.NotSupported(type_name) return resource_class.resource_to_template(type_name, template_type) - except (exception.InvalidResourceType, - exception.ResourceTypeNotFound, - exception.TemplateNotFound) as ex: - raise ex + except exception.StackValidationFailed: + raise exception.ResourceTypeNotFound(type_name=type_name) + except exception.NotFound: + LOG.exception(_LE('Error loading resource type %s ' + 'from global environment.'), + type_name) + raise exception.InvalidGlobalResource(type_name=type_name) @context.request_context def list_events(self, cnxt, stack_identity, filters=None, limit=None, diff --git a/heat/tests/api/openstack_v1/test_stacks.py b/heat/tests/api/openstack_v1/test_stacks.py index b8b813cfe6..0d5205e4ca 100644 --- a/heat/tests/api/openstack_v1/test_stacks.py +++ b/heat/tests/api/openstack_v1/test_stacks.py @@ -2308,6 +2308,27 @@ class StackControllerTest(tools.ControllerTest, common.HeatTestCase): self.assertEqual('ResourceTypeNotFound', resp.json['error']['type']) self.m.VerifyAll() + def test_resource_schema_faulty_template(self, mock_enforce): + self._mock_enforce_setup(mock_enforce, 'resource_schema', True) + req = self._get('/resource_types/FaultyTemplate') + type_name = 'FaultyTemplate' + + error = heat_exc.InvalidGlobalResource(type_name='FaultyTemplate') + self.m.StubOutWithMock(rpc_client.EngineClient, 'call') + rpc_client.EngineClient.call( + req.context, + ('resource_schema', {'type_name': type_name}) + ).AndRaise(tools.to_remote_error(error)) + self.m.ReplayAll() + + resp = tools.request_with_middleware(fault.FaultWrapper, + self.controller.resource_schema, + req, tenant_id=self.tenant, + type_name=type_name) + self.assertEqual(500, resp.json['code']) + self.assertEqual('InvalidGlobalResource', resp.json['error']['type']) + self.m.VerifyAll() + def test_resource_schema_err_denied_policy(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'resource_schema', False) req = self._get('/resource_types/BogusResourceType') diff --git a/heat/tests/engine/test_resource_type.py b/heat/tests/engine/test_resource_type.py index bcd6102785..d4fb4f3815 100644 --- a/heat/tests/engine/test_resource_type.py +++ b/heat/tests/engine/test_resource_type.py @@ -134,11 +134,12 @@ class ResourceTypeTest(common.HeatTestCase): mock_iterable = mock.MagicMock(return_value=iter([info])) with mock.patch('heat.engine.environment.ResourceRegistry.iterable_by', new=mock_iterable): - ex = self.assertRaises(exception.TemplateNotFound, + ex = self.assertRaises(exception.InvalidGlobalResource, function, self.ctx, type_name='ResourceWithWrongRefOnFile') - msg = 'Could not fetch remote template "not_existing.yaml"' + msg = ('There was an error loading the definition of the global ' + 'resource type ResourceWithWrongRefOnFile.') self.assertIn(msg, six.text_type(ex)) def test_resource_schema_no_template_file(self): diff --git a/heat/tests/test_provider_template.py b/heat/tests/test_provider_template.py index 72ccedc063..d84726c405 100644 --- a/heat/tests/test_provider_template.py +++ b/heat/tests/test_provider_template.py @@ -614,7 +614,7 @@ class ProviderTemplateTest(common.HeatTestCase): env_str = {'resource_registry': {'resources': {'fred': { "OS::ResourceType": "some_magic.yaml"}}}} env = environment.Environment(env_str) - ex = self.assertRaises(exception.TemplateNotFound, env.get_class, + ex = self.assertRaises(exception.NotFound, env.get_class, 'OS::ResourceType', 'fred') self.assertIn('Could not fetch remote template "some_magic.yaml"', six.text_type(ex)) @@ -934,16 +934,18 @@ class TemplateDataTest(common.HeatTestCase): self.res.action = self.res.UPDATE self.res.nested = mock.MagicMock() self.res.get_template_file = mock.Mock( - side_effect=exception.TemplateNotFound( - message='test_resource.template')) - self.assertRaises(exception.TemplateNotFound, self.res.template_data) + side_effect=exception.NotFound( + msg_fmt='Could not fetch remote template ' + '"test_resource.template": file not found')) + self.assertRaises(exception.NotFound, self.res.template_data) def test_template_data_in_create_without_template_file(self): self.res.action = self.res.CREATE self.res.nested = mock.MagicMock() self.res.get_template_file = mock.Mock( - side_effect=exception.TemplateNotFound( - message='test_resource.template')) + side_effect=exception.NotFound( + msg_fmt='Could not fetch remote template ' + '"test_resource.template": file not found')) self.assertEqual('{}', self.res.template_data()) diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index be00a3813b..cb70bac192 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -73,7 +73,7 @@ class ResourceTest(common.HeatTestCase): self.assertEqual(generic_rsrc.GenericResource, cls) def test_get_class_noexist(self): - self.assertRaises(exception.ResourceTypeNotFound, + self.assertRaises(exception.StackValidationFailed, resources.global_env().get_class, 'NoExistResourceType') @@ -173,13 +173,13 @@ class ResourceTest(common.HeatTestCase): def test_resource_new_err(self): snippet = rsrc_defn.ResourceDefinition('aresource', 'NoExistResourceType') - self.assertRaises(exception.ResourceTypeNotFound, + self.assertRaises(exception.StackValidationFailed, resource.Resource, 'aresource', snippet, self.stack) def test_resource_non_type(self): resource_name = 'aresource' snippet = rsrc_defn.ResourceDefinition(resource_name, '') - ex = self.assertRaises(exception.InvalidResourceType, + ex = self.assertRaises(exception.StackValidationFailed, resource.Resource, resource_name, snippet, self.stack) self.assertIn(_('Resource "%s" has no type') % resource_name, diff --git a/heat/tests/test_resource_group.py b/heat/tests/test_resource_group.py index 8cd1c3d7dc..e01fc78d56 100644 --- a/heat/tests/test_resource_group.py +++ b/heat/tests/test_resource_group.py @@ -416,10 +416,9 @@ class ResourceGroupTest(common.HeatTestCase): stack = utils.parse_stack(tmp) snip = stack.t.resource_definitions(stack)['group1'] resg = resource_group.ResourceGroup('test', snip, stack) - exc = self.assertRaises(exception.ResourceTypeNotFound, + exc = self.assertRaises(exception.StackValidationFailed, resg.validate) - exp_msg = 'The Resource Type (idontexist) could not be found.' - self.assertIn(exp_msg, six.text_type(exc)) + self.assertIn('Unknown resource Type', six.text_type(exc)) def test_reference_attr(self): stack = utils.parse_stack(template2) diff --git a/heat/tests/test_stack_resource.py b/heat/tests/test_stack_resource.py index 32f32a629d..179f22ee5c 100644 --- a/heat/tests/test_stack_resource.py +++ b/heat/tests/test_stack_resource.py @@ -377,28 +377,21 @@ class StackResourceTest(StackResourceBaseTest): self.assertEqual(raise_exc_msg, six.text_type(exc)) def _test_validate_unknown_resource_type(self, stack_name, tmpl, - resource_name, - stack_resource=True): - raise_exc_msg = 'The Resource Type (idontexist) could not be found.' + resource_name): + raise_exc_msg = ('Unknown resource Type : idontexist') stack = parser.Stack(utils.dummy_context(), stack_name, tmpl) rsrc = stack[resource_name] - if stack_resource: - exc = self.assertRaises(exception.StackValidationFailed, - rsrc.validate) - else: - exc = self.assertRaises(exception.ResourceTypeNotFound, - rsrc.validate) + exc = self.assertRaises(exception.StackValidationFailed, + rsrc.validate) self.assertIn(raise_exc_msg, six.text_type(exc)) def test_validate_resource_group(self): - # resource group validate without nested template is a normal - # resource validation + # test validate without nested template stack_name = 'validate_resource_group_template' t = template_format.parse(resource_group_template) tmpl = templatem.Template(t) self._test_validate_unknown_resource_type(stack_name, tmpl, - 'my_resource_group', - stack_resource=False) + 'my_resource_group') # validate with nested template res_prop = t['resources']['my_resource_group']['properties'] @@ -409,7 +402,7 @@ class StackResourceTest(StackResourceBaseTest): 'my_resource_group') def test_validate_heat_autoscaling_group(self): - # Autoscaling validation is a nested stack validation + # test validate without nested template stack_name = 'validate_heat_autoscaling_group_template' t = template_format.parse(heat_autoscaling_group_template) tmpl = templatem.Template(t) @@ -863,8 +856,8 @@ class WithTemplateTest(StackResourceBaseTest): class RaiseLocalException(StackResourceBaseTest): def test_heat_exception(self): - local = exception.InvalidResourceType(message='test') - self.assertRaises(exception.InvalidResourceType, + local = exception.StackValidationFailed(message='test') + self.assertRaises(exception.StackValidationFailed, self.parent_resource.raise_local_exception, local) def test_messaging_timeout(self): @@ -873,9 +866,9 @@ class RaiseLocalException(StackResourceBaseTest): self.parent_resource.raise_local_exception, local) def test_remote_heat_ex(self): - class InvalidResourceType_Remote(exception.InvalidResourceType): + class StackValidationFailed_Remote(exception.StackValidationFailed): pass - local = InvalidResourceType_Remote(message='test') + local = StackValidationFailed_Remote(message='test') self.assertRaises(exception.ResourceFailure, self.parent_resource.raise_local_exception, local)