Merge pull request #1106 from GNS3/idlepc_apicall

Implement an api call for computing the IDLE PC
This commit is contained in:
Jeremy Grossmann 2017-07-10 11:33:16 +07:00 committed by GitHub
commit 339ed8ab57
5 changed files with 72 additions and 0 deletions

View File

@ -597,3 +597,24 @@ class Controller:
if not hasattr(Controller, '_instance') or Controller._instance is None:
Controller._instance = Controller()
return Controller._instance
@asyncio.coroutine
def autoidlepc(self, compute_id, platform, image):
"""
Compute and IDLE PC value for an image
:param compute_id: ID of the compute where the idlepc operation need to run
:param platform: Platform type
:param image: Image to use
"""
compute = self.get_compute(compute_id)
for project in list(self._projects.values()):
if project.name == "AUTOIDLEPC":
yield from project.delete()
self.remove_project(project)
project = yield from self.add_project(name="AUTOIDLEPC")
node = yield from project.add_node(compute, "AUTOIDLEPC", str(uuid.uuid4()), node_type="dynamips", platform=platform, image=image, ram=512)
res = yield from node.dynamips_auto_idlepc()
yield from project.delete()
self.remove_project(project)
return res

View File

@ -665,3 +665,4 @@ class Compute:
return (this_interface["ip_address"], other_interface["ip_address"])
raise ValueError("No common subnet for compute {} and {}".format(self.name, other_compute.name))

View File

@ -154,3 +154,18 @@ class ComputeHandler:
controller = Controller.instance()
yield from controller.delete_compute(request.match_info["compute_id"])
response.set_status(204)
@Route.post(
r"/computes/{compute_id}/autoidlepc",
parameters={
"compute_id": "Compute UUID"
},
status_codes={
200: "Idle PC computed",
},
description="Compute IDLE PC value")
def autoidlepc(request, response):
controller = Controller.instance()
res = yield from controller.autoidlepc(request.match_info["compute_id"], request.json["platform"], request.json["image"])
response.set_status(200)
response.json(res)

View File

@ -517,3 +517,13 @@ def test_load_appliances(controller):
assert qemu_uuid == appliance.id
elif appliance.name == "Cloud":
assert cloud_uuid == appliance.id
def test_autoidlepc(controller, async_run):
controller._computes["local"] = AsyncioMagicMock()
node_mock = AsyncioMagicMock()
with asyncio_patch("gns3server.controller.Project.add_node", return_value=node_mock):
async_run(controller.autoidlepc("local", "c7200", "test.bin"))
assert node_mock.dynamips_auto_idlepc.called
assert len(controller.projects) == 0

View File

@ -216,3 +216,28 @@ def test_compute_create_img(http_controller, controller):
with asyncio_patch("gns3server.controller.compute.Compute.forward", return_value=[]) as mock:
response = http_controller.post("/computes/my_compute/qemu/img", params, example=True)
mock.assert_called_with("POST", "qemu", "img", data=unittest.mock.ANY)
def test_compute_autoidlepc(http_controller, controller):
params = {
"compute_id": "my_compute_id",
"protocol": "http",
"host": "localhost",
"port": 84,
"user": "julien",
"password": "secure"
}
response = http_controller.post("/computes", params, example=False)
params = {
"platform": "c7200",
"image": "test.bin"
}
with asyncio_patch("gns3server.controller.Controller.autoidlepc", return_value={"idlepc": "0x606de20c"}) as mock:
response = http_controller.post("/computes/my_compute_id/autoidlepc", params, example=True)
assert mock.called
assert response.status == 200