mirror of
https://github.com/GNS3/gns3-server
synced 2025-02-15 17:42:01 +00:00
/start /stop and /suspend API for VM via container
This commit is contained in:
parent
ad7ca86f32
commit
2055cdea5e
@ -110,6 +110,27 @@ class VM:
|
|||||||
else:
|
else:
|
||||||
self._properties[key] = value
|
self._properties[key] = value
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def start(self):
|
||||||
|
"""
|
||||||
|
Start a VM
|
||||||
|
"""
|
||||||
|
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/start".format(self._project.id, self._vm_type, self._id))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def stop(self):
|
||||||
|
"""
|
||||||
|
Stop a VM
|
||||||
|
"""
|
||||||
|
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/stop".format(self._project.id, self._vm_type, self._id))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def suspend(self):
|
||||||
|
"""
|
||||||
|
Suspend a VM
|
||||||
|
"""
|
||||||
|
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/suspend".format(self._project.id, self._vm_type, self._id))
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def post(self, path, data={}):
|
def post(self, path, data={}):
|
||||||
"""
|
"""
|
||||||
|
@ -47,3 +47,66 @@ class VMHandler:
|
|||||||
vm = yield from project.addVM(hypervisor, request.json.pop("vm_id", None), **request.json)
|
vm = yield from project.addVM(hypervisor, request.json.pop("vm_id", None), **request.json)
|
||||||
response.set_status(201)
|
response.set_status(201)
|
||||||
response.json(vm)
|
response.json(vm)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@Route.post(
|
||||||
|
r"/projects/{project_id}/vms/{vm_id}/start",
|
||||||
|
parameters={
|
||||||
|
"project_id": "UUID for the project",
|
||||||
|
"vm_id": "UUID for the VM"
|
||||||
|
},
|
||||||
|
status_codes={
|
||||||
|
201: "Instance created",
|
||||||
|
400: "Invalid request"
|
||||||
|
},
|
||||||
|
description="Start a VM instance",
|
||||||
|
output=VM_OBJECT_SCHEMA)
|
||||||
|
def start(request, response):
|
||||||
|
|
||||||
|
project = Controller.instance().getProject(request.match_info["project_id"])
|
||||||
|
vm = project.getVM(request.match_info["vm_id"])
|
||||||
|
yield from vm.start()
|
||||||
|
response.set_status(201)
|
||||||
|
response.json(vm)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@Route.post(
|
||||||
|
r"/projects/{project_id}/vms/{vm_id}/stop",
|
||||||
|
parameters={
|
||||||
|
"project_id": "UUID for the project",
|
||||||
|
"vm_id": "UUID for the VM"
|
||||||
|
},
|
||||||
|
status_codes={
|
||||||
|
201: "Instance created",
|
||||||
|
400: "Invalid request"
|
||||||
|
},
|
||||||
|
description="Start a VM instance",
|
||||||
|
output=VM_OBJECT_SCHEMA)
|
||||||
|
def stop(request, response):
|
||||||
|
|
||||||
|
project = Controller.instance().getProject(request.match_info["project_id"])
|
||||||
|
vm = project.getVM(request.match_info["vm_id"])
|
||||||
|
yield from vm.stop()
|
||||||
|
response.set_status(201)
|
||||||
|
response.json(vm)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@Route.post(
|
||||||
|
r"/projects/{project_id}/vms/{vm_id}/suspend",
|
||||||
|
parameters={
|
||||||
|
"project_id": "UUID for the project",
|
||||||
|
"vm_id": "UUID for the VM"
|
||||||
|
},
|
||||||
|
status_codes={
|
||||||
|
201: "Instance created",
|
||||||
|
400: "Invalid request"
|
||||||
|
},
|
||||||
|
description="Start a VM instance",
|
||||||
|
output=VM_OBJECT_SCHEMA)
|
||||||
|
def suspend(request, response):
|
||||||
|
|
||||||
|
project = Controller.instance().getProject(request.match_info["project_id"])
|
||||||
|
vm = project.getVM(request.match_info["vm_id"])
|
||||||
|
yield from vm.suspend()
|
||||||
|
response.set_status(201)
|
||||||
|
response.json(vm)
|
||||||
|
@ -86,6 +86,30 @@ def test_create(vm, hypervisor, project, async_run):
|
|||||||
assert vm._properties == {"startup_script": "echo test"}
|
assert vm._properties == {"startup_script": "echo test"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_start(vm, hypervisor, project, async_run):
|
||||||
|
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
async_run(vm.start())
|
||||||
|
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/start".format(vm.project.id, vm.id))
|
||||||
|
|
||||||
|
|
||||||
|
def test_stop(vm, hypervisor, project, async_run):
|
||||||
|
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
async_run(vm.stop())
|
||||||
|
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/stop".format(vm.project.id, vm.id))
|
||||||
|
|
||||||
|
|
||||||
|
def test_suspend(vm, hypervisor, project, async_run):
|
||||||
|
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
async_run(vm.suspend())
|
||||||
|
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/suspend".format(vm.project.id, vm.id))
|
||||||
|
|
||||||
|
|
||||||
def test_create_without_console(vm, hypervisor, project, async_run):
|
def test_create_without_console(vm, hypervisor, project, async_run):
|
||||||
"""
|
"""
|
||||||
None properties should be send. Because it can mean the emulator doesn"t support it
|
None properties should be send. Because it can mean the emulator doesn"t support it
|
||||||
|
@ -31,6 +31,7 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
|
|||||||
|
|
||||||
from gns3server.handlers.api.controller.project_handler import ProjectHandler
|
from gns3server.handlers.api.controller.project_handler import ProjectHandler
|
||||||
from gns3server.controller import Controller
|
from gns3server.controller import Controller
|
||||||
|
from gns3server.controller.vm import VM
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -46,6 +47,13 @@ def project(http_controller, async_run):
|
|||||||
return async_run(Controller.instance().addProject())
|
return async_run(Controller.instance().addProject())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def vm(project, hypervisor, async_run):
|
||||||
|
vm = VM(project, hypervisor, name="test", vm_type="vpcs")
|
||||||
|
project._vms[vm.id] = vm
|
||||||
|
return vm
|
||||||
|
|
||||||
|
|
||||||
def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.json = {"console": 2048}
|
response.json = {"console": 2048}
|
||||||
@ -62,3 +70,30 @@ def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
|||||||
assert response.status == 201
|
assert response.status == 201
|
||||||
assert response.json["name"] == "test"
|
assert response.json["name"] == "test"
|
||||||
assert "name" not in response.json["properties"]
|
assert "name" not in response.json["properties"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||||
|
response = MagicMock()
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
response = http_controller.post("/projects/{}/vms/{}/start".format(project.id, vm.id), example=True)
|
||||||
|
assert response.status == 201
|
||||||
|
assert response.json["name"] == vm.name
|
||||||
|
|
||||||
|
|
||||||
|
def test_stop_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||||
|
response = MagicMock()
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
response = http_controller.post("/projects/{}/vms/{}/stop".format(project.id, vm.id), example=True)
|
||||||
|
assert response.status == 201
|
||||||
|
assert response.json["name"] == vm.name
|
||||||
|
|
||||||
|
|
||||||
|
def test_suspend_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||||
|
response = MagicMock()
|
||||||
|
hypervisor.post = AsyncioMagicMock()
|
||||||
|
|
||||||
|
response = http_controller.post("/projects/{}/vms/{}/suspend".format(project.id, vm.id), example=True)
|
||||||
|
assert response.status == 201
|
||||||
|
assert response.json["name"] == vm.name
|
||||||
|
@ -22,7 +22,7 @@ import pytest
|
|||||||
def test_udp_allocation(http_hypervisor, project):
|
def test_udp_allocation(http_hypervisor, project):
|
||||||
response = http_hypervisor.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
|
response = http_hypervisor.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
|
||||||
assert response.status == 201
|
assert response.status == 201
|
||||||
assert response.json == {'udp_port': 10000}
|
assert response.json['udp_port'] is not None
|
||||||
|
|
||||||
|
|
||||||
# Netfifaces is not available on Travis
|
# Netfifaces is not available on Travis
|
||||||
|
Loading…
Reference in New Issue
Block a user