From fb3b6b62f57f2b6baad63bd6708f2040a64db92a Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 25 Jul 2016 09:41:41 +0200 Subject: [PATCH] Support a name and path during the import Not exposed to the client for the moment. --- gns3server/controller/import_project.py | 17 ++++++++++--- tests/controller/test_import_project.py | 34 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/gns3server/controller/import_project.py b/gns3server/controller/import_project.py index 0ccd72fe..7c8b81c3 100644 --- a/gns3server/controller/import_project.py +++ b/gns3server/controller/import_project.py @@ -34,17 +34,23 @@ Handle the import of project from a .gns3project @asyncio.coroutine -def import_project(controller, project_id, stream): +def import_project(controller, project_id, stream, location=None, name=None): """ Import a project contain in a zip file You need to handle OSerror exceptions + :param controller: GNS3 Controller + :param project_id: ID of the project to import :param stream: A io.BytesIO of the zipfile - :param gns3vm: True move Docker, IOU and Qemu to the GNS3 VM + :param location: Parent directory for the project if None put in the default directory + :param name: Wanted project name, generate one from the .gns3 if None :returns: Project """ - projects_path = controller.projects_directory() + if location: + projects_path = location + else: + projects_path = controller.projects_directory() os.makedirs(projects_path, exist_ok=True) with zipfile.ZipFile(stream) as myzip: @@ -52,7 +58,10 @@ def import_project(controller, project_id, stream): try: topology = json.loads(myzip.read("project.gns3").decode()) # If the project name is already used we generate a new one - project_name = controller.get_free_project_name(topology["name"]) + if name: + project_name = controller.get_free_project_name(name) + else: + project_name = controller.get_free_project_name(topology["name"]) except KeyError: raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing") diff --git a/tests/controller/test_import_project.py b/tests/controller/test_import_project.py index 08e82006..b68f46d0 100644 --- a/tests/controller/test_import_project.py +++ b/tests/controller/test_import_project.py @@ -237,3 +237,37 @@ def test_move_files_to_compute(tmpdir, async_run): mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test"), "project-files/docker/test") mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test2"), "project-files/docker/test2") assert not os.path.exists(str(tmpdir / "project-files" / "docker")) + + +def test_import_project_name_and_location(async_run, tmpdir, controller): + """ + Import a project with a different location and name + """ + project_id = str(uuid.uuid4()) + + topology = { + "project_id": str(uuid.uuid4()), + "name": "test", + "topology": { + }, + "version": "2.0.0" + } + + with open(str(tmpdir / "project.gns3"), 'w+') as f: + json.dump(topology, f) + + zip_path = str(tmpdir / "project.zip") + with zipfile.ZipFile(zip_path, 'w') as myzip: + myzip.write(str(tmpdir / "project.gns3"), "project.gns3") + + with open(zip_path, "rb") as f: + project = async_run(import_project(controller, project_id, f, name="hello", location=str(tmpdir / "test"))) + + assert project.name == "hello" + + assert os.path.exists(str(tmpdir / "test" / "hello" / "hello.gns3")) + + # A new project name is generated when you import twice the same name + with open(zip_path, "rb") as f: + project = async_run(import_project(controller, str(uuid.uuid4()), f, name="hello", location=str(tmpdir / "test"))) + assert project.name == "hello-1"