From 2af0625d62a720b7a46834f32266988c92872044 Mon Sep 17 00:00:00 2001 From: Dongfeng Huang Date: Sun, 19 Feb 2017 11:46:43 +0800 Subject: [PATCH] Catch an exception on resource routing update 1. What is the problem When resource routing needs to update its attribute named pod_id, it will fail if the pod where the new pod_id lays on doesn't exist. This update error was caught through internal server error originally and it was not detailed enough. 2. What is the solution for the problem Dismiss this update request by giving a regular invalid parameter error and tell the operator that the new pod for resource routing should exist. The pod to which the pod_id belongs can only be selected from the existing pods. Because it is a foreign key and highly depends on pod table. At the same time, we should add one test case for this circumstance. 3. What the features need to be implemented to the Tricircle to realize the solution None. Change-Id: Iae6e3368c7f0ff0cd751aca932cba818328bb723 --- tricircle/api/controllers/routing.py | 16 ++++++++++++++++ .../tests/unit/api/controllers/test_routing.py | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/tricircle/api/controllers/routing.py b/tricircle/api/controllers/routing.py index 4e72adca..bfe07b5e 100644 --- a/tricircle/api/controllers/routing.py +++ b/tricircle/api/controllers/routing.py @@ -197,6 +197,22 @@ class RoutingController(rest.RestController): return utils.format_api_error( 400, _('There is no such resource type')) + # the pod with new pod_id should exist in pod table + if 'pod_id' in update_dict: + new_pod_id = update_dict.get('pod_id') + try: + # find the pod through the pod_id and verify whether it exists + db_api.get_pod(context, new_pod_id) + except t_exc.ResourceNotFound: + return utils.format_api_error( + 400, _("The pod %(new_pod_id)s doesn't" + " exist") % {'new_pod_id': new_pod_id}) + except Exception as e: + LOG.exception(_LE('Failed to update resource routing: ' + '%(exception)s '), {'exception': e}) + return utils.format_api_error( + 500, _('Failed to update resource routing')) + try: routing_updated = db_api.update_resource_routing( context, _id, update_dict) diff --git a/tricircle/tests/unit/api/controllers/test_routing.py b/tricircle/tests/unit/api/controllers/test_routing.py index a559ca22..dfb4d998 100644 --- a/tricircle/tests/unit/api/controllers/test_routing.py +++ b/tricircle/tests/unit/api/controllers/test_routing.py @@ -391,5 +391,14 @@ class RoutingControllerTest(unittest.TestCase): res = self.controller.put(-123, **body_update1) self._validate_error_code(res, 404) + # failure case, the pod where the new pod_id lays on + # should exist in pod table + + # a variable used for later test + new_pod_id = uuidutils.generate_uuid() + body_update6 = {'routing': {'pod_id': new_pod_id}} + res = self.controller.put(id, **body_update6) + self._validate_error_code(res, 400) + def tearDown(self): core.ModelBase.metadata.drop_all(core.get_engine())