Ns descriptor related stuffs. 71/61571/3
authorlaili <lai.li@zte.com.cn>
Tue, 21 Aug 2018 12:40:03 +0000 (20:40 +0800)
committerlaili <lai.li@zte.com.cn>
Tue, 21 Aug 2018 13:05:51 +0000 (21:05 +0800)
Implement the biz and view of the uploading pnfd content.

Change-Id: I62bc77d37fa5aaa650aca8b98edf753f236e6a49
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
catalog/packages/biz/pnf_descriptor.py
catalog/packages/views/pnfd_content_views.py [new file with mode: 0644]

index 68d8029..d6710c4 100644 (file)
 
 
 import logging
+import os
 import uuid
 
+from catalog.pub.config.config import CATALOG_ROOT_PATH
+from catalog.pub.utils import fileutil
 from catalog.pub.utils.values import ignore_case_get
 
 logger = logging.getLogger(__name__)
@@ -34,4 +37,17 @@ def create(data):
 
 
 def upload(files, pnfd_info_id):
-    pass
+    remote_files = files
+    for remote_file in remote_files:
+        local_file_name = remote_file.name
+        local_file_dir = os.path.join(CATALOG_ROOT_PATH, pnfd_info_id)
+        local_file_name = os.path.join(local_file_dir, local_file_name)
+        if not os.path.exists(local_file_dir):
+            fileutil.make_dirs(local_file_dir)
+        with open(local_file_name, 'wb') as local_file:
+            if remote_file.multiple_chunks(chunk_size=None):  # TODO: chunk_size
+                for chunk in remote_file.chunks():
+                    local_file.write(chunk)
+            else:
+                data = remote_file.read()
+                local_file.write(data)
diff --git a/catalog/packages/views/pnfd_content_views.py b/catalog/packages/views/pnfd_content_views.py
new file mode 100644 (file)
index 0000000..b991c76
--- /dev/null
@@ -0,0 +1,71 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+import traceback
+
+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 upload
+from catalog.pub.exceptions import CatalogException
+
+logger = logging.getLogger(__name__)
+
+
+@swagger_auto_schema(
+    responses={
+        # status.HTTP_200_OK: Serializer(),
+        status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+    }
+)
+def get(self, request):
+    # TODO
+    return None
+
+
+@swagger_auto_schema(
+    # request_body=CreateVnfReqSerializer(),
+    responses={
+        #     status.HTTP_201_CREATED: CreateVnfRespSerializer(),
+        status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+    }
+)
+def post(self, request):
+    # TODO
+    return None
+
+
+@swagger_auto_schema(
+    method='PUT',
+    operation_description="Upload PNFD content",
+    request_body=no_body,
+    responses={
+        status.HTTP_204_NO_CONTENT: {},
+        status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+    }
+)
+@api_view(http_method_names=['PUT'])
+def upload_pnfd_content(request, *args, **kwargs):
+    pnfd_info_id = kwargs.get("pnfdInfoId")
+    files = request.FILES.getlist('file')
+    try:
+        upload(files, pnfd_info_id)
+        return Response(data={}, status=status.HTTP_204_NO_CONTENT)
+    except IOError:
+        logger.error(traceback.format_exc())
+        raise CatalogException
+        return Response(data={'error': 'Uploading pnfd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)