Deal with nfPackage 69/63369/1
authorbiancunkang <bian.cunkang@zte.com.cn>
Wed, 29 Aug 2018 04:15:16 +0000 (12:15 +0800)
committerbiancunkang <bian.cunkang@zte.com.cn>
Wed, 29 Aug 2018 04:15:16 +0000 (12:15 +0800)
Add a exception test and add 404 status for query single

Change-Id: I1a9d735b894e1e9a375efa09fccf102d5d8c60ed
Issue-ID: VFC-1038
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
catalog/packages/biz/vnf_package.py
catalog/packages/tests/test_vnf_package.py
catalog/packages/views/vnf_package_views.py
catalog/pub/exceptions.py

index 7f3511d..661597d 100644 (file)
@@ -25,7 +25,7 @@ from rest_framework import status
 from django.http import StreamingHttpResponse
 from catalog.pub.config.config import CATALOG_ROOT_PATH
 from catalog.pub.database.models import VnfPackageModel
-from catalog.pub.exceptions import CatalogException
+from catalog.pub.exceptions import CatalogException, VnfPkgNotFoundException
 from catalog.pub.utils.values import ignore_case_get
 from catalog.pub.utils import fileutil, toscaparser
 
@@ -57,8 +57,6 @@ def create_vnf_pkg(data):
 def query_multiple():
     pkgs_info = []
     nf_pkgs = VnfPackageModel.objects.filter()
-    if not nf_pkgs.exists():
-        raise CatalogException('VNF packages do not exist.')
     for nf_pkg in nf_pkgs:
         ret = fill_response_data(nf_pkg)
         pkgs_info.append(ret)
@@ -68,7 +66,7 @@ def query_multiple():
 def query_single(vnf_pkg_id):
     nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
     if not nf_pkg.exists():
-        raise CatalogException('VNF package(%s) does not exist.' % vnf_pkg_id)
+        raise VnfPkgNotFoundException('VNF package(%s) does not exist.' % vnf_pkg_id)
     return fill_response_data(nf_pkg[0])
 
 
@@ -77,12 +75,12 @@ def delete_vnf_pkg(vnf_pkg_id):
     if not vnf_pkg.exists():
         logger.debug('VNF package(%s) is deleted.' % vnf_pkg_id)
         return
-    if vnf_pkg[0].onboardingState != "CREATED":
-        raise CatalogException("The VNF package (%s) is not on-boarded" % vnf_pkg_id)
+    '''
     if vnf_pkg[0].operationalState != "DISABLED":
         raise CatalogException("The VNF package (%s) is not disabled" % vnf_pkg_id)
     if vnf_pkg[0].usageState != "NOT_IN_USE":
         raise CatalogException("The VNF package (%s) is in use" % vnf_pkg_id)
+    '''
     vnf_pkg.delete()
     vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id)
     fileutil.delete_dirs(vnf_pkg_path)
index fb3a5de..10527cc 100644 (file)
@@ -410,6 +410,11 @@ class TestVnfPackage(TestCase):
         self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
         self.assertEqual(response.data, None)
 
+    def test_delete_when_vnf_pkg_not_exist(self):
+        response = self.client.delete("/api/vnfpkgm/v1/vnf_packages/222")
+        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
+        self.assertEqual(response.data, None)
+
     def test_fetch_vnf_pkg(self):
         with open("vnfPackage.csar", "wb") as fp:
             fp.writelines("AAAABBBBCCCCDDDD")
index 26244dd..3782ea5 100644 (file)
@@ -21,7 +21,7 @@ from drf_yasg.utils import swagger_auto_schema, no_body
 from rest_framework import status
 from rest_framework.decorators import api_view
 from rest_framework.response import Response
-from catalog.pub.exceptions import CatalogException
+from catalog.pub.exceptions import CatalogException, VnfPkgNotFoundException
 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
@@ -206,16 +206,20 @@ def vnf_package_rd(request, vnfPkgId):
             res = query_single(vnfPkgId)
             query_serializer = VnfPkgInfoSerializer(data=res)
             if not query_serializer.is_valid():
-                raise CatalogException
+                logger.debug("Data validation failed.")
+                raise CatalogException(query_serializer.error)
             return Response(data=query_serializer.data, status=status.HTTP_200_OK)
-        except CatalogException:
-            logger.error(traceback.format_exc())
-            return Response(data={'error': 'Query an individual VNF package failed.'},
-                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        except VnfPkgNotFoundException as e:
+            logger.error(e.message)
+            return Response(data={'error': 'Query an individual VNF package failed.'}, status=status.HTTP_404_NOT_FOUND)
+        except CatalogException as e:
+            logger.error(e.message)
+            error_msg = {'error': 'Query an individual VNF package failed.'}
         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)
+            error_msg = {'error': 'unexpected exception'}
+        return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
     if request.method == 'DELETE':
         logger.debug("Delete an individual VNF package> %s" % request.data)
index c994b46..2192e11 100644 (file)
@@ -15,3 +15,7 @@
 
 class CatalogException(Exception):
     pass
+
+
+class VnfPkgNotFoundException(CatalogException):
+    pass