diff options
author | fujinhua <fu.jinhua@zte.com.cn> | 2019-04-05 10:58:51 +0800 |
---|---|---|
committer | fujinhua <fu.jinhua@zte.com.cn> | 2019-04-05 11:40:44 +0800 |
commit | d78eee5a04a0a544169adaddcad5585accc31260 (patch) | |
tree | 17c3e56d1733c1fff51a8840ee76d8e9cd65aad0 /lcm | |
parent | f443b5233b6b5df67a35563a7600ee52b56edbfb (diff) |
Refactor codes for curd vnf
Change-Id: Ib6b81b438c3217d533fb558d573a98ee2779ae98
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
Diffstat (limited to 'lcm')
-rw-r--r-- | lcm/lcm/nf/tests/test_create_vnf.py | 14 | ||||
-rw-r--r-- | lcm/lcm/nf/tests/test_query_vnf.py | 15 | ||||
-rw-r--r-- | lcm/lcm/nf/views/curd_vnf_views.py | 153 |
3 files changed, 79 insertions, 103 deletions
diff --git a/lcm/lcm/nf/tests/test_create_vnf.py b/lcm/lcm/nf/tests/test_create_vnf.py index a672827e..4ded31ce 100644 --- a/lcm/lcm/nf/tests/test_create_vnf.py +++ b/lcm/lcm/nf/tests/test_create_vnf.py @@ -62,7 +62,10 @@ class TestNFInstantiate(TestCase): response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json') self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) context = json.loads(response.content) - self.assertEqual({'error': 'VNF is already exist.'}, context) + self.assertEqual({ + 'detail': 'VNF is already exist.', + 'status': 500 + }, context) @mock.patch.object(restcall, 'call_req') @mock.patch.object(uuid, 'uuid4') @@ -76,6 +79,11 @@ class TestNFInstantiate(TestCase): "vnfInstanceDescription": "vFW in Nanjing TIC Edge" } response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json') - expect_data = {"id": "1", "vnfProvider": "huawei", "vnfdVersion": "1.0", "vnfPkgId": "111", - "instantiationState": "NOT_INSTANTIATED"} + expect_data = { + "id": "1", + "vnfProvider": "huawei", + "vnfdVersion": "1.0", + "vnfPkgId": "111", + "instantiationState": "NOT_INSTANTIATED" + } self.assertEqual(expect_data, response.data) diff --git a/lcm/lcm/nf/tests/test_query_vnf.py b/lcm/lcm/nf/tests/test_query_vnf.py index 66673f6b..e041216c 100644 --- a/lcm/lcm/nf/tests/test_query_vnf.py +++ b/lcm/lcm/nf/tests/test_query_vnf.py @@ -147,7 +147,10 @@ class ResourceTest(TestCase): def test_get_vnf_not_exist(self): response = self.client.get("/api/vnflcm/v1/vnf_instances/x", format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) - self.assertEqual({'error': 'VnfInst(x) does not exist.'}, response.data) + self.assertEqual({ + 'detail': 'VnfInst(x) does not exist.', + 'status': 500 + }, response.data) def test_get_vnf_net_not_exist(self): vnf_inst_id = "1" @@ -165,7 +168,10 @@ class ResourceTest(TestCase): ownertype='0').save() response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) - self.assertEqual({'error': 'NetworkInst(x) does not exist.'}, response.data) + self.assertEqual({ + 'detail': 'NetworkInst(x) does not exist.', + 'status': 500 + }, response.data) def test_get_vnf_vm_not_exist(self): vnf_inst_id = "1" @@ -182,7 +188,10 @@ class ResourceTest(TestCase): vmid='x').save() response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) - self.assertEqual({'error': 'VmInst(x) does not exist.'}, response.data) + self.assertEqual({ + 'detail': 'VmInst(x) does not exist.', + 'status': 500 + }, response.data) def test_get_vnf_storage_not_exist(self): vnf_inst_id = "1" diff --git a/lcm/lcm/nf/views/curd_vnf_views.py b/lcm/lcm/nf/views/curd_vnf_views.py index abd6c62c..64fe0029 100644 --- a/lcm/lcm/nf/views/curd_vnf_views.py +++ b/lcm/lcm/nf/views/curd_vnf_views.py @@ -13,7 +13,6 @@ # limitations under the License. import logging -import traceback from drf_yasg.utils import swagger_auto_schema from lcm.nf.biz.delete_vnf import DeleteVnf @@ -33,6 +32,7 @@ from lcm.pub.exceptions import NFLCMException from lcm.pub.exceptions import NFLCMExceptionNotFound from lcm.pub.database.models import NfInstModel from lcm.nf.const import VNF_STATUS +from .common import view_safe_call_with_log logger = logging.getLogger(__name__) @@ -44,24 +44,17 @@ class CreateVnfAndQueryVnfs(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) + @view_safe_call_with_log(logger=logger) def get(self, request): logger.debug("QueryMultiVnf--get::> %s" % request.data) - try: - resp_data = QueryVnf(request.data).query_multi_vnf() - if len(resp_data) == 0: - return Response(data=[], status=status.HTTP_200_OK) - vnf_instances_serializer = VnfInstancesSerializer(data=resp_data) - if not vnf_instances_serializer.is_valid(): - raise NFLCMException(vnf_instances_serializer.errors) - - return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK) - except NFLCMException as e: - logger.error(e.message) - return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + resp_data = QueryVnf(request.data).query_multi_vnf() + if len(resp_data) == 0: + return Response(data=[], status=status.HTTP_200_OK) + vnf_instances_serializer = VnfInstancesSerializer(data=resp_data) + if not vnf_instances_serializer.is_valid(): + raise NFLCMException(vnf_instances_serializer.errors) + + return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK) @swagger_auto_schema( request_body=CreateVnfReqSerializer(), @@ -70,29 +63,23 @@ class CreateVnfAndQueryVnfs(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) + @view_safe_call_with_log(logger=logger) def post(self, request): logger.debug("CreateVnfIdentifier--post::> %s" % request.data) - try: - req_serializer = CreateVnfReqSerializer(data=request.data) - if not req_serializer.is_valid(): - raise NFLCMException(req_serializer.errors) - - nf_inst = CreateVnf(request.data).do_biz() - create_vnf_resp_serializer = VnfInstanceSerializer(data={"id": nf_inst.nfinstid, - "vnfProvider": nf_inst.vendor, - "vnfdVersion": nf_inst.version, - "vnfPkgId": nf_inst.package_id, - "instantiationState": nf_inst.status}) - if not create_vnf_resp_serializer.is_valid(): - raise NFLCMException(create_vnf_resp_serializer.errors) - return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED) - except NFLCMException as e: - logger.error(e.message) - return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + req_serializer = CreateVnfReqSerializer(data=request.data) + if not req_serializer.is_valid(): + raise NFLCMException(req_serializer.errors) + + nf_inst = CreateVnf(request.data).do_biz() + create_vnf_resp_serializer = VnfInstanceSerializer( + data={"id": nf_inst.nfinstid, + "vnfProvider": nf_inst.vendor, + "vnfdVersion": nf_inst.version, + "vnfPkgId": nf_inst.package_id, + "instantiationState": nf_inst.status}) + if not create_vnf_resp_serializer.is_valid(): + raise NFLCMException(create_vnf_resp_serializer.errors) + return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED) class DeleteVnfAndQueryVnf(APIView): @@ -102,26 +89,16 @@ class DeleteVnfAndQueryVnf(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) + @view_safe_call_with_log(logger=logger) def get(self, request, instanceid): logger.debug("QuerySingleVnf--get::> %s" % request.data) - try: - resp_data = QueryVnf(request.data, instanceid).query_single_vnf() - - vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data) - if not vnfs_instance_serializer.is_valid(): - raise NFLCMException(vnfs_instance_serializer.errors) - - return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK) - except NFLCMException as e: - logger.error(e.message) - return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except NFLCMExceptionNotFound as e: - return Response(data={'error': '%s' % e.message}, status=status.HTTP_404_NOT_FOUND) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + resp_data = QueryVnf(request.data, instanceid).query_single_vnf() + + vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data) + if not vnfs_instance_serializer.is_valid(): + raise NFLCMException(vnfs_instance_serializer.errors) + + return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK) @swagger_auto_schema( responses={ @@ -129,21 +106,13 @@ class DeleteVnfAndQueryVnf(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) + @view_safe_call_with_log(logger=logger) def delete(self, request, instanceid): logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data) - try: - DeleteVnf(request.data, instanceid).do_biz() - - return Response(data=None, status=status.HTTP_204_NO_CONTENT) - except NFLCMException as e: - logger.error(e.message) - logger.debug('Delete VNF instance[%s] failed' % instanceid) - return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - logger.debug('Delete VNF instance[%s] failed' % instanceid) - return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + DeleteVnf(request.data, instanceid).do_biz() + + return Response(data=None, status=status.HTTP_204_NO_CONTENT) @swagger_auto_schema( request_body=VnfInfoModificationsSerializer(), @@ -152,33 +121,23 @@ class DeleteVnfAndQueryVnf(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) + @view_safe_call_with_log(logger=logger) def patch(self, request, instanceid): logger.debug("UpdateSingleVnf--patch::> %s, %s", instanceid, request.data) - try: - upd_vnf_serializer = VnfInfoModificationsSerializer(data=request.data) - if not upd_vnf_serializer.is_valid(): - raise NFLCMException(upd_vnf_serializer.errors) - - job_id = JobUtil.create_job('NF', 'UPDATE', instanceid) - JobUtil.add_job_status(job_id, 0, "UPDATE_VNF_READY") - - vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid) - if not vnf_insts.exists(): - raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instanceid) - vnf_insts.update(status=VNF_STATUS.UPDATING) - - JobUtil.add_job_status(job_id, 15, 'Nf updating pre-check finish') - UpdateVnf(request.data, instanceid, job_id).start() - - return Response(data=None, status=status.HTTP_202_ACCEPTED) - except NFLCMException as e: - logger.error(e.message) - logger.error('Update VNF instance[%s] failed' % instanceid) - return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except NFLCMExceptionNotFound as e: - return Response(data={'error': '%s' % e.message}, status=status.HTTP_404_NOT_FOUND) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - logger.error('Update VNF instance[%s] failed' % instanceid) - return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + upd_vnf_serializer = VnfInfoModificationsSerializer(data=request.data) + if not upd_vnf_serializer.is_valid(): + raise NFLCMException(upd_vnf_serializer.errors) + + job_id = JobUtil.create_job('NF', 'UPDATE', instanceid) + JobUtil.add_job_status(job_id, 0, "UPDATE_VNF_READY") + + vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid) + if not vnf_insts.exists(): + raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instanceid) + vnf_insts.update(status=VNF_STATUS.UPDATING) + + JobUtil.add_job_status(job_id, 15, 'Nf updating pre-check finish') + UpdateVnf(request.data, instanceid, job_id).start() + + return Response(data=None, status=status.HTTP_202_ACCEPTED) |