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

Create VPCS VM on controller

This commit is contained in:
Julien Duponchelle 2016-03-11 15:06:14 +01:00
parent 4326d412f9
commit be4aa41dda
No known key found for this signature in database
GPG Key ID: F1E2485547D4595D
13 changed files with 76 additions and 34 deletions

View File

@ -90,9 +90,9 @@ class Hypervisor:
data = data.__json__()
data = json.dumps(data)
response = yield from session.request(method, url, headers=headers, data=data)
print(response.status)
assert response.status < 300
body = yield from response.read()
if response.status >= 300:
raise aiohttp.errors.HttpProcessingError(code=response.status, message=body)
yield from response.release()
return body

View File

@ -21,7 +21,8 @@ import uuid
class VM:
def __init__(self, project, hypervisor, vm_id=None, vm_type=None, name=None, console=None, console_type="telnet", **kwargs):
def __init__(self, project, hypervisor, vm_id=None, vm_type=None, name=None, console=None, console_type="telnet", properties={}):
"""
:param project: Project of the VM
:param hypervisor: Hypervisor server where the server will run
@ -30,7 +31,7 @@ class VM:
:param name: Name of the vm
:param console: TCP port of the console
:param console_type: Type of the console (telnet, vnc, serial..)
:param kwargs: Emulator specific properties of the VM
:param properties: Emulator specific properties of the VM
"""
if vm_id is None:
@ -44,7 +45,7 @@ class VM:
self._vm_type = vm_type
self._console = console
self._console_type = console_type
self._properties = kwargs
self._properties = properties
@property
def id(self):
@ -74,6 +75,7 @@ class VM:
def create(self):
data = self._properties
data["vm_id"] = self._id
data["name"] = self._name
data["console"] = self._console
data["console_type"] = self._console_type
yield from self._hypervisor.post("/projects/{}/{}/vms".format(self._project.id, self._vm_type), data=data)
@ -81,6 +83,7 @@ class VM:
def __json__(self):
return {
"hypervisor_id": self._hypervisor.id,
"project_id": self._project.id,
"vm_id": self._id,
"vm_type": self._vm_type,
"name": self._name,
@ -88,4 +91,3 @@ class VM:
"console_type": self._console_type,
"properties": self._properties
}

View File

@ -19,4 +19,3 @@ from .hypervisor_handler import HypervisorHandler
from .project_handler import ProjectHandler
from .version_handler import VersionHandler
from .vm_handler import VMHandler

View File

@ -47,4 +47,3 @@ class VMHandler:
vm = yield from project.addVM(hypervisor, request.json.pop("vm_id", None), **request.json)
response.set_status(201)
response.json(vm)

View File

@ -112,6 +112,7 @@ class VPCSVM(BaseVM):
"vm_directory": self.working_dir,
"status": self.status,
"console": self._console,
"console_type": "telnet",
"project_id": self.project.id,
"startup_script": self.startup_script,
"startup_script_path": self.relative_startup_script,

View File

@ -70,7 +70,11 @@ VM_OBJECT_SCHEMA = {
"type": "object",
"properties": {
"hypervisor_id": {
"description": "Server identifier",
"description": "Hypervisor identifier",
"type": "string"
},
"project_id": {
"description": "Project identifier",
"type": "string"
},
"vm_id": {

View File

@ -42,6 +42,10 @@ VPCS_CREATE_SCHEMA = {
"maximum": 65535,
"type": ["integer", "null"]
},
"console_type": {
"description": "console type",
"enum": ["telnet"]
},
"startup_script": {
"description": "Content of the VPCS startup script",
"type": ["string", "null"]
@ -67,6 +71,10 @@ VPCS_UPDATE_SCHEMA = {
"maximum": 65535,
"type": ["integer", "null"]
},
"console_type": {
"description": "console type",
"enum": ["telnet"]
},
"startup_script": {
"description": "Content of the VPCS startup script",
"type": ["string", "null"]
@ -106,6 +114,10 @@ VPCS_OBJECT_SCHEMA = {
"maximum": 65535,
"type": "integer"
},
"console_type": {
"description": "console type",
"enum": ["telnet"]
},
"project_id": {
"description": "Project UUID",
"type": "string",
@ -127,5 +139,5 @@ VPCS_OBJECT_SCHEMA = {
}
},
"additionalProperties": False,
"required": ["name", "vm_id", "status", "console", "project_id", "startup_script_path", "command_line"]
"required": ["name", "vm_id", "status", "console", "console_type", "project_id", "startup_script_path", "command_line"]
}

View File

@ -18,6 +18,7 @@
import pytest
import json
import aiohttp
from unittest.mock import patch, MagicMock
from gns3server.controller.project import Project
@ -58,6 +59,15 @@ def test_hypervisor_httpQuery(hypervisor, async_run):
mock.assert_called_with("POST", "https://example.com:84/v2/hypervisor/projects", data='{"a": "b"}', headers={'content-type': 'application/json'})
def test_hypervisor_httpQueryError(hypervisor, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 409
with pytest.raises(aiohttp.errors.HttpProcessingError):
async_run(hypervisor.post("/projects", {"a": "b"}))
def test_hypervisor_httpQuery_project(hypervisor, async_run):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:

View File

@ -38,5 +38,10 @@ def test_json(tmpdir):
def test_addVM(async_run):
hypervisor = MagicMock()
project = Project()
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", startup_config="test.cfg"))
hypervisor.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id), data={'console': None, 'vm_id': vm.id, 'console_type': 'telnet', 'startup_config': 'test.cfg'})
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", properties= {"startup_config": "test.cfg"}))
hypervisor.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id),
data={'console': None,
'vm_id': vm.id,
'console_type': 'telnet',
'startup_config': 'test.cfg',
'name': 'test'})

View File

@ -38,13 +38,15 @@ def vm(hypervisor):
name="demo",
vm_id=str(uuid.uuid4()),
vm_type="vpcs",
console_type="vnc")
console_type="vnc",
properties={"startup_script": "echo test"})
return vm
def test_json(vm, hypervisor):
assert vm.__json__() == {
"hypervisor_id": hypervisor.id,
"project_id": vm.project.id,
"vm_id": vm.id,
"vm_type": vm.vm_type,
"name": "demo",
@ -66,6 +68,8 @@ def test_create(vm, hypervisor, project, async_run):
data = {
"console": None,
"console_type": "vnc",
"vm_id": vm.id
"vm_id": vm.id,
"startup_script": "echo test",
"name": "demo"
}
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)

View File

@ -47,7 +47,13 @@ def project(http_controller, async_run):
def test_create_vm(http_controller, tmpdir, project, hypervisor):
response = http_controller.post("/projects/{}/vms".format(project.id), {"name": "test", "vm_type": "vpcs", "hypervisor_id": "example.com"}, example=True)
response = http_controller.post("/projects/{}/vms".format(project.id), {
"name": "test",
"vm_type": "vpcs",
"hypervisor_id": "example.com",
"properties": {
"startup_script": "echo test"
}
}, example=True)
assert response.status == 201
assert response.json["name"] == "test"