mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Reorganize how appliance creation is validated against JSON schemas.
This allows for clearer error messages when validation fails.
This commit is contained in:
1 parent
71fcf855b4
commit
499ab9844a
19 files changed
+1524
-1325
No files matched your search
@@ -23,6 +23,7 @@ import socket
|
||||
import shutil
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import jsonschema
|
||||
|
||||
from ..config import Config
|
||||
from .project import Project
|
||||
@@ -144,10 +145,9 @@ class Controller:
|
||||
appliance_id = settings.setdefault("appliance_id", str(uuid.uuid4()))
|
||||
try:
|
||||
appliance = Appliance(appliance_id, settings)
|
||||
appliance.__json__() # Check if loaded without error
|
||||
except KeyError as e:
|
||||
# appliance settings is not complete
|
||||
raise aiohttp.web.HTTPConflict(text="Cannot create new appliance: key '{}' is missing for appliance ID '{}'".format(e, appliance_id))
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "JSON schema error adding appliance with JSON data '{}': {}".format(settings, e.message)
|
||||
raise aiohttp.web.HTTPBadRequest(text=message)
|
||||
self._appliances[appliance.id] = appliance
|
||||
self.save()
|
||||
self.notification.controller_emit("appliance.created", appliance.__json__())
|
||||
@@ -327,11 +327,10 @@ class Controller:
|
||||
for appliance_settings in controller_settings["appliances"]:
|
||||
try:
|
||||
appliance = Appliance(appliance_settings["appliance_id"], appliance_settings)
|
||||
appliance.__json__() # Check if loaded without error
|
||||
self._appliances[appliance.id] = appliance
|
||||
except KeyError as e:
|
||||
# appliance data is not complete (missing name or type)
|
||||
log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(appliance_settings["appliance_id"], appliance_settings.get("name", "unknown"), e))
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "Cannot load appliance with JSON data '{}': {}".format(appliance_settings, e.message)
|
||||
log.warning(message)
|
||||
continue
|
||||
|
||||
# load GNS3 VM settings
|
||||
|
||||
@@ -17,6 +17,56 @@
|
||||
|
||||
import copy
|
||||
import uuid
|
||||
import json
|
||||
import jsonschema
|
||||
|
||||
from gns3server.schemas.cloud_appliance import CLOUD_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_switch_appliance import ETHERNET_SWITCH_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.ethernet_hub_appliance import ETHERNET_HUB_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.docker_appliance import DOCKER_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vpcs_appliance import VPCS_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.traceng_appliance import TRACENG_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.virtualbox_appliance import VIRTUALBOX_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.vmware_appliance import VMWARE_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.iou_appliance import IOU_APPLIANCE_OBJECT_SCHEMA
|
||||
from gns3server.schemas.qemu_appliance import QEMU_APPLIANCE_OBJECT_SCHEMA
|
||||
|
||||
from gns3server.schemas.dynamips_appliance import (
|
||||
DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C7200_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3745_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3725_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C3600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C2691_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C2600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
C1700_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA
|
||||
)
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Add default values for missing entries in a request, largely taken from jsonschema documentation example
|
||||
# https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
|
||||
def extend_with_default(validator_class):
|
||||
|
||||
validate_properties = validator_class.VALIDATORS["properties"]
|
||||
def set_defaults(validator, properties, instance, schema):
|
||||
if jsonschema.Draft4Validator(schema).is_valid(instance):
|
||||
# only add default for the matching sub-schema (e.g. when using 'oneOf')
|
||||
for property, subschema in properties.items():
|
||||
if "default" in subschema:
|
||||
instance.setdefault(property, subschema["default"])
|
||||
|
||||
for error in validate_properties(validator, properties, instance, schema,):
|
||||
yield error
|
||||
|
||||
return jsonschema.validators.extend(
|
||||
validator_class, {"properties" : set_defaults},
|
||||
)
|
||||
|
||||
|
||||
ValidatorWithDefaults = extend_with_default(jsonschema.Draft4Validator)
|
||||
|
||||
ID_TO_CATEGORY = {
|
||||
3: "firewall",
|
||||
@@ -25,6 +75,30 @@ ID_TO_CATEGORY = {
|
||||
0: "router"
|
||||
}
|
||||
|
||||
APPLIANCE_TYPE_TO_SHEMA = {
|
||||
"cloud": CLOUD_APPLIANCE_OBJECT_SCHEMA,
|
||||
"ethernet_hub": ETHERNET_HUB_APPLIANCE_OBJECT_SCHEMA,
|
||||
"ethernet_switch": ETHERNET_SWITCH_APPLIANCE_OBJECT_SCHEMA,
|
||||
"docker": DOCKER_APPLIANCE_OBJECT_SCHEMA,
|
||||
"dynamips": DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"vpcs": VPCS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"traceng": TRACENG_APPLIANCE_OBJECT_SCHEMA,
|
||||
"virtualbox": VIRTUALBOX_APPLIANCE_OBJECT_SCHEMA,
|
||||
"vmware": VMWARE_APPLIANCE_OBJECT_SCHEMA,
|
||||
"iou": IOU_APPLIANCE_OBJECT_SCHEMA,
|
||||
"qemu": QEMU_APPLIANCE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
DYNAMIPS_PLATFORM_TO_SHEMA = {
|
||||
"c7200": C7200_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3745": C3745_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3725": C3725_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c3600": C3600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c2691": C2691_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c2600": C2600_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA,
|
||||
"c1700": C1700_DYNAMIPS_APPLIANCE_OBJECT_SCHEMA
|
||||
}
|
||||
|
||||
|
||||
class Appliance:
|
||||
|
||||
@@ -66,6 +140,13 @@ class Appliance:
|
||||
|
||||
self._builtin = builtin
|
||||
|
||||
if builtin is False:
|
||||
self.validate_and_apply_defaults(APPLIANCE_TYPE_TO_SHEMA[self.appliance_type])
|
||||
|
||||
if self.appliance_type == "dynamips":
|
||||
# special case for Dynamips to cover all platform types that contain specific settings
|
||||
self.validate_and_apply_defaults(DYNAMIPS_PLATFORM_TO_SHEMA[self._settings["platform"]])
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
@@ -102,6 +183,17 @@ class Appliance:
|
||||
controller.notification.controller_emit("appliance.updated", self.__json__())
|
||||
controller.save()
|
||||
|
||||
def validate_and_apply_defaults(self, schema):
|
||||
|
||||
validator = ValidatorWithDefaults(schema)
|
||||
try:
|
||||
validator.validate(self.__json__())
|
||||
except jsonschema.ValidationError as e:
|
||||
message = "JSON schema error {}".format(e.message)
|
||||
log.error(message)
|
||||
log.debug("Input schema: {}".format(json.dumps(schema)))
|
||||
raise
|
||||
|
||||
def __json__(self):
|
||||
"""
|
||||
Appliance settings.
|
||||
@@ -109,8 +201,6 @@ class Appliance:
|
||||
|
||||
settings = self._settings
|
||||
settings.update({"appliance_id": self._id,
|
||||
"default_name_format": settings.get("default_name_format", "{name}-{0}"),
|
||||
"symbol": settings.get("symbol", ":/symbols/computer.svg"),
|
||||
"builtin": self.builtin})
|
||||
|
||||
if not self.builtin:
|
||||
|
||||
Reference in new issue
Block a user