1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-30 20:28:08 +00:00

Support forwarding POST request to compute nodes

This commit is contained in:
Julien Duponchelle 2016-06-06 15:45:32 +02:00
parent 61ac8763f2
commit 06b54d82dc
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
4 changed files with 51 additions and 8 deletions

View File

@ -324,9 +324,9 @@ class Compute:
return (yield from self.http_query("DELETE", path, **kwargs))
@asyncio.coroutine
def forward(self, type, path):
def forward(self, method, type, path, data=None):
"""
Forward a call to the emulator on compute
"""
res = yield from self.get("/{}/{}".format(type, path))
res = yield from self.http_query(method, "/{}/{}".format(type, path), data=data)
return res.json

View File

@ -87,10 +87,26 @@ class ComputeHandler:
404: "Instance doesn't exist"
},
description="Forward call specific to compute node. Read the full compute API for available actions")
def forward(request, response):
def get_forward(request, response):
controller = Controller.instance()
compute = controller.get_compute(request.match_info["compute_id"])
images = yield from compute.forward(request.match_info["emulator"], request.match_info["action"])
images = yield from compute.forward("GET", request.match_info["emulator"], request.match_info["action"])
response.json(images)
@Route.post(
r"/computes/{compute_id}/{emulator}/{action}",
parameters={
"compute_id": "Compute UUID"
},
status_codes={
200: "OK",
404: "Instance doesn't exist"
},
description="Forward call specific to compute node. Read the full compute API for available actions")
def post_forward(request, response):
controller = Controller.instance()
compute = controller.get_compute(request.match_info["compute_id"])
images = yield from compute.forward("POST", request.match_info["emulator"], request.match_info["action"], data=dict(request.json))
response.json(images)
@Route.get(

View File

@ -223,9 +223,17 @@ def test_update(compute, controller, async_run):
assert compute.connected is False
def test_forward(compute, async_run):
def test_forward_get(compute, async_run):
response = MagicMock()
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
async_run(compute.forward("qemu", "images"))
async_run(compute.forward("GET", "qemu", "images"))
mock.assert_called_with("GET", "https://example.com:84/v2/compute/qemu/images", auth=None, data=None, headers={'content-type': 'application/json'})
def test_forward_post(compute, async_run):
response = MagicMock()
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
async_run(compute.forward("POST", "qemu", "img", data={"id": 42}))
mock.assert_called_with("POST", "https://example.com:84/v2/compute/qemu/img", auth=None, data='{"id": 42}', headers={'content-type': 'application/json'})

View File

@ -171,7 +171,7 @@ def test_compute_list_images(http_controller, controller):
with asyncio_patch("gns3server.controller.compute.Compute.forward", return_value=[]) as mock:
response = http_controller.get("/computes/my_compute/qemu/images")
assert response.json == []
mock.assert_called_with("qemu", "images")
mock.assert_called_with("GET", "qemu", "images")
def test_compute_list_vms(http_controller, controller):
@ -190,4 +190,23 @@ def test_compute_list_vms(http_controller, controller):
with asyncio_patch("gns3server.controller.compute.Compute.forward", return_value=[]) as mock:
response = http_controller.get("/computes/my_compute/virtualbox/vms")
assert response.json == []
mock.assert_called_with("virtualbox", "vms")
mock.assert_called_with("GET", "virtualbox", "vms")
def test_compute_create_img(http_controller, controller):
params = {
"compute_id": "my_compute",
"protocol": "http",
"host": "example.com",
"port": 84,
"user": "julien",
"password": "secure"
}
response = http_controller.post("/computes", params)
assert response.status == 201
params = {"path": "/test"}
with asyncio_patch("gns3server.controller.compute.Compute.forward", return_value=[]) as mock:
response = http_controller.post("/computes/my_compute/qemu/img", params)
mock.assert_called_with("POST", "qemu", "img", data={"path": "/test"})