From 1915acc547d18563df190c1cbd91e3ffa9d6608f Mon Sep 17 00:00:00 2001 From: liangke Date: Thu, 21 Sep 2017 17:14:08 +0800 Subject: Fix registry bug Change-Id: Ia6ca2240cfedd3e49cbf6fb5a44c1b78667b9ec9 Issue-Id: MULTICLOUD-102 Signed-off-by: liangke --- vio/vio/swagger/views/registry/views.py | 48 +++++++++++++++------------------ vio/vio/tests/tests_registry_view.py | 22 ++++++--------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/vio/vio/swagger/views/registry/views.py b/vio/vio/swagger/views/registry/views.py index 7969ad1..db616f9 100644 --- a/vio/vio/swagger/views/registry/views.py +++ b/vio/vio/swagger/views/registry/views.py @@ -36,11 +36,9 @@ class Registry(APIView): try: projects = tenant_instance.get_projects(auth_info) except Exception as e: - if hasattr(e, "http_status"): - return Response(data={'error': str(e)}, status=e.http_status) - else: - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + logger.exception("get tenants error %(e)s", {"e": e}) + raise e + rsp = {"tenants": []} for project in projects: rsp['tenants'].append(project.to_dict()) @@ -51,11 +49,9 @@ class Registry(APIView): try: images = image_instance.get_vim_images() except Exception as e: - if hasattr(e, "http_status"): - return Response(data={'error': str(e)}, status=e.http_status) - else: - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + logger.exception("get images error %(e)s", {"e": e}) + raise e + rsp = {"images": []} for image in images: rsp['images'].append(image.to_dict()) @@ -67,11 +63,9 @@ class Registry(APIView): flavors = flavors_op.list_flavors( auth_info, auth_info['tenant']) except Exception as e: - if hasattr(e, "http_status"): - return Response(data={'error': str(e)}, status=e.http_status) - else: - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + logger.exception("get flavors error %(e)s", {"e": e}) + raise e + rsp = {"flavors": []} for flavor in flavors: rsp['flavors'].append(flavor[0].to_dict()) @@ -83,11 +77,9 @@ class Registry(APIView): resp = net_op.list_networks( auth_info['vimId'], auth_info['tenant']) except Exception as e: - if hasattr(e, "http_status"): - return Response(data={'error': str(e)}, status=e.http_status) - else: - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + logger.exception("get networks error %(e)s", {"e": e}) + raise e + rsp = {'networks': resp['networks']} return rsp @@ -96,11 +88,9 @@ class Registry(APIView): try: hypervisors = hypervisor_op.list_hypervisors(auth_info) except Exception as e: - if hasattr(e, "http_status"): - return Response(data={'error': str(e)}, status=e.http_status) - else: - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + logger.exception("get hypervisors error %(e)s", {"e": e}) + raise e + rsp = {"hypervisors": []} for hypervisor in hypervisors: rsp['hypervisors'].append(hypervisor.to_dict()) @@ -157,8 +147,12 @@ class Registry(APIView): aai_adapter = AAIClient(cloud_owner, cloud_region) aai_adapter.update_vim(rsp) except Exception as e: - return Response(data=e.message, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response(data="", status=status.HTTP_200_OK) diff --git a/vio/vio/tests/tests_registry_view.py b/vio/vio/tests/tests_registry_view.py index 31c2d78..363513b 100644 --- a/vio/vio/tests/tests_registry_view.py +++ b/vio/vio/tests/tests_registry_view.py @@ -61,12 +61,9 @@ class RegistryViewTest(unittest.TestCase): @mock.patch.object(extsys, 'get_vim_by_id') def test_reg_get_tenants_view_fail(self, mock_vim_info, mock_projects): mock_vim_info.return_value = VIM_INFO - - mock_projects.side_effect = TypeError("wrong type") - - self.assertEqual( - status.HTTP_500_INTERNAL_SERVER_ERROR, - self.reg._get_tenants("vimid").status_code) + mock_projects.side_effect = Exception("something wrong") + self.assertRaisesRegexp(Exception, "something .*", + self.reg._get_tenants, "viminfo") @mock.patch.object(OperateImage, 'get_vim_images') @mock.patch.object(extsys, 'get_vim_by_id') @@ -122,10 +119,8 @@ class RegistryViewTest(unittest.TestCase): @mock.patch.object(extsys, 'get_vim_by_id') def test_reg_get_flavors_view_fail2(self, mock_vim_info, mock_flavors): mock_vim_info.return_value = VIM_INFO - mock_flavors.side_effect = TypeError("wrong type") - self.assertEqual( - status.HTTP_500_INTERNAL_SERVER_ERROR, - self.reg._get_flavors("viminfo").status_code) + mock_flavors.side_effect = Exception("something wrong") + self.assertRaises(Exception, self.reg._get_flavors) @mock.patch.object(OperateHypervisor, 'list_hypervisors') @mock.patch.object(extsys, 'get_vim_by_id') @@ -161,10 +156,9 @@ class RegistryViewTest(unittest.TestCase): def test_reg_get_hypervisors_view_fail(self, mock_vim_info, mock_hypervisor): mock_vim_info.return_value = VIM_INFO - mock_hypervisor.side_effect = TypeError("wrong type") - self.assertEqual( - status.HTTP_500_INTERNAL_SERVER_ERROR, - self.reg._get_hypervisors("viminfo").status_code) + mock_hypervisor.side_effect = Exception("something wrong") + self.assertRaisesRegexp(Exception, "something .*", + self.reg._get_hypervisors, "viminfo") def test_reg_find_tenant(self): tenants = {"tenants": [ -- cgit 1.2.3-korg