diff options
author | laili <lai.li@zte.com.cn> | 2018-08-31 17:54:12 +0800 |
---|---|---|
committer | laili <lai.li@zte.com.cn> | 2018-08-31 17:54:30 +0800 |
commit | 79a5e8900dc61562924579e1d8bcf7016b1d5f78 (patch) | |
tree | 9437197bd12266b62a4ef9afb8131c62ab0c2ea4 | |
parent | d2ec8b67204818f8786e9bd90ed145938c84bf4c (diff) |
Ns descriptor related stuffs.
Refactor and add a test.
Change-Id: Ic32fe45fd70a1c74e165b890584034df3e723606
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/common.py | 30 | ||||
-rw-r--r-- | catalog/packages/biz/ns_descriptor.py | 12 | ||||
-rw-r--r-- | catalog/packages/biz/pnf_descriptor.py | 14 | ||||
-rw-r--r-- | catalog/packages/tests/test_pnf_descriptor.py | 14 |
4 files changed, 47 insertions, 23 deletions
diff --git a/catalog/packages/biz/common.py b/catalog/packages/biz/common.py new file mode 100644 index 00000000..b2fcee55 --- /dev/null +++ b/catalog/packages/biz/common.py @@ -0,0 +1,30 @@ +# Copyright 2018 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 os + +from catalog.pub.config.config import CATALOG_ROOT_PATH +from catalog.pub.utils import fileutil + + +def save(remote_file, descriptor_id): + local_file_name = remote_file.name + local_file_dir = os.path.join(CATALOG_ROOT_PATH, descriptor_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: + for chunk in remote_file.chunks(chunk_size=1024 * 8): + local_file.write(chunk) + return local_file_name diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py index 099a7006..8a890999 100644 --- a/catalog/packages/biz/ns_descriptor.py +++ b/catalog/packages/biz/ns_descriptor.py @@ -24,6 +24,7 @@ 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__) @@ -94,16 +95,9 @@ class NsDescriptor(object): if not ns_pkgs.exists(): logger.info('NSD(%s) does not exist.' % nsd_info_id) raise CatalogException('NSD(%s) does not exist.' % nsd_info_id) - ns_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) - local_file_name = remote_file.name - local_file_dir = os.path.join(CATALOG_ROOT_PATH, nsd_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: - for chunk in remote_file.chunks(chunk_size=1024 * 8): - local_file.write(chunk) + + local_file_name = save(remote_file, nsd_info_id) logger.info('NSD(%s) content has been uploaded.' % nsd_info_id) return local_file_name diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py index 188bc708..8caf98bd 100644 --- a/catalog/packages/biz/pnf_descriptor.py +++ b/catalog/packages/biz/pnf_descriptor.py @@ -24,6 +24,7 @@ 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__) @@ -73,16 +74,9 @@ class PnfPackage(object): if not pnf_pkgs.exists(): logger.info('PNFD(%s) does not exist.' % pnfd_info_id) raise CatalogException('PNFD (%s) does not exist.' % pnfd_info_id) - pnf_pkgs.update(onboardingState=PKG_STATUS.UPLOADING) - 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: - for chunk in remote_file.chunks(chunk_size=1024 * 8): - local_file.write(chunk) + + local_file_name = save(remote_file, pnfd_info_id) logger.info('PNFD(%s) content has been uploaded.' % pnfd_info_id) return local_file_name @@ -137,7 +131,7 @@ def parse_pnfd_and_save(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_pnf_descriptor.py b/catalog/packages/tests/test_pnf_descriptor.py index 60460df5..fc607116 100644 --- a/catalog/packages/tests/test_pnf_descriptor.py +++ b/catalog/packages/tests/test_pnf_descriptor.py @@ -15,18 +15,19 @@ import copy import json -import os import mock +import os from django.test import TestCase from rest_framework import status from rest_framework.test import APIClient -from catalog.pub.database.models import PnfPackageModel, NSPackageModel -from catalog.pub.utils import toscaparser + +from catalog.packages.biz.pnf_descriptor import PnfPackage from catalog.packages.const import PKG_STATUS from catalog.packages.tests.const import pnfd_data -from catalog.packages.biz.pnf_descriptor import PnfPackage +from catalog.pub.database.models import PnfPackageModel, NSPackageModel +from catalog.pub.utils import toscaparser class TestPnfDescriptor(TestCase): @@ -136,6 +137,11 @@ class TestPnfDescriptor(TestCase): self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(None, resp.data) + def test_delete_single_pnfd_when_not_exist(self): + resp = self.client.delete("/api/nsd/v1/pnf_descriptors/22", format='json') + self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(None, resp.data) + @mock.patch.object(toscaparser, "parse_pnfd") def test_pnfd_content_upload_normal(self, mock_parse_pnfd): user_defined_data_json = json.JSONEncoder().encode(self.user_defined_data) |