Refactor codes for vnf pkg artifact 29/84629/3
authorfujinhua <fu.jinhua@zte.com.cn>
Tue, 9 Apr 2019 08:21:31 +0000 (16:21 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Tue, 9 Apr 2019 08:57:17 +0000 (16:57 +0800)
Change-Id: Ia3804af864522371a9bf01fd235ac217620b03f0
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
catalog/packages/biz/vnf_pkg_artifacts.py
catalog/packages/tests/test_vnf_package.py
catalog/packages/views/common.py
catalog/packages/views/vnf_package_artifact_views.py

index 1077228..d8a7748 100644 (file)
@@ -25,10 +25,11 @@ class FetchVnfPkgArtifact(object):
     def fetch(self, vnfPkgId, artifactPath):
         logger.debug("FetchVnfPkgArtifact--get--single--artifact--biz::>"
                      "ID: %s path: %s" % (vnfPkgId, artifactPath))
-        vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId).get()
-        if not vnf_pkg:
+        vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
+        if not vnf_pkg.exists():
             err_msg = "NF Package (%s) doesn't exists." % vnfPkgId
-            return ResourceNotFoundException(err_msg)
+            raise ResourceNotFoundException(err_msg)
+        vnf_pkg = vnf_pkg.get()
         local_path = vnf_pkg.localFilePath
         if local_path.endswith(".csar") or local_path.endswith(".zip"):
             vnf_extract_path = fileutil.unzip_csar_to_tmp(local_path)
index 135c983..e83aa7a 100644 (file)
@@ -346,7 +346,7 @@ class TestVnfPackage(TestCase):
         mock_parse_vnfd.return_value = json.JSONEncoder().encode(vnfd_data)
         response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
-        response = self.client.get("/api/vnfpkgm/v1/vnf_packages/2224/artifacts/image")
+        response = self.client.get("/api/vnfpkgm/v1/vnf_packages/1451/artifacts/image")
         self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
 
     @mock.patch.object(toscaparser, 'parse_vnfd')
index f7e3f70..e902f57 100644 (file)
@@ -22,6 +22,7 @@ from catalog.pub.exceptions import CatalogException
 from catalog.pub.exceptions import NsdmBadRequestException
 from catalog.pub.exceptions import PackageNotFoundException
 from catalog.pub.exceptions import ResourceNotFoundException
+from catalog.pub.exceptions import ArtifactNotFoundException
 
 logger = logging.getLogger(__name__)
 
@@ -65,6 +66,12 @@ def view_safe_call_with_log(logger):
                     detail=e.message,
                     status=status.HTTP_404_NOT_FOUND
                 )
+            except ArtifactNotFoundException as e:
+                logger.error(e.message)
+                return make_error_resp(
+                    detail=e.message,
+                    status=status.HTTP_404_NOT_FOUND
+                )
             except NsdmBadRequestException as e:
                 logger.error(e.message)
                 return make_error_resp(
index c321cca..75ae8be 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
-import traceback
 import logging
 
 from drf_yasg.utils import swagger_auto_schema
 from rest_framework import status
 from rest_framework.views import APIView
-from rest_framework.response import Response
 from django.http import FileResponse
 
 from catalog.packages.serializers.response import ProblemDetailsSerializer
 from catalog.packages.biz.vnf_pkg_artifacts import FetchVnfPkgArtifact
-from catalog.pub.exceptions import ResourceNotFoundException, ArtifactNotFoundException
+from .common import view_safe_call_with_log
 
 logger = logging.getLogger(__name__)
-VALID_FILTERS = ["callbackUri", "notificationTypes", "vnfdId", "vnfPkgId", "operationalState", "usageState"]
-
 
-def get_problem_details_serializer(status_code, error_message):
-    problem_details = {
-        "status": status_code,
-        "detail": error_message
-    }
-    problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
-    problem_details_serializer.is_valid()
-    return problem_details_serializer
+VALID_FILTERS = [
+    "callbackUri",
+    "notificationTypes",
+    "vnfdId",
+    "vnfPkgId",
+    "operationalState",
+    "usageState"
+]
 
 
 class FetchVnfPkgmArtifactsView(APIView):
@@ -49,29 +44,11 @@ class FetchVnfPkgmArtifactsView(APIView):
             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
         }
     )
+    @view_safe_call_with_log(logger=logger)
     def get(self, request, vnfPkgId, artifactPath):
         logger.debug("FetchVnfPkgmArtifactsView--get::> ")
-        try:
 
-            resp_data = FetchVnfPkgArtifact().fetch(vnfPkgId, artifactPath)
-            response = FileResponse(resp_data)
+        resp_data = FetchVnfPkgArtifact().fetch(vnfPkgId, artifactPath)
+        response = FileResponse(resp_data)
 
-            return response
-        except ResourceNotFoundException as e:
-            logger.error(e.message)
-            logger.error(traceback.format_exc())
-            problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
-                                                                        traceback.format_exc())
-            return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
-        except ArtifactNotFoundException as e:
-            logger.error(e.message)
-            logger.error(traceback.format_exc())
-            problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
-                                                                        traceback.format_exc())
-            return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
-        except Exception as e:
-            logger.error(e.message)
-            logger.error(traceback.format_exc())
-            problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
-                                                                        traceback.format_exc())
-            return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
+        return response