mirror of
https://github.com/GNS3/gns3-server.git
synced 2025-01-30 16:13:58 +00:00
Support a name and path during the import
Not exposed to the client for the moment.
This commit is contained in:
parent
786f9c87c9
commit
fb3b6b62f5
@ -34,17 +34,23 @@ Handle the import of project from a .gns3project
|
|||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@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
|
Import a project contain in a zip file
|
||||||
|
|
||||||
You need to handle OSerror exceptions
|
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 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
|
: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)
|
os.makedirs(projects_path, exist_ok=True)
|
||||||
|
|
||||||
with zipfile.ZipFile(stream) as myzip:
|
with zipfile.ZipFile(stream) as myzip:
|
||||||
@ -52,7 +58,10 @@ def import_project(controller, project_id, stream):
|
|||||||
try:
|
try:
|
||||||
topology = json.loads(myzip.read("project.gns3").decode())
|
topology = json.loads(myzip.read("project.gns3").decode())
|
||||||
# If the project name is already used we generate a new one
|
# 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:
|
except KeyError:
|
||||||
raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing")
|
raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing")
|
||||||
|
|
||||||
|
@ -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" / "test"), "project-files/docker/test")
|
||||||
mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test2"), "project-files/docker/test2")
|
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"))
|
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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user