From b6a935aeb8ceeb240518c6a072c9b16c881e9a07 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 27 Apr 2015 14:19:17 -0600 Subject: [PATCH] Return an explicit error when a NIO type is not supported by a VM. --- .../handlers/api/dynamips_vm_handler.py | 6 +- gns3server/handlers/api/iou_handler.py | 9 +- gns3server/handlers/api/qemu_handler.py | 10 +- gns3server/handlers/api/virtualbox_handler.py | 10 +- gns3server/handlers/api/vpcs_handler.py | 10 +- gns3server/schemas/dynamips_vm.py | 141 ---------------- gns3server/schemas/iou.py | 72 -------- gns3server/schemas/nio.py | 158 ++++++++++++++++++ gns3server/schemas/qemu.py | 56 ------- gns3server/schemas/virtualbox.py | 40 ----- gns3server/schemas/vpcs.py | 56 ------- 11 files changed, 188 insertions(+), 380 deletions(-) create mode 100644 gns3server/schemas/nio.py diff --git a/gns3server/handlers/api/dynamips_vm_handler.py b/gns3server/handlers/api/dynamips_vm_handler.py index 8d96f98b..ff3c3707 100644 --- a/gns3server/handlers/api/dynamips_vm_handler.py +++ b/gns3server/handlers/api/dynamips_vm_handler.py @@ -19,11 +19,11 @@ import os import base64 from ...web.route import Route +from ...schemas.nio import NIO_SCHEMA from ...schemas.dynamips_vm import VM_CREATE_SCHEMA from ...schemas.dynamips_vm import VM_UPDATE_SCHEMA from ...schemas.dynamips_vm import VM_CAPTURE_SCHEMA from ...schemas.dynamips_vm import VM_OBJECT_SCHEMA -from ...schemas.dynamips_vm import VM_NIO_SCHEMA from ...schemas.dynamips_vm import VM_CONFIGS_SCHEMA from ...modules.dynamips import Dynamips from ...modules.project_manager import ProjectManager @@ -256,8 +256,8 @@ class DynamipsVMHandler: 404: "Instance doesn't exist" }, description="Add a NIO to a Dynamips VM instance", - input=VM_NIO_SCHEMA, - output=VM_NIO_SCHEMA) + input=NIO_SCHEMA, + output=NIO_SCHEMA) def create_nio(request, response): dynamips_manager = Dynamips.instance() diff --git a/gns3server/handlers/api/iou_handler.py b/gns3server/handlers/api/iou_handler.py index 3314e580..b8378bb8 100644 --- a/gns3server/handlers/api/iou_handler.py +++ b/gns3server/handlers/api/iou_handler.py @@ -19,10 +19,10 @@ import os from aiohttp.web import HTTPConflict from ...web.route import Route +from ...schemas.nio import NIO_SCHEMA from ...schemas.iou import IOU_CREATE_SCHEMA from ...schemas.iou import IOU_UPDATE_SCHEMA from ...schemas.iou import IOU_OBJECT_SCHEMA -from ...schemas.iou import IOU_NIO_SCHEMA from ...schemas.iou import IOU_CAPTURE_SCHEMA from ...schemas.iou import IOU_INITIAL_CONFIG_SCHEMA from ...modules.iou import IOU @@ -200,12 +200,15 @@ class IOUHandler: 404: "Instance doesn't exist" }, description="Add a NIO to a IOU instance", - input=IOU_NIO_SCHEMA, - output=IOU_NIO_SCHEMA) + input=NIO_SCHEMA, + output=NIO_SCHEMA) def create_nio(request, response): iou_manager = IOU.instance() vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) + nio_type = request.json["type"] + if nio_type not in ("nio_udp", "nio_tap", "nio_generic_ethernet"): + raise HTTPConflict(text="NIO of type {} is not supported".format(nio_type)) nio = iou_manager.create_nio(vm.iouyap_path, request.json) vm.adapter_add_nio_binding(int(request.match_info["adapter_number"]), int(request.match_info["port_number"]), nio) response.set_status(201) diff --git a/gns3server/handlers/api/qemu_handler.py b/gns3server/handlers/api/qemu_handler.py index 714149d6..704b02d7 100644 --- a/gns3server/handlers/api/qemu_handler.py +++ b/gns3server/handlers/api/qemu_handler.py @@ -15,11 +15,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from aiohttp.web import HTTPConflict from ...web.route import Route +from ...schemas.nio import NIO_SCHEMA from ...schemas.qemu import QEMU_CREATE_SCHEMA from ...schemas.qemu import QEMU_UPDATE_SCHEMA from ...schemas.qemu import QEMU_OBJECT_SCHEMA -from ...schemas.qemu import QEMU_NIO_SCHEMA from ...schemas.qemu import QEMU_BINARY_LIST_SCHEMA from ...modules.qemu import Qemu @@ -239,12 +240,15 @@ class QEMUHandler: 404: "Instance doesn't exist" }, description="Add a NIO to a Qemu VM instance", - input=QEMU_NIO_SCHEMA, - output=QEMU_NIO_SCHEMA) + input=NIO_SCHEMA, + output=NIO_SCHEMA) def create_nio(request, response): qemu_manager = Qemu.instance() vm = qemu_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) + nio_type = request.json["type"] + if nio_type != "nio_udp": + raise HTTPConflict(text="NIO of type {} is not supported".format(nio_type)) nio = qemu_manager.create_nio(vm.qemu_path, request.json) yield from vm.adapter_add_nio_binding(int(request.match_info["adapter_number"]), nio) response.set_status(201) diff --git a/gns3server/handlers/api/virtualbox_handler.py b/gns3server/handlers/api/virtualbox_handler.py index 008ac2e6..58160cb4 100644 --- a/gns3server/handlers/api/virtualbox_handler.py +++ b/gns3server/handlers/api/virtualbox_handler.py @@ -16,10 +16,11 @@ # along with this program. If not, see . import os +from aiohttp.web import HTTPConflict from ...web.route import Route +from ...schemas.nio import NIO_SCHEMA from ...schemas.virtualbox import VBOX_CREATE_SCHEMA from ...schemas.virtualbox import VBOX_UPDATE_SCHEMA -from ...schemas.virtualbox import VBOX_NIO_SCHEMA from ...schemas.virtualbox import VBOX_CAPTURE_SCHEMA from ...schemas.virtualbox import VBOX_OBJECT_SCHEMA from ...modules.virtualbox import VirtualBox @@ -286,12 +287,15 @@ class VirtualBoxHandler: 404: "Instance doesn't exist" }, description="Add a NIO to a VirtualBox VM instance", - input=VBOX_NIO_SCHEMA, - output=VBOX_NIO_SCHEMA) + input=NIO_SCHEMA, + output=NIO_SCHEMA) def create_nio(request, response): vbox_manager = VirtualBox.instance() vm = vbox_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) + nio_type = request.json["type"] + if nio_type != "nio_udp": + raise HTTPConflict(text="NIO of type {} is not supported".format(nio_type)) nio = vbox_manager.create_nio(vbox_manager.vboxmanage_path, request.json) yield from vm.adapter_add_nio_binding(int(request.match_info["adapter_number"]), nio) response.set_status(201) diff --git a/gns3server/handlers/api/vpcs_handler.py b/gns3server/handlers/api/vpcs_handler.py index 588ff50c..ad22ac2b 100644 --- a/gns3server/handlers/api/vpcs_handler.py +++ b/gns3server/handlers/api/vpcs_handler.py @@ -15,11 +15,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from aiohttp.web import HTTPConflict from ...web.route import Route +from ...schemas.nio import NIO_SCHEMA from ...schemas.vpcs import VPCS_CREATE_SCHEMA from ...schemas.vpcs import VPCS_UPDATE_SCHEMA from ...schemas.vpcs import VPCS_OBJECT_SCHEMA -from ...schemas.vpcs import VPCS_NIO_SCHEMA from ...modules.vpcs import VPCS @@ -191,12 +192,15 @@ class VPCSHandler: 404: "Instance doesn't exist" }, description="Add a NIO to a VPCS instance", - input=VPCS_NIO_SCHEMA, - output=VPCS_NIO_SCHEMA) + input=NIO_SCHEMA, + output=NIO_SCHEMA) def create_nio(request, response): vpcs_manager = VPCS.instance() vm = vpcs_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) + nio_type = request.json["type"] + if nio_type not in ("nio_udp", "nio_tap"): + raise HTTPConflict(text="NIO of type {} is not supported".format(nio_type)) nio = vpcs_manager.create_nio(vm.vpcs_path, request.json) vm.port_add_nio_binding(int(request.match_info["port_number"]), nio) response.set_status(201) diff --git a/gns3server/schemas/dynamips_vm.py b/gns3server/schemas/dynamips_vm.py index 8888f6a6..25efd419 100644 --- a/gns3server/schemas/dynamips_vm.py +++ b/gns3server/schemas/dynamips_vm.py @@ -473,147 +473,6 @@ VM_UPDATE_SCHEMA = { "additionalProperties": False, } -VM_NIO_SCHEMA = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to add a NIO for a Dynamips VM instance", - "type": "object", - "definitions": { - "UDP": { - "description": "UDP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_udp"] - }, - "lport": { - "description": "Local port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - }, - "rhost": { - "description": "Remote host", - "type": "string", - "minLength": 1 - }, - "rport": { - "description": "Remote port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - } - }, - "required": ["type", "lport", "rhost", "rport"], - "additionalProperties": False - }, - "Ethernet": { - "description": "Generic Ethernet Network Input/Output", - "properties": { - "type": { - "enum": ["nio_generic_ethernet"] - }, - "ethernet_device": { - "description": "Ethernet device name e.g. eth0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "ethernet_device"], - "additionalProperties": False - }, - "LinuxEthernet": { - "description": "Linux Ethernet Network Input/Output", - "properties": { - "type": { - "enum": ["nio_linux_ethernet"] - }, - "ethernet_device": { - "description": "Ethernet device name e.g. eth0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "ethernet_device"], - "additionalProperties": False - }, - "TAP": { - "description": "TAP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_tap"] - }, - "tap_device": { - "description": "TAP device name e.g. tap0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "tap_device"], - "additionalProperties": False - }, - "UNIX": { - "description": "UNIX Network Input/Output", - "properties": { - "type": { - "enum": ["nio_unix"] - }, - "local_file": { - "description": "path to the UNIX socket file (local)", - "type": "string", - "minLength": 1 - }, - "remote_file": { - "description": "path to the UNIX socket file (remote)", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "local_file", "remote_file"], - "additionalProperties": False - }, - "VDE": { - "description": "VDE Network Input/Output", - "properties": { - "type": { - "enum": ["nio_vde"] - }, - "control_file": { - "description": "path to the VDE control file", - "type": "string", - "minLength": 1 - }, - "local_file": { - "description": "path to the VDE control file", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "control_file", "local_file"], - "additionalProperties": False - }, - "NULL": { - "description": "NULL Network Input/Output", - "properties": { - "type": { - "enum": ["nio_null"] - }, - }, - "required": ["type"], - "additionalProperties": False - }, - }, - "oneOf": [ - {"$ref": "#/definitions/UDP"}, - {"$ref": "#/definitions/Ethernet"}, - {"$ref": "#/definitions/LinuxEthernet"}, - {"$ref": "#/definitions/TAP"}, - {"$ref": "#/definitions/UNIX"}, - {"$ref": "#/definitions/VDE"}, - {"$ref": "#/definitions/NULL"}, - ], - "additionalProperties": True, - "required": ["type"] -} - VM_CAPTURE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Request validation to start a packet capture on a Dynamips VM instance port", diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 011607ba..40420994 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -214,78 +214,6 @@ IOU_OBJECT_SCHEMA = { "required": ["name", "vm_id", "console", "project_id", "path", "serial_adapters", "ethernet_adapters", "ram", "nvram", "l1_keepalives", "initial_config", "use_default_iou_values"] } -IOU_NIO_SCHEMA = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to add a NIO for a IOU instance", - "type": "object", - "definitions": { - "UDP": { - "description": "UDP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_udp"] - }, - "lport": { - "description": "Local port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - }, - "rhost": { - "description": "Remote host", - "type": "string", - "minLength": 1 - }, - "rport": { - "description": "Remote port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - } - }, - "required": ["type", "lport", "rhost", "rport"], - "additionalProperties": False - }, - "Ethernet": { - "description": "Generic Ethernet Network Input/Output", - "properties": { - "type": { - "enum": ["nio_generic_ethernet"] - }, - "ethernet_device": { - "description": "Ethernet device name e.g. eth0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "ethernet_device"], - "additionalProperties": False - }, - "TAP": { - "description": "TAP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_tap"] - }, - "tap_device": { - "description": "TAP device name e.g. tap0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "tap_device"], - "additionalProperties": False - }, - }, - "oneOf": [ - {"$ref": "#/definitions/UDP"}, - {"$ref": "#/definitions/Ethernet"}, - {"$ref": "#/definitions/TAP"}, - ], - "additionalProperties": True, - "required": ["type"] -} - IOU_CAPTURE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Request validation to start a packet capture on a IOU instance", diff --git a/gns3server/schemas/nio.py b/gns3server/schemas/nio.py new file mode 100644 index 00000000..52a97c66 --- /dev/null +++ b/gns3server/schemas/nio.py @@ -0,0 +1,158 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2015 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +NIO_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Request validation to add a NIO for a VM instance", + "type": "object", + "definitions": { + "UDP": { + "description": "UDP Network Input/Output", + "properties": { + "type": { + "enum": ["nio_udp"] + }, + "lport": { + "description": "Local port", + "type": "integer", + "minimum": 1, + "maximum": 65535 + }, + "rhost": { + "description": "Remote host", + "type": "string", + "minLength": 1 + }, + "rport": { + "description": "Remote port", + "type": "integer", + "minimum": 1, + "maximum": 65535 + } + }, + "required": ["type", "lport", "rhost", "rport"], + "additionalProperties": False + }, + "Ethernet": { + "description": "Generic Ethernet Network Input/Output", + "properties": { + "type": { + "enum": ["nio_generic_ethernet"] + }, + "ethernet_device": { + "description": "Ethernet device name e.g. eth0", + "type": "string", + "minLength": 1 + }, + }, + "required": ["type", "ethernet_device"], + "additionalProperties": False + }, + "LinuxEthernet": { + "description": "Linux Ethernet Network Input/Output", + "properties": { + "type": { + "enum": ["nio_linux_ethernet"] + }, + "ethernet_device": { + "description": "Ethernet device name e.g. eth0", + "type": "string", + "minLength": 1 + }, + }, + "required": ["type", "ethernet_device"], + "additionalProperties": False + }, + "TAP": { + "description": "TAP Network Input/Output", + "properties": { + "type": { + "enum": ["nio_tap"] + }, + "tap_device": { + "description": "TAP device name e.g. tap0", + "type": "string", + "minLength": 1 + }, + }, + "required": ["type", "tap_device"], + "additionalProperties": False + }, + "UNIX": { + "description": "UNIX Network Input/Output", + "properties": { + "type": { + "enum": ["nio_unix"] + }, + "local_file": { + "description": "path to the UNIX socket file (local)", + "type": "string", + "minLength": 1 + }, + "remote_file": { + "description": "path to the UNIX socket file (remote)", + "type": "string", + "minLength": 1 + }, + }, + "required": ["type", "local_file", "remote_file"], + "additionalProperties": False + }, + "VDE": { + "description": "VDE Network Input/Output", + "properties": { + "type": { + "enum": ["nio_vde"] + }, + "control_file": { + "description": "path to the VDE control file", + "type": "string", + "minLength": 1 + }, + "local_file": { + "description": "path to the VDE control file", + "type": "string", + "minLength": 1 + }, + }, + "required": ["type", "control_file", "local_file"], + "additionalProperties": False + }, + "NULL": { + "description": "NULL Network Input/Output", + "properties": { + "type": { + "enum": ["nio_null"] + }, + }, + "required": ["type"], + "additionalProperties": False + }, + }, + "oneOf": [ + {"$ref": "#/definitions/UDP"}, + {"$ref": "#/definitions/Ethernet"}, + {"$ref": "#/definitions/LinuxEthernet"}, + {"$ref": "#/definitions/TAP"}, + {"$ref": "#/definitions/UNIX"}, + {"$ref": "#/definitions/VDE"}, + {"$ref": "#/definitions/NULL"}, + ], + "additionalProperties": True, + "required": ["type"] +} diff --git a/gns3server/schemas/qemu.py b/gns3server/schemas/qemu.py index dfa45a07..d8a5005b 100644 --- a/gns3server/schemas/qemu.py +++ b/gns3server/schemas/qemu.py @@ -211,62 +211,6 @@ QEMU_UPDATE_SCHEMA = { "additionalProperties": False, } -QEMU_NIO_SCHEMA = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to add a NIO for a QEMU instance", - "type": "object", - "definitions": { - "UDP": { - "description": "UDP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_udp"] - }, - "lport": { - "description": "Local port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - }, - "rhost": { - "description": "Remote host", - "type": "string", - "minLength": 1 - }, - "rport": { - "description": "Remote port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - } - }, - "required": ["type", "lport", "rhost", "rport"], - "additionalProperties": False - }, - "Ethernet": { - "description": "Generic Ethernet Network Input/Output", - "properties": { - "type": { - "enum": ["nio_generic_ethernet"] - }, - "ethernet_device": { - "description": "Ethernet device name e.g. eth0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "ethernet_device"], - "additionalProperties": False - }, - }, - "oneOf": [ - {"$ref": "#/definitions/UDP"}, - {"$ref": "#/definitions/Ethernet"}, - ], - "additionalProperties": True, - "required": ["type"] -} - QEMU_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Request validation for a QEMU VM instance", diff --git a/gns3server/schemas/virtualbox.py b/gns3server/schemas/virtualbox.py index 930cbbbe..8e57c35c 100644 --- a/gns3server/schemas/virtualbox.py +++ b/gns3server/schemas/virtualbox.py @@ -139,46 +139,6 @@ VBOX_UPDATE_SCHEMA = { "additionalProperties": False, } -VBOX_NIO_SCHEMA = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to add a NIO for a VirtualBox VM instance", - "type": "object", - "definitions": { - "UDP": { - "description": "UDP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_udp"] - }, - "lport": { - "description": "Local port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - }, - "rhost": { - "description": "Remote host", - "type": "string", - "minLength": 1 - }, - "rport": { - "description": "Remote port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - } - }, - "required": ["type", "lport", "rhost", "rport"], - "additionalProperties": False - }, - }, - "oneOf": [ - {"$ref": "#/definitions/UDP"}, - ], - "additionalProperties": True, - "required": ["type"] -} - VBOX_CAPTURE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Request validation to start a packet capture on a VirtualBox VM instance port", diff --git a/gns3server/schemas/vpcs.py b/gns3server/schemas/vpcs.py index 14cad8be..05d60d98 100644 --- a/gns3server/schemas/vpcs.py +++ b/gns3server/schemas/vpcs.py @@ -75,62 +75,6 @@ VPCS_UPDATE_SCHEMA = { "additionalProperties": False, } -VPCS_NIO_SCHEMA = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Request validation to add a NIO for a VPCS instance", - "type": "object", - "definitions": { - "UDP": { - "description": "UDP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_udp"] - }, - "lport": { - "description": "Local port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - }, - "rhost": { - "description": "Remote host", - "type": "string", - "minLength": 1 - }, - "rport": { - "description": "Remote port", - "type": "integer", - "minimum": 1, - "maximum": 65535 - } - }, - "required": ["type", "lport", "rhost", "rport"], - "additionalProperties": False - }, - "TAP": { - "description": "TAP Network Input/Output", - "properties": { - "type": { - "enum": ["nio_tap"] - }, - "tap_device": { - "description": "TAP device name e.g. tap0", - "type": "string", - "minLength": 1 - }, - }, - "required": ["type", "tap_device"], - "additionalProperties": False - }, - }, - "oneOf": [ - {"$ref": "#/definitions/UDP"}, - {"$ref": "#/definitions/TAP"}, - ], - "additionalProperties": True, - "required": ["type"] -} - VPCS_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "VPCS instance",