diff options
author | laili <lai.li@zte.com.cn> | 2018-09-03 15:38:52 +0800 |
---|---|---|
committer | laili <lai.li@zte.com.cn> | 2018-09-03 15:38:52 +0800 |
commit | 33f078ece7f04d26b4f2f08311b197aa297ff6d6 (patch) | |
tree | 6a6ec34777558c2e33bd36d0f773e70342ccb898 | |
parent | 9cc81a60be51eb797a8a1da41e0f4f7ea0731407 (diff) |
Ns descriptor related stuffs.
Refactor biz and view of pnfd.
Change-Id: I5cfa53f1d84a3a0974ced25ed3c804a89c64dd75
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/pnf_descriptor.py | 11 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_package.py | 14 | ||||
-rw-r--r-- | catalog/packages/views/ns_descriptor_views.py | 3 | ||||
-rw-r--r-- | catalog/packages/views/pnf_descriptor_views.py | 67 |
4 files changed, 47 insertions, 48 deletions
diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py index ef93173b..e250ba0c 100644 --- a/catalog/packages/biz/pnf_descriptor.py +++ b/catalog/packages/biz/pnf_descriptor.py @@ -18,13 +18,13 @@ import logging import os import uuid +from catalog.packages.biz.common import read, save +from catalog.packages.const import PKG_STATUS from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.database.models import NSPackageModel, PnfPackageModel from catalog.pub.exceptions import CatalogException, ResourceNotFoundException from catalog.pub.utils import fileutil, toscaparser from catalog.pub.utils.values import ignore_case_get -from catalog.packages.const import PKG_STATUS -from catalog.packages.biz.common import save logger = logging.getLogger(__name__) @@ -120,17 +120,16 @@ class PnfDescriptor(object): if pnf_pkgs[0].onboardingState != PKG_STATUS.ONBOARDED: logger.error('PNFD(%s) is not ONBOARDED.' % pnfd_info_id) raise CatalogException('PNFD(%s) is not ONBOARDED.' % pnfd_info_id) + local_file_path = pnf_pkgs[0].localFilePath - local_file_name = local_file_path.split('/')[-1] - local_file_name = local_file_name.split('\\')[-1] + start, end = 0, os.path.getsize(local_file_path) logger.info('PNFD(%s) has been downloaded.' % pnfd_info_id) - return local_file_path, local_file_name, os.path.getsize(local_file_path) + return read(local_file_path, start, end) def parse_pnfd_and_save(self, pnfd_info_id, local_file_name): logger.info('Start to process PNFD(%s)...' % pnfd_info_id) pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id) pnf_pkgs.update(onboardingState=PKG_STATUS.PROCESSING) - PnfPackageModel pnfd_json = toscaparser.parse_pnfd(local_file_name) pnfd = json.JSONDecoder().decode(pnfd_json) diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py index 1bfafa7d..ef2dac49 100644 --- a/catalog/packages/tests/test_vnf_package.py +++ b/catalog/packages/tests/test_vnf_package.py @@ -12,21 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import json -import mock +import os import urllib2 +import mock -from rest_framework.test import APIClient from django.test import TestCase from rest_framework import status +from rest_framework.test import APIClient + +from catalog.packages.biz.vnf_package import VnfPackage, VnfPkgUploadThread +from catalog.packages.const import PKG_STATUS +from catalog.packages.tests.const import vnfd_data from catalog.pub.config.config import CATALOG_ROOT_PATH -from catalog.packages.biz.vnf_package import VnfPkgUploadThread from catalog.pub.database.models import VnfPackageModel from catalog.pub.utils import toscaparser -from catalog.packages.const import PKG_STATUS -from catalog.packages.tests.const import vnfd_data -from catalog.packages.biz.vnf_package import VnfPackage class MockReq(): diff --git a/catalog/packages/views/ns_descriptor_views.py b/catalog/packages/views/ns_descriptor_views.py index 83992431..aa68accf 100644 --- a/catalog/packages/views/ns_descriptor_views.py +++ b/catalog/packages/views/ns_descriptor_views.py @@ -167,8 +167,7 @@ def nsd_content_ru(request, **kwargs): try: file_range = request.META.get('RANGE') file_iterator = NsDescriptor().download(nsd_info_id, file_range) - response = StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK) - return response + return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK) except ResourceNotFoundException as e: logger.error(e.message) error_data = {'error': e.message} diff --git a/catalog/packages/views/pnf_descriptor_views.py b/catalog/packages/views/pnf_descriptor_views.py index 09b242aa..15d7e643 100644 --- a/catalog/packages/views/pnf_descriptor_views.py +++ b/catalog/packages/views/pnf_descriptor_views.py @@ -15,11 +15,12 @@ import logging import traceback -from django.http import FileResponse +from django.http import StreamingHttpResponse from drf_yasg.utils import no_body, swagger_auto_schema from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response + from catalog.packages.biz.pnf_descriptor import PnfDescriptor from catalog.packages.serializers.create_pnfd_info_request import CreatePnfdInfoRequestSerializer from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer @@ -36,7 +37,7 @@ logger = logging.getLogger(__name__) request_body=no_body, responses={ status.HTTP_200_OK: PnfdInfoSerializer(), - status.HTTP_404_NOT_FOUND: "PNFD does not exist.", + status.HTTP_404_NOT_FOUND: "PNFD does not exist", status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) @@ -50,35 +51,38 @@ logger = logging.getLogger(__name__) } ) @api_view(http_method_names=['GET', 'DELETE']) -def pnfd_info_rd(request, pnfdInfoId): # TODO +def pnfd_info_rd(request, **kwargs): # TODO + pnfd_info_id = kwargs.get('pnfdInfoId') if request.method == 'GET': logger.debug("Query an individual PNF descriptor> %s" % request.data) try: - data = PnfDescriptor().query_single(pnfdInfoId) + data = PnfDescriptor().query_single(pnfd_info_id) pnfd_info = validate_data(data, PnfdInfoSerializer) return Response(data=pnfd_info.data, status=status.HTTP_200_OK) except ResourceNotFoundException as e: logger.error(e.message) - return Response(data={'error': "PNFD does not exist."}, status=status.HTTP_404_NOT_FOUND) + error_data = {'error': e.message} + error_code = status.HTTP_404_NOT_FOUND except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Query of a PNFD failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_data = {'error': 'Query of PNFD(%s) failed.' % pnfd_info_id} + error_code = status.HTTP_500_INTERNAL_SERVER_ERROR + return Response(data=error_data, status=error_code) if request.method == 'DELETE': logger.debug("Delete an individual PNFD resource> %s" % request.data) try: - PnfDescriptor().delete_single(pnfdInfoId) + PnfDescriptor().delete_single(pnfd_info_id) return Response(data=None, status=status.HTTP_204_NO_CONTENT) except CatalogException as e: logger.error(e.message) - error_msg = {'error': 'Deletion of a PNFD failed.'} + error_data = {'error': e.message} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Deletion of a PNFD failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_data = {'error': 'Deletion of a PNFD failed.'} + return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @swagger_auto_schema( @@ -100,21 +104,18 @@ def pnfd_info_rd(request, pnfdInfoId): # TODO } ) @api_view(http_method_names=['POST', 'GET']) -def pnf_descriptors_rc(request, *args, **kwargs): +def pnf_descriptors_rc(request): if request.method == 'POST': try: create_pnfd_info_request = validate_data(request.data, CreatePnfdInfoRequestSerializer) data = PnfDescriptor().create(create_pnfd_info_request.data) pnfd_info = validate_data(data, PnfdInfoSerializer) return Response(data=pnfd_info.data, status=status.HTTP_201_CREATED) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Creating a pnfd failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Creating a pnfd failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_data = {'error': 'Creating a pnfd failed.'} + return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': try: @@ -124,8 +125,8 @@ def pnf_descriptors_rc(request, *args, **kwargs): except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Query of multiple PNFDs failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_data = {'error': 'Query of multiple PNFDs failed.'} + return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @swagger_auto_schema( @@ -143,12 +144,12 @@ def pnf_descriptors_rc(request, *args, **kwargs): request_body=no_body, responses={ status.HTTP_204_NO_CONTENT: 'PNFD file', - status.HTTP_404_NOT_FOUND: "PNFD does not exist.", + status.HTTP_404_NOT_FOUND: "PNFD does not exist", status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) @api_view(http_method_names=['PUT', 'GET']) -def pnfd_content_ru(request, *args, **kwargs): +def pnfd_content_ru(request, **kwargs): pnfd_info_id = kwargs.get("pnfdInfoId") if request.method == 'PUT': files = request.FILES.getlist('file') @@ -159,29 +160,29 @@ def pnfd_content_ru(request, *args, **kwargs): except CatalogException as e: PnfDescriptor().handle_upload_failed(pnfd_info_id) logger.error(e.message) - error_msg = {'error': 'Uploading PNFD content failed.'} + error_data = {'error': e.message} except Exception as e: PnfDescriptor().handle_upload_failed(pnfd_info_id) logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Uploading PNFD content failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_data = {'error': 'Uploading PNFD content failed.'} + return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': try: - file_path, file_name, file_size = PnfDescriptor().download(pnfd_info_id) - response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK) - response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8') - response['Content-Length'] = file_size - return response + file_iterator = PnfDescriptor().download(pnfd_info_id) + return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK) except ResourceNotFoundException as e: logger.error(e.message) - return Response(data={'error': "PNFD does not exist."}, status=status.HTTP_404_NOT_FOUND) + error_data = {'error': e.message} + error_code = status.HTTP_404_NOT_FOUND except CatalogException as e: logger.error(e.message) - error_msg = {'error': 'Downloading PNFD content failed.'} + error_data = {'error': e.message} + error_code = status.HTTP_500_INTERNAL_SERVER_ERROR except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) - error_msg = {'error': 'Downloading PNFD content failed.'} - return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + error_code = status.HTTP_500_INTERNAL_SERVER_ERROR + error_data = {'error': 'Downloading PNFD content failed.'} + return Response(data=error_data, status=error_code) |