From 9771b33b688fed23a3f549248bd8e3ba7c685671 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 14 Mar 2016 16:51:47 +0100 Subject: [PATCH] Create link using UDP work --- gns3server/controller/hypervisor.py | 25 ++++++++++++---- gns3server/controller/project.py | 4 +-- gns3server/controller/udp_link.py | 4 +-- .../handlers/api/controller/link_handler.py | 1 + tests/controller/test_hypervisor.py | 2 +- tests/handlers/api/controller/test_link.py | 30 ++++++++++--------- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/gns3server/controller/hypervisor.py b/gns3server/controller/hypervisor.py index 7304e4ea..abb05e90 100644 --- a/gns3server/controller/hypervisor.py +++ b/gns3server/controller/hypervisor.py @@ -91,11 +91,26 @@ class Hypervisor: data = json.dumps(data) response = yield from session.request(method, url, headers=headers, data=data) body = yield from response.read() - if response.status >= 300: - raise aiohttp.errors.HttpProcessingError(code=response.status, message=body) - yield from response.release() - return body + if body: + body = body.decode() + if response.status == 400: + raise aiohttp.web.HTTPBadRequest(text=body) + elif response.status == 401: + raise aiohttp.web.HTTPUnauthorized(text=body) + elif response.status == 403: + raise aiohttp.web.HTTPForbidden(text=body) + elif response.status == 404: + raise aiohttp.web.HTTPNotFound(text="{} not found on hypervisor".format(url)) + elif response.status == 409: + raise aiohttp.web.HTTPConflict(text=body) + elif response.status >= 300: + raise NotImplemented("{} status code is not supported".format(e.status)) + if body and len(body): + response.json = json.loads(body) + else: + response.json = {} + return response @asyncio.coroutine def post(self, path, data={}): - yield from self.httpQuery("POST", path, data) + return (yield from self.httpQuery("POST", path, data)) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 840731b5..fd2e4f47 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -20,7 +20,7 @@ import aiohttp from uuid import UUID, uuid4 from .vm import VM -from .link import Link +from .udp_link import UDPLink class Project: @@ -98,7 +98,7 @@ class Project: """ Create a link. By default the link is empty """ - link = Link(self) + link = UDPLink(self) self._links[link.id] = link return link diff --git a/gns3server/controller/udp_link.py b/gns3server/controller/udp_link.py index c731be13..dff41c3c 100644 --- a/gns3server/controller/udp_link.py +++ b/gns3server/controller/udp_link.py @@ -35,9 +35,9 @@ class UDPLink(Link): port_number2 = self._vms[1]["port_number"] # Reserve a UDP port on both side - response = yield from vm1.post("/ports/udp".format(self._project.id)) + response = yield from vm1.hypervisor.post("/projects/{}/ports/udp".format(self._project.id)) vm1_port = response.json["udp_port"] - response = yield from vm2.post("/ports/udp".format(self._project.id)) + response = yield from vm2.hypervisor.post("/projects/{}/ports/udp".format(self._project.id)) vm2_port = response.json["udp_port"] # Create the tunnel on both side diff --git a/gns3server/handlers/api/controller/link_handler.py b/gns3server/handlers/api/controller/link_handler.py index 2fdaa762..be2d3741 100644 --- a/gns3server/handlers/api/controller/link_handler.py +++ b/gns3server/handlers/api/controller/link_handler.py @@ -48,5 +48,6 @@ class LinkHandler: yield from link.addVM(project.getVM(vm["vm_id"]), vm["adapter_number"], vm["port_number"]) + yield from link.create() response.set_status(201) response.json(link) diff --git a/tests/controller/test_hypervisor.py b/tests/controller/test_hypervisor.py index 845d5cb0..72035732 100644 --- a/tests/controller/test_hypervisor.py +++ b/tests/controller/test_hypervisor.py @@ -64,7 +64,7 @@ def test_hypervisor_httpQueryError(hypervisor, async_run): with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock: response.status = 409 - with pytest.raises(aiohttp.errors.HttpProcessingError): + with pytest.raises(aiohttp.web.HTTPConflict): async_run(hypervisor.post("/projects", {"a": "b"})) diff --git a/tests/handlers/api/controller/test_link.py b/tests/handlers/api/controller/test_link.py index 343a32de..ce82d02c 100644 --- a/tests/handlers/api/controller/test_link.py +++ b/tests/handlers/api/controller/test_link.py @@ -51,20 +51,22 @@ def test_create_link(http_controller, tmpdir, project, hypervisor, async_run): vm1 = async_run(project.addVM(hypervisor, None)) vm2 = async_run(project.addVM(hypervisor, None)) - response = http_controller.post("/projects/{}/links".format(project.id), { - "vms": [ - { - "vm_id": vm1.id, - "adapter_number": 0, - "port_number": 3 - }, - { - "vm_id": vm2.id, - "adapter_number": 2, - "port_number": 4 - } - ] - }, example=True) + + with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"): + response = http_controller.post("/projects/{}/links".format(project.id), { + "vms": [ + { + "vm_id": vm1.id, + "adapter_number": 0, + "port_number": 3 + }, + { + "vm_id": vm2.id, + "adapter_number": 2, + "port_number": 4 + } + ] + }, example=True) assert response.status == 201 assert response.json["link_id"] is not None assert len(response.json["vms"]) == 2