From a461cb71c691d9bb603a749ef4d27075d71c04e9 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 9 Jul 2015 16:06:52 +0200 Subject: [PATCH] Allow to send the iourc when starting the VM Ref https://github.com/GNS3/gns3-server/issues/255 --- gns3server/handlers/api/iou_handler.py | 9 +++++++++ gns3server/schemas/iou.py | 18 ++++++++++++++++-- tests/handlers/api/test_iou.py | 16 +++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/gns3server/handlers/api/iou_handler.py b/gns3server/handlers/api/iou_handler.py index b4f8fe22..28c26b68 100644 --- a/gns3server/handlers/api/iou_handler.py +++ b/gns3server/handlers/api/iou_handler.py @@ -21,6 +21,7 @@ from aiohttp.web import HTTPConflict from ...web.route import Route from ...schemas.nio import NIO_SCHEMA from ...schemas.iou import IOU_CREATE_SCHEMA +from ...schemas.iou import IOU_START_SCHEMA from ...schemas.iou import IOU_UPDATE_SCHEMA from ...schemas.iou import IOU_OBJECT_SCHEMA from ...schemas.iou import IOU_CAPTURE_SCHEMA @@ -151,11 +152,19 @@ class IOUHandler: 400: "Invalid request", 404: "Instance doesn't exist" }, + input=IOU_START_SCHEMA, description="Start a IOU instance") def start(request, response): iou_manager = IOU.instance() vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) + + for name, value in request.json.items(): + if hasattr(vm, name) and getattr(vm, name) != value: + setattr(vm, name, value) + print(name) + print(vm.iourc_path) + yield from vm.start() response.set_status(204) diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 62079031..f541ba1a 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -91,7 +91,7 @@ IOU_CREATE_SCHEMA = { "type": ["string", "null"] }, "iourc_content": { - "description": "Content of the iourc file, if a file exist on servers this variable is ignored. It's mostly for compatibility with < 1.3 releases", + "description": "Content of the iourc file. Ignored if Null", "type": ["string", "null"] } }, @@ -156,13 +156,27 @@ IOU_UPDATE_SCHEMA = { "type": ["boolean", "null"] }, "iourc_content": { - "description": "Content of the iourc file, if a file exist on servers this variable is ignored. It's mostly for compatibility with < 1.3 releases", + "description": "Content of the iourc file. Ignored if Null", "type": ["string", "null"] } }, "additionalProperties": False, } + +IOU_START_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Request validation to start an IOU instance", + "type": "object", + "properties": { + "iourc_content": { + "description": "Content of the iourc file. Ignored if Null", + "type": ["string", "null"] + } + } +} + + IOU_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "IOU instance", diff --git a/tests/handlers/api/test_iou.py b/tests/handlers/api/test_iou.py index 169ff4ee..94612a03 100644 --- a/tests/handlers/api/test_iou.py +++ b/tests/handlers/api/test_iou.py @@ -137,11 +137,25 @@ def test_iou_get(server, project, vm): def test_iou_start(server, vm): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock: - response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True) + response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"])) assert mock.called assert response.status == 204 +def test_iou_start_with_iourc(server, vm, tmpdir): + body = {"iourc_content": "test"} + + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock: + response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True) + assert mock.called + assert response.status == 204 + + response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"])) + assert response.status == 200 + with open(response.json["iourc_path"]) as f: + assert f.read() == "test" + + def test_iou_stop(server, vm): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.stop", return_value=True) as mock: response = server.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)