diff options
author | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-27 14:30:59 +0800 |
---|---|---|
committer | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-27 14:30:59 +0800 |
commit | 4f432f67318225c6c13e28a70c153e090d222208 (patch) | |
tree | c5d66630baaa27de079e43ca3bfd46feea8e3e97 | |
parent | d58f1c71fb1571671a0a7c83ee0d09b068440d32 (diff) |
Deal with nfPackage
fetch nfPkg content
Change-Id: I35935a92105028b7b3750ceaff4f3f3cfacc9940
Issue-ID: VFC-1038
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 16 | ||||
-rw-r--r-- | catalog/packages/views/vnf_package_views.py | 74 |
2 files changed, 65 insertions, 25 deletions
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index 71ec697e..d251f3ba 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -21,6 +21,8 @@ import traceback import urllib2 import uuid +from rest_framework import status +from django.http import FileResponse from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.database.models import VnfPackageModel from catalog.pub.exceptions import CatalogException @@ -164,3 +166,17 @@ def fill_response_data(nf_pkg): pkg_info["userDefinedData"] = json.JSONDecoder().decode(nf_pkg[0].userDefinedData) pkg_info["_links"] = None # TODO return pkg_info + + +def fetch_vnf_pkg(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) + if nf_pkg[0].localFilePath != "ONBOARDED": + raise CatalogException("VNF package (%s) is not on-boarded" % vnf_pkg_id) + file_path = nf_pkg[0].localFilePath + file_name = file_path.split('/')[-1] + file_name = file_name.split('\\')[-1] + response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK) + response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8') + return response diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py index 7defc347..43ac26fa 100644 --- a/catalog/packages/views/vnf_package_views.py +++ b/catalog/packages/views/vnf_package_views.py @@ -27,7 +27,7 @@ from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInf from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfPkgUploadThread, \ - query_single, delete_vnf_pkg, parse_vnfd_and_save + query_single, delete_vnf_pkg, parse_vnfd_and_save, fetch_vnf_pkg from catalog.pub.database.models import VnfPackageModel logger = logging.getLogger(__name__) @@ -98,32 +98,56 @@ def vnf_packages_rc(request): status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) -@api_view(http_method_names=['PUT']) +@swagger_auto_schema( + method="GET", + operation_description="Fetch VNF package content", + request_body=no_body, + responses={ + status.HTTP_200_OK: VnfPkgInfosSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } +) +@api_view(http_method_names=["PUT", "GET"]) def upload_vnf_pkg_content(request, vnfPkgId): - logger.debug("Upload VNF package %s" % vnfPkgId) - try: - vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) - if vnf_pkg[0].onboardingState != "CREATED": - raise CatalogException("VNF package (%s) is not created" % vnfPkgId) - file_object = request.FILES.get('file') - upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) - if not os.path.exists(upload_path): - os.makedirs(upload_path, 0o777) - - upload_file_name = os.path.join(upload_path, file_object.name) - with open(upload_file_name, 'wb+') as dest_file: - for chunk in file_object.chunks(): - dest_file.write(chunk) - - parse_vnfd_and_save(vnfPkgId, upload_file_name) - return Response(None, status=status.HTTP_202_ACCEPTED) - except CatalogException: + if request.method == "PUT": + logger.debug("Upload VNF package %s" % vnfPkgId) + try: + vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) + if vnf_pkg[0].onboardingState != "CREATED": + raise CatalogException("VNF package (%s) is not created" % vnfPkgId) + file_object = request.FILES.get('file') + upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) + if not os.path.exists(upload_path): + os.makedirs(upload_path, 0o777) + + upload_file_name = os.path.join(upload_path, file_object.name) + with open(upload_file_name, 'wb+') as dest_file: + for chunk in file_object.chunks(): + dest_file.write(chunk) + + parse_vnfd_and_save(vnfPkgId, upload_file_name) + return Response(None, status=status.HTTP_202_ACCEPTED) + except CatalogException: + logger.error(traceback.format_exc()) + return Response(data={'error': 'Upload VNF package failed.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + except Exception as e: + logger.error(e.message) logger.error(traceback.format_exc()) - return Response(data={'error': 'Upload VNF package failed.'}, 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) + return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + if request.method == "GET": + try: + response = fetch_vnf_pkg(vnfPkgId) + return response + except CatalogException: + logger.error(traceback.format_exc()) + return Response(data={'error': 'Fetch VNF package failed.'}, + 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) @swagger_auto_schema( |