aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-04-09 16:21:31 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-04-09 16:57:17 +0800
commit4005f05cbf5be14ad89a8c3da89d7d905b6b9a7a (patch)
treede7800cdaa5bc3bcdd66f23363379d17ee2f4553
parentf5a0353d4127f32ebc95012b24f344b858ceec3a (diff)
Refactor codes for vnf pkg artifact
Change-Id: Ia3804af864522371a9bf01fd235ac217620b03f0 Issue-ID: VFC-1306 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--catalog/packages/biz/vnf_pkg_artifacts.py7
-rw-r--r--catalog/packages/tests/test_vnf_package.py2
-rw-r--r--catalog/packages/views/common.py7
-rw-r--r--catalog/packages/views/vnf_package_artifact_views.py49
4 files changed, 25 insertions, 40 deletions
diff --git a/catalog/packages/biz/vnf_pkg_artifacts.py b/catalog/packages/biz/vnf_pkg_artifacts.py
index 1077228a..d8a77488 100644
--- a/catalog/packages/biz/vnf_pkg_artifacts.py
+++ b/catalog/packages/biz/vnf_pkg_artifacts.py
@@ -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)
diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py
index 135c9838..e83aa7a3 100644
--- a/catalog/packages/tests/test_vnf_package.py
+++ b/catalog/packages/tests/test_vnf_package.py
@@ -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')
diff --git a/catalog/packages/views/common.py b/catalog/packages/views/common.py
index f7e3f708..e902f578 100644
--- a/catalog/packages/views/common.py
+++ b/catalog/packages/views/common.py
@@ -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(
diff --git a/catalog/packages/views/vnf_package_artifact_views.py b/catalog/packages/views/vnf_package_artifact_views.py
index c321ccaa..75ae8be2 100644
--- a/catalog/packages/views/vnf_package_artifact_views.py
+++ b/catalog/packages/views/vnf_package_artifact_views.py
@@ -12,32 +12,27 @@
# 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