From: Zane Bitter Date: Thu, 17 Jan 2013 10:10:15 +0000 (+0100) Subject: RPC API: Add a ResourceNotFound exception X-Git-Tag: 2014.1~998 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=9c95aec814ed7ab40d6919e8312a7514c8a9c2c9;p=openstack-build%2Fheat-build.git RPC API: Add a ResourceNotFound exception Change-Id: If55cd6ca0eab8be9d192e76430aedf1d5a1767c3 Signed-off-by: Zane Bitter --- diff --git a/heat/api/aws/exception.py b/heat/api/aws/exception.py index 1cfc1c89..5ccfa239 100644 --- a/heat/api/aws/exception.py +++ b/heat/api/aws/exception.py @@ -248,6 +248,7 @@ def map_remote_error(ex): 'ValueError', 'InvalidTenant', 'StackNotFound', + 'ResourceNotFound', 'StackExists', ) diff --git a/heat/api/openstack/v1/util.py b/heat/api/openstack/v1/util.py index c231b194..1cd4b7ad 100644 --- a/heat/api/openstack/v1/util.py +++ b/heat/api/openstack/v1/util.py @@ -92,6 +92,7 @@ def remote_error(ex, force_exists=False): 'AttributeError': client_error, 'ValueError': client_error, 'StackNotFound': exc.HTTPNotFound, + 'ResourceNotFound': exc.HTTPNotFound, 'InvalidTenant': exc.HTTPForbidden, 'StackExists': exc.HTTPConflict, } diff --git a/heat/common/exception.py b/heat/common/exception.py index 8a170399..9136e529 100644 --- a/heat/common/exception.py +++ b/heat/common/exception.py @@ -215,3 +215,8 @@ class StackNotFound(OpenstackException): class StackExists(OpenstackException): message = _("The Stack (%(stack_name)s) already exists.") + + +class ResourceNotFound(OpenstackException): + message = _("The Resource (%(resource_name)s) could not be found " + "in Stack %(stack_name)s.") diff --git a/heat/engine/service.py b/heat/engine/service.py index 2ed405bb..6ed9371e 100644 --- a/heat/engine/service.py +++ b/heat/engine/service.py @@ -376,7 +376,8 @@ class EngineService(service.Service): stack = parser.Stack.load(context, stack=s) if resource_name not in stack: - raise AttributeError('Unknown resource name') + raise exception.ResourceNotFound(resource_name=resource_name, + stack_name=stack.name) resource = stack[resource_name] if resource.id is None: @@ -437,7 +438,8 @@ class EngineService(service.Service): stack = parser.Stack.load(context, stack=s) if resource_name not in stack: - raise AttributeError("Resource not found %s" % resource_name) + raise exception.ResourceNotFound(resource_name=resource_name, + stack_name=stack.name) resource = stack[resource_name] resource.metadata_update(metadata) diff --git a/heat/tests/test_api_cfn_v1.py b/heat/tests/test_api_cfn_v1.py index 44b85f43..57576cbf 100644 --- a/heat/tests/test_api_cfn_v1.py +++ b/heat/tests/test_api_cfn_v1.py @@ -1024,6 +1024,37 @@ class StackControllerTest(unittest.TestCase): self.assertEqual(type(result), exception.HeatInvalidParameterValueError) + def test_describe_stack_resource_nonexistent(self): + # Format a dummy request + stack_name = "wordpress" + identity = dict(identifier.HeatIdentifier('t', stack_name, '6')) + params = {'Action': 'DescribeStackResource', + 'StackName': stack_name, + 'LogicalResourceId': "wibble"} + dummy_req = self._dummy_GET_request(params) + + # Stub out the RPC call to the engine with a pre-canned response + self.m.StubOutWithMock(rpc, 'call') + rpc.call(dummy_req.context, self.topic, + {'method': 'identify_stack', + 'args': {'stack_name': stack_name}, + 'version': self.api_version}, None).AndReturn(identity) + args = { + 'stack_identity': identity, + 'resource_name': dummy_req.params.get('LogicalResourceId'), + } + rpc.call(dummy_req.context, self.topic, + {'method': 'describe_stack_resource', + 'args': args, + 'version': self.api_version}, + None).AndRaise(rpc_common.RemoteError("ResourceNotFound")) + + self.m.ReplayAll() + + result = self.controller.describe_stack_resource(dummy_req) + self.assertEqual(type(result), + exception.HeatInvalidParameterValueError) + def test_describe_stack_resources(self): # Format a dummy request stack_name = "wordpress" diff --git a/heat/tests/test_api_openstack_v1.py b/heat/tests/test_api_openstack_v1.py index e9b19fd8..eae72227 100644 --- a/heat/tests/test_api_openstack_v1.py +++ b/heat/tests/test_api_openstack_v1.py @@ -1047,6 +1047,32 @@ class ResourceControllerTest(ControllerTest, unittest.TestCase): resource_name=res_name) self.m.VerifyAll() + def test_show_nonexist_resource(self): + res_name = 'Wibble' + stack_identity = identifier.HeatIdentifier(self.tenant, + 'wordpress', '1') + res_identity = identifier.ResourceIdentifier(resource_name=res_name, + **stack_identity) + + req = self._get(res_identity._tenant_path()) + + self.m.StubOutWithMock(rpc, 'call') + rpc.call(req.context, self.topic, + {'method': 'describe_stack_resource', + 'args': {'stack_identity': stack_identity, + 'resource_name': res_name}, + 'version': self.api_version}, + None).AndRaise(rpc_common.RemoteError("ResourceNotFound")) + self.m.ReplayAll() + + self.assertRaises(webob.exc.HTTPNotFound, + self.controller.show, + req, tenant_id=self.tenant, + stack_name=stack_identity.stack_name, + stack_id=stack_identity.stack_id, + resource_name=res_name) + self.m.VerifyAll() + def test_metadata_show(self): res_name = 'WikiDatabase' stack_identity = identifier.HeatIdentifier(self.tenant, @@ -1115,6 +1141,32 @@ class ResourceControllerTest(ControllerTest, unittest.TestCase): resource_name=res_name) self.m.VerifyAll() + def test_metadata_show_nonexist_resource(self): + res_name = 'wibble' + stack_identity = identifier.HeatIdentifier(self.tenant, + 'wordpress', '1') + res_identity = identifier.ResourceIdentifier(resource_name=res_name, + **stack_identity) + + req = self._get(res_identity._tenant_path() + '/metadata') + + self.m.StubOutWithMock(rpc, 'call') + rpc.call(req.context, self.topic, + {'method': 'describe_stack_resource', + 'args': {'stack_identity': stack_identity, + 'resource_name': res_name}, + 'version': self.api_version}, + None).AndRaise(rpc_common.RemoteError("ResourceNotFound")) + self.m.ReplayAll() + + self.assertRaises(webob.exc.HTTPNotFound, + self.controller.metadata, + req, tenant_id=self.tenant, + stack_name=stack_identity.stack_name, + stack_id=stack_identity.stack_id, + resource_name=res_name) + self.m.VerifyAll() + @attr(tag=['unit', 'api-openstack-v1', 'EventController']) @attr(speed='fast') diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index d4cd759a..ab4298f5 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -580,7 +580,7 @@ class stackServiceTest(unittest.TestCase): self.ctx, nonexist, 'WebServer') def test_stack_resource_describe_nonexist_resource(self): - self.assertRaises(AttributeError, + self.assertRaises(exception.ResourceNotFound, self.man.describe_stack_resource, self.ctx, self.stack_identity, 'foo') @@ -687,7 +687,7 @@ class stackServiceTest(unittest.TestCase): def test_metadata_err_resource(self): test_metadata = {'foo': 'bar', 'baz': 'quux', 'blarg': 'wibble'} - self.assertRaises(AttributeError, + self.assertRaises(exception.ResourceNotFound, self.man.metadata_update, self.ctx, dict(self.stack_identity), 'NooServer', test_metadata)