mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Serialize NIO
This commit is contained in:
1 parent
0cdc1c3042
commit
bf6f62e629
11 files changed
+65
-26
No files matched your search
@@ -19,7 +19,7 @@
|
||||
from ..web.route import Route
|
||||
from ..schemas.vpcs import VPCS_CREATE_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_OBJECT_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_ADD_NIO_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_NIO_SCHEMA
|
||||
from ..modules.vpcs import VPCS
|
||||
|
||||
|
||||
@@ -48,7 +48,8 @@ class VPCSHandler(object):
|
||||
"vpcs_id": "Id of VPCS instance"
|
||||
},
|
||||
status_codes={
|
||||
201: "Success of creation of VPCS",
|
||||
200: "Success of starting VPCS",
|
||||
404: "If VPCS doesn't exist"
|
||||
},
|
||||
description="Start VPCS",
|
||||
)
|
||||
@@ -64,7 +65,8 @@ class VPCSHandler(object):
|
||||
"vpcs_id": "Id of VPCS instance"
|
||||
},
|
||||
status_codes={
|
||||
201: "Success of stopping VPCS",
|
||||
200: "Success of stopping VPCS",
|
||||
404: "If VPCS doesn't exist"
|
||||
},
|
||||
description="Stop VPCS",
|
||||
)
|
||||
@@ -81,17 +83,17 @@ class VPCSHandler(object):
|
||||
},
|
||||
status_codes={
|
||||
201: "Success of creation of NIO",
|
||||
409: "Conflict"
|
||||
404: "If VPCS doesn't exist"
|
||||
},
|
||||
description="ADD NIO to a VPCS",
|
||||
input=VPCS_ADD_NIO_SCHEMA)
|
||||
input=VPCS_NIO_SCHEMA,
|
||||
output=VPCS_NIO_SCHEMA)
|
||||
def create_nio(request, response):
|
||||
# TODO: raise 404 if VPCS not found GET VM can raise an exeption
|
||||
# TODO: response with nio
|
||||
vpcs_manager = VPCS.instance()
|
||||
vm = vpcs_manager.get_vm(int(request.match_info['vpcs_id']))
|
||||
vm.port_add_nio_binding(int(request.match_info['port_id']), request.json)
|
||||
nio = vm.port_add_nio_binding(int(request.match_info['port_id']), request.json)
|
||||
|
||||
response.json({'name': "PC 2", "vpcs_id": 42, "console": 4242})
|
||||
response.json(nio)
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ class BaseManager:
|
||||
:returns: instance of Manager
|
||||
"""
|
||||
|
||||
if not hasattr(cls, "_instance"):
|
||||
if not hasattr(cls, "_instance") or cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
|
||||
@@ -44,3 +44,6 @@ class NIO_TAP(object):
|
||||
def __str__(self):
|
||||
|
||||
return "NIO TAP"
|
||||
|
||||
def __json__(self):
|
||||
return {"type": "nio_tap", "tap_device": self._tap_device}
|
||||
@@ -70,3 +70,6 @@ class NIO_UDP(object):
|
||||
def __str__(self):
|
||||
|
||||
return "NIO UDP"
|
||||
|
||||
def __json__(self):
|
||||
return {"type": "nio_udp", "lport": self._lport, "rport": self._rport, "rhost": self._rhost}
|
||||
@@ -265,7 +265,6 @@ class VPCSDevice(BaseVM):
|
||||
nio = NIO_UDP(lport, rhost, rport)
|
||||
elif nio_settings["type"] == "nio_tap":
|
||||
tap_device = nio_settings["tap_device"]
|
||||
print(has_privileged_access)
|
||||
if not has_privileged_access(self._path):
|
||||
raise VPCSError("{} has no privileged access to {}.".format(self._path, tap_device))
|
||||
nio = NIO_TAP(tap_device)
|
||||
|
||||
@@ -42,7 +42,7 @@ VPCS_CREATE_SCHEMA = {
|
||||
}
|
||||
|
||||
|
||||
VPCS_ADD_NIO_SCHEMA = {
|
||||
VPCS_NIO_SCHEMA = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "Request validation to add a NIO for a VPCS instance",
|
||||
"type": "object",
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
import json
|
||||
import jsonschema
|
||||
import aiohttp.web
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class Response(aiohttp.web.Response):
|
||||
|
||||
@@ -29,13 +31,22 @@ class Response(aiohttp.web.Response):
|
||||
headers['X-Route'] = self._route
|
||||
super().__init__(headers=headers, **kwargs)
|
||||
|
||||
"""
|
||||
Set the response content type to application/json and serialize
|
||||
the content.
|
||||
|
||||
:param anwser The response as a Python object
|
||||
"""
|
||||
def json(self, answer):
|
||||
"""Pass a Python object and return a JSON as answer"""
|
||||
|
||||
self.content_type = "application/json"
|
||||
if hasattr(answer, '__json__'):
|
||||
answer = answer.__json__()
|
||||
if self._output_schema is not None:
|
||||
try:
|
||||
jsonschema.validate(answer, self._output_schema)
|
||||
except jsonschema.ValidationError as e:
|
||||
log.error("Invalid output schema")
|
||||
raise aiohttp.web.HTTPBadRequest(text="{}".format(e))
|
||||
self.body = json.dumps(answer, indent=4, sort_keys=True).encode('utf-8')
|
||||
@@ -19,6 +19,9 @@ import json
|
||||
import jsonschema
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from ..modules.vm_error import VMError
|
||||
from .response import Response
|
||||
@@ -37,6 +40,7 @@ def parse_request(request, input_schema):
|
||||
try:
|
||||
jsonschema.validate(request.json, input_schema)
|
||||
except jsonschema.ValidationError as e:
|
||||
log.error("Invalid input schema")
|
||||
raise aiohttp.web.HTTPBadRequest(text="Request is not {} '{}' in schema: {}".format(
|
||||
e.validator,
|
||||
e.validator_value,
|
||||
|
||||
Reference in new issue
Block a user