From 1736ae789581b5013297852eec03d668fc7e1328 Mon Sep 17 00:00:00 2001 From: Shubham Potale Date: Tue, 24 Dec 2019 12:50:11 +0530 Subject: [PATCH] Add command for Fetch VNF package API Added command support for downloading csar vnf package contents. Change-Id: I6371ebdf0dd9d127ca88bcd721a31036d11ea5fc Implements: bp enhance-vnf-package-support-part1 --- tackerclient/osc/v1/vnfpkgm/vnf_package.py | 19 +++++------ .../tests/unit/osc/v1/test_vnf_package.py | 34 +++++++++++++++++++ tackerclient/v1_0/client.py | 9 +++++ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/tackerclient/osc/v1/vnfpkgm/vnf_package.py b/tackerclient/osc/v1/vnfpkgm/vnf_package.py index 5f9ed843..b5b8e87d 100644 --- a/tackerclient/osc/v1/vnfpkgm/vnf_package.py +++ b/tackerclient/osc/v1/vnfpkgm/vnf_package.py @@ -260,7 +260,8 @@ class DeleteVnfPackage(command.Command): class DownloadVnfPackage(command.Command): - _description = _("Read VNFD of an on-boarded VNF Package") + _description = _("Download VNF package contents or VNFD of an on-boarded " + "VNF package.") def get_parser(self, prog_name): parser = super(DownloadVnfPackage, self).get_parser(prog_name) @@ -272,9 +273,9 @@ class DownloadVnfPackage(command.Command): parser.add_argument( "--file", metavar="", - help=_("Local file to save downloaded vnfd data. " + help=_("Local file to save downloaded VNF Package or VNFD data. " "If this is not specified and there is no redirection " - "then vnfd data will not be saved.") + "then data will not be saved.") ) parser.add_argument( "--vnfd", @@ -308,15 +309,13 @@ class DownloadVnfPackage(command.Command): body = client.download_vnfd_from_vnf_package( parsed_args.vnf_package, parsed_args.type) - if parsed_args.file: - sdk_utils.save_data(body, parsed_args.file) - else: + if not parsed_args.file: print(body) - + return else: - msg = ("Currently only download vnfd from on-boarded vnf package " - "is supported. use --vnfd") - sdk_utils.exit(msg) + body = client.download_vnf_package(parsed_args.vnf_package) + + sdk_utils.save_data(body, parsed_args.file) class UpdateVnfPackage(command.ShowOne): diff --git a/tackerclient/tests/unit/osc/v1/test_vnf_package.py b/tackerclient/tests/unit/osc/v1/test_vnf_package.py index 4fba7dc6..d9cf0b2f 100644 --- a/tackerclient/tests/unit/osc/v1/test_vnf_package.py +++ b/tackerclient/tests/unit/osc/v1/test_vnf_package.py @@ -560,3 +560,37 @@ class TestDownloadVnfPackage(TestVnfPackage): self.assertRaises(SystemExit, self.download_vnf_package.take_action, parsed_args) + + def test_download_vnf_package(self): + file_name = 'vnf_package_data.zip' + test_file, temp_dir = _create_zip() + + # file in which VNF Package data will be stored. + # for testing purpose we are creating temporary zip file. + local_file = tempfile.NamedTemporaryFile(suffix=file_name) + vnf_package_data = open(test_file, 'rb').read() + + arglist = [ + self._vnf_package['id'], + '--file', local_file.name + ] + verifylist = [ + ('vnf_package', self._vnf_package['id']), + ('file', local_file.name) + ] + + parsed_args = self.check_parser(self.download_vnf_package, arglist, + verifylist) + + url = os.path.join(self.url, '/vnfpkgm/v1/vnf_packages', + self._vnf_package['id'], 'package_content') + + self.requests_mock.register_uri( + 'GET', url, headers={'content-type': 'application/zip'}, + content=vnf_package_data) + + self.download_vnf_package.take_action(parsed_args) + self.assertTrue(filecmp.cmp(test_file, local_file.name), + "Downloaded contents don't match test file") + self.assertTrue(self._check_valid_zip_file(local_file.name)) + shutil.rmtree(temp_dir) diff --git a/tackerclient/v1_0/client.py b/tackerclient/v1_0/client.py index f4062121..427f3527 100644 --- a/tackerclient/v1_0/client.py +++ b/tackerclient/v1_0/client.py @@ -754,6 +754,7 @@ class VnfPackageClient(ClientBase): vnfpackages_path = '/vnfpkgm/v1/vnf_packages' vnfpackage_path = '/vnfpkgm/v1/vnf_packages/%s' vnfpackage_vnfd_path = '/vnfpkgm/v1/vnf_packages/%s/vnfd' + vnfpackage_download_path = '/vnfpkgm/v1/vnf_packages/%s/package_content' def build_action(self, action): return action @@ -794,6 +795,11 @@ class VnfPackageClient(ClientBase): base_path=self.vnfpackages_path), body=file_data) + @APIParamsCall + def download_vnf_package(self, vnf_package): + self.format = 'zip' + return self.get(self.vnfpackage_download_path % vnf_package) + @APIParamsCall def download_vnfd_from_vnf_package(self, vnf_package, accept): """Read VNFD of an on-boarded VNF Package. @@ -1148,3 +1154,6 @@ class Client(object): def download_vnfd_from_vnf_package(self, vnf_package, accept): return self.vnf_package_client.download_vnfd_from_vnf_package( vnf_package, accept) + + def download_vnf_package(self, vnf_package): + return self.vnf_package_client.download_vnf_package(vnf_package)