]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
RPC API: Add a ResourceNotFound exception
authorZane Bitter <zbitter@redhat.com>
Thu, 17 Jan 2013 10:10:15 +0000 (11:10 +0100)
committerZane Bitter <zbitter@redhat.com>
Thu, 17 Jan 2013 10:47:47 +0000 (11:47 +0100)
Change-Id: If55cd6ca0eab8be9d192e76430aedf1d5a1767c3
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/api/aws/exception.py
heat/api/openstack/v1/util.py
heat/common/exception.py
heat/engine/service.py
heat/tests/test_api_cfn_v1.py
heat/tests/test_api_openstack_v1.py
heat/tests/test_engine_service.py

index 1cfc1c89a5770734b44699380ee6cd84018805ce..5ccfa2396263f6d245b759b5e5c0f725d52b323b 100644 (file)
@@ -248,6 +248,7 @@ def map_remote_error(ex):
             'ValueError',
             'InvalidTenant',
             'StackNotFound',
+            'ResourceNotFound',
             'StackExists',
         )
 
index c231b1940bb2ddeb3c5f431ff64618f49adaa838..1cd4b7ada7d2c9c3485135b726455e8d3e6c6d8c 100644 (file)
@@ -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,
     }
index 8a170399feeeba702d060a49acb7cb1694facc9f..9136e529405f21f9f953bcbec879bb4227045125 100644 (file)
@@ -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.")
index 2ed405bb2c302acd06e0a48525b0096faaa128ea..6ed9371eb3f755e23ca7e5c668b78366224cf9ed 100644 (file)
@@ -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)
index 44b85f43b6870a2e1af5debff130f7de2a3483d0..57576cbf90b08143477a43774120fc0af1998ce8 100644 (file)
@@ -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"
index e9b19fd8dce2303d774f50aa77660438fe1306ce..eae72227c4c820b0dff891751b235daf88509b39 100644 (file)
@@ -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')
index d4cd759a7815d887e53c8b96ee0023983dec18a6..ab4298f5ad68a9eeb1b2ddeede6a573a5d022f94 100644 (file)
@@ -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)