From fa57485f11fd3e2d6599fbf4c87d7a7f28c4c044 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 20 Jan 2015 19:56:18 +0100 Subject: [PATCH] Support script file --- docs/api/examples/post_vpcs.txt | 5 +++-- gns3server/handlers/vpcs_handler.py | 10 +++++----- gns3server/modules/base_manager.py | 4 ++-- gns3server/modules/vpcs/vpcs_vm.py | 13 +++++++++++-- gns3server/schemas/vpcs.py | 10 +++++++++- tests/api/test_vpcs.py | 10 ++++++++++ 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/docs/api/examples/post_vpcs.txt b/docs/api/examples/post_vpcs.txt index 00d846ec..9b6ffd02 100644 --- a/docs/api/examples/post_vpcs.txt +++ b/docs/api/examples/post_vpcs.txt @@ -9,7 +9,7 @@ POST /vpcs HTTP/1.1 HTTP/1.1 200 CONNECTION: close -CONTENT-LENGTH: 160 +CONTENT-LENGTH: 185 CONTENT-TYPE: application/json DATE: Thu, 08 Jan 2015 16:09:15 GMT SERVER: Python/3.4 aiohttp/0.13.1 @@ -19,5 +19,6 @@ X-ROUTE: /vpcs "console": 2000, "name": "PC TEST 1", "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80", - "uuid": "6373ecfa-a186-48de-be4d-3f3463d2e23c" + "script_file": null, + "uuid": "aa017896-574d-4a10-bcad-d3001ea98f8b" } diff --git a/gns3server/handlers/vpcs_handler.py b/gns3server/handlers/vpcs_handler.py index 3eb9b46d..2978e0de 100644 --- a/gns3server/handlers/vpcs_handler.py +++ b/gns3server/handlers/vpcs_handler.py @@ -41,11 +41,11 @@ class VPCSHandler: def create(request, response): vpcs = VPCS.instance() - vm = yield from vpcs.create_vm(request.json["name"], request.json["project_uuid"], uuid=request.json.get("uuid")) - response.json({"name": vm.name, - "uuid": vm.uuid, - "console": vm.console, - "project_uuid": vm.project.uuid}) + vm = yield from vpcs.create_vm(request.json["name"], + request.json["project_uuid"], + uuid=request.json.get("uuid"), + script_file=request.json.get("script_file")) + response.json(vm) @classmethod @Route.post( diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index c90a2bab..5a98ebd0 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -87,7 +87,7 @@ class BaseManager: return self._vms[uuid] @asyncio.coroutine - def create_vm(self, name, project_identifier, uuid=None): + def create_vm(self, name, project_identifier, uuid=None, **kwargs): """ Create a new VM @@ -104,7 +104,7 @@ class BaseManager: if not uuid: uuid = str(uuid4()) - vm = self._VM_CLASS(name, uuid, project, self) + vm = self._VM_CLASS(name, uuid, project, self, **kwargs) future = vm.create() if isinstance(future, asyncio.Future): yield from future diff --git a/gns3server/modules/vpcs/vpcs_vm.py b/gns3server/modules/vpcs/vpcs_vm.py index cd02882e..66cba55e 100644 --- a/gns3server/modules/vpcs/vpcs_vm.py +++ b/gns3server/modules/vpcs/vpcs_vm.py @@ -52,9 +52,10 @@ class VPCSVM(BaseVM): :param project: Project instance :param manager: parent VM Manager :param console: TCP console port + :param script_file: A VPCS startup script """ - def __init__(self, name, uuid, project, manager, console=None): + def __init__(self, name, uuid, project, manager, console=None, script_file=None): super().__init__(name, uuid, project, manager) @@ -68,7 +69,7 @@ class VPCSVM(BaseVM): self._started = False # VPCS settings - self._script_file = "" + self._script_file = script_file self._ethernet_adapter = EthernetAdapter() # one adapter with 1 Ethernet interface try: @@ -103,6 +104,14 @@ class VPCSVM(BaseVM): self._check_vpcs_version() + def __json__(self): + + return {"name": self.name, + "uuid": self.uuid, + "console": self.console, + "project_uuid": self.project.uuid, + "script_file": self.script_file} + @property def console(self): """ diff --git a/gns3server/schemas/vpcs.py b/gns3server/schemas/vpcs.py index 8a2834ae..ed738eb4 100644 --- a/gns3server/schemas/vpcs.py +++ b/gns3server/schemas/vpcs.py @@ -50,6 +50,10 @@ VPCS_CREATE_SCHEMA = { "maximum": 65535, "type": "integer" }, + "script_file": { + "description": "VPCS startup script", + "type": ["string", "null"] + }, }, "additionalProperties": False, "required": ["name", "project_uuid"] @@ -143,7 +147,11 @@ VPCS_OBJECT_SCHEMA = { "minLength": 36, "maxLength": 36, "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" - } + }, + "script_file": { + "description": "VPCS startup script", + "type": ["string", "null"] + }, }, "additionalProperties": False, "required": ["name", "uuid", "console", "project_uuid"] diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index 7054bfbb..0f56e1a1 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -35,6 +35,16 @@ def test_vpcs_create(server, project): assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" assert response.json["project_uuid"] == project.uuid + assert response.json["script_file"] is None + + +def test_vpcs_create_script_file(server, project): + response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid, "script_file": "/tmp/test"}) + assert response.status == 200 + assert response.route == "/vpcs" + assert response.json["name"] == "PC TEST 1" + assert response.json["project_uuid"] == project.uuid + assert response.json["script_file"] == "/tmp/test" def test_vpcs_nio_create_udp(server, vm):