コード例 #1
0
    def test_export_package(self, mock_post, mock_build_opener):
        # prepare
        mock_build_opener.return_value.open.return_value.read.return_value = 'TOKEN'

        prepared_response = Response()
        prepared_response.status_code = 201
        prepared_response._content = 'zip package content'
        mock_post.return_value = prepared_response

        # act
        client = PackagingRestApiClient('SERVER', 9000, 'USER', 'PASS', 'Global')
        response = client.export_package(['topology_name'])

        # verify
        mock_post.assert_called_once_with(
            'http://SERVER:9000/API/Package/ExportPackage',
            headers={'Authorization': 'Basic TOKEN'},
            data={'TopologyNames': ['topology_name']},
        )
        self.assertEqual(response, 'zip package content')
2. Quali API to download blueprint
3. unzip package
4. copy over files in repo and commit
"""
import os

from credentials import cs_credentials
from cloudshell.rest.api import PackagingRestApiClient
import zipfile

TARGET_BLUEPRINT = "backup test"

api = PackagingRestApiClient(ip=cs_credentials["server"],
                             port=9000,
                             username=cs_credentials["user"],
                             password=cs_credentials["password"],
                             domain=cs_credentials["domain"])

# download from quali api to local repo
response_content = api.export_package([TARGET_BLUEPRINT])
package_name = TARGET_BLUEPRINT + "_package.zip"
with open(package_name, "wb") as target_file:
    target_file.write(response_content)

curr_dir = os.getcwd()
package_path = os.path.join(curr_dir, package_name)
unzipped_dir = "{}_package_unzipped".format(TARGET_BLUEPRINT)
unzipped_path = os.path.join(curr_dir, unzipped_dir)
with zipfile.ZipFile(package_path, 'r') as zip_ref:
    zip_ref.extractall(unzipped_path)