From f0fe9d39fa9418f56c3bf7faa8b34a4291659c37 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 11 Nov 2018 20:13:58 +0800 Subject: [PATCH 1/6] Base for dedicated appliance management API. Ref https://github.com/GNS3/gns3-server/issues/1427 --- gns3server/compute/vmware/vmware_vm.py | 8 +- gns3server/controller/__init__.py | 283 +++++++++++------- gns3server/controller/appliance.py | 77 +++-- gns3server/controller/node.py | 5 +- gns3server/controller/project.py | 7 +- .../api/controller/appliance_handler.py | 85 +++++- .../handlers/api/controller/node_handler.py | 3 +- gns3server/schemas/appliance.py | 48 +++ 8 files changed, 359 insertions(+), 157 deletions(-) diff --git a/gns3server/compute/vmware/vmware_vm.py b/gns3server/compute/vmware/vmware_vm.py index 2ca01096..f1fb7e0d 100644 --- a/gns3server/compute/vmware/vmware_vm.py +++ b/gns3server/compute/vmware/vmware_vm.py @@ -188,10 +188,10 @@ class VMwareVM(BaseNode): # create the linked clone based on the base snapshot new_vmx_path = os.path.join(self.working_dir, self.name + ".vmx") await self._control_vm("clone", - new_vmx_path, - "linked", - "-snapshot={}".format(base_snapshot_name), - "-cloneName={}".format(self.name)) + new_vmx_path, + "linked", + "-snapshot={}".format(base_snapshot_name), + "-cloneName={}".format(self.name)) try: vmsd_pairs = self.manager.parse_vmware_file(vmsd_path) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index cf303e90..27c3b2cb 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -128,71 +128,59 @@ class Controller: log.warning("Cannot load appliance template file '%s': %s", path, str(e)) continue + def add_appliance(self, settings): + """ + Adds a new appliance. + + :param settings: appliance settings + + :returns: Appliance object + """ + + appliance_id = settings.get("appliance_id", "") + if appliance_id in self._appliances: + raise aiohttp.web.HTTPConflict(text="Appliance ID '{}' already exists".format(appliance_id)) + else: + 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)) + self._appliances[appliance.id] = appliance + self.save() + return appliance + + def get_appliance(self, appliance_id): + """ + Gets an appliance. + + :param appliance_id: appliance identifier + + :returns: Appliance object + """ + + appliance = self._appliances.get(appliance_id) + if not appliance: + raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id)) + return appliance + + def delete_appliance(self, appliance_id): + """ + Deletes an appliance. + + :param appliance_id: appliance identifier + """ + + appliance = self._appliances.get(appliance_id) + if appliance.builtin: + raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is builtin".format(appliance_id)) + self._appliances.pop(appliance_id) + def load_appliances(self): - self._appliances = {} - vms = [] - for vm in self._settings.get("Qemu", {}).get("vms", []): - vm["node_type"] = "qemu" - vms.append(vm) - for vm in self._settings.get("IOU", {}).get("devices", []): - vm["node_type"] = "iou" - vms.append(vm) - for vm in self._settings.get("Docker", {}).get("containers", []): - vm["node_type"] = "docker" - vms.append(vm) - for vm in self._settings.get("Builtin", {}).get("cloud_nodes", []): - vm["node_type"] = "cloud" - vms.append(vm) - for vm in self._settings.get("Builtin", {}).get("ethernet_switches", []): - vm["node_type"] = "ethernet_switch" - vms.append(vm) - for vm in self._settings.get("Builtin", {}).get("ethernet_hubs", []): - vm["node_type"] = "ethernet_hub" - vms.append(vm) - for vm in self._settings.get("Dynamips", {}).get("routers", []): - vm["node_type"] = "dynamips" - vms.append(vm) - for vm in self._settings.get("VMware", {}).get("vms", []): - vm["node_type"] = "vmware" - vms.append(vm) - for vm in self._settings.get("VirtualBox", {}).get("vms", []): - vm["node_type"] = "virtualbox" - vms.append(vm) - for vm in self._settings.get("VPCS", {}).get("nodes", []): - vm["node_type"] = "vpcs" - vms.append(vm) - for vm in self._settings.get("TraceNG", {}).get("nodes", []): - vm["node_type"] = "traceng" - vms.append(vm) - - for vm in vms: - # remove deprecated properties - for prop in vm.copy(): - if prop in ["enable_remote_console", "use_ubridge", "acpi_shutdown"]: - del vm[prop] - - # remove deprecated default_symbol and hover_symbol - # and set symbol if not present - deprecated = ["default_symbol", "hover_symbol"] - if len([prop for prop in vm.keys() if prop in deprecated]) > 0: - if "default_symbol" in vm.keys(): - del vm["default_symbol"] - if "hover_symbol" in vm.keys(): - del vm["hover_symbol"] - - if "symbol" not in vm.keys(): - vm["symbol"] = ":/symbols/computer.svg" - - vm.setdefault("appliance_id", str(uuid.uuid4())) - try: - appliance = Appliance(vm["appliance_id"], vm) - 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(vm["appliance_id"], vm.get("name", "unknown"), e)) - continue + #self._appliances = {} # Add builtins builtins = [] @@ -232,14 +220,14 @@ class Controller: computes = await self._load_controller_settings() try: self._local_server = await self.add_compute(compute_id="local", - name=name, - protocol=server_config.get("protocol", "http"), - host=host, - console_host=console_host, - port=port, - user=server_config.get("user", ""), - password=server_config.get("password", ""), - force=True) + name=name, + protocol=server_config.get("protocol", "http"), + host=host, + console_host=console_host, + port=port, + user=server_config.get("user", ""), + password=server_config.get("password", ""), + force=True) except aiohttp.web.HTTPConflict as e: log.fatal("Cannot access to the local server, make sure something else is not running on the TCP port {}".format(port)) sys.exit(1) @@ -288,31 +276,34 @@ class Controller: # We don't save during the loading otherwise we could lost stuff if self._settings is None: return - data = { - "computes": [], - "settings": self._settings, - "gns3vm": self.gns3vm.__json__(), - "appliance_templates_etag": self._appliance_templates_etag, - "version": __version__ - } - for c in self._computes.values(): - if c.id != "local" and c.id != "vm": - data["computes"].append({ - "host": c.host, - "name": c.name, - "port": c.port, - "protocol": c.protocol, - "user": c.user, - "password": c.password, - "compute_id": c.id - }) + controller_settings = {"computes": [], + "settings": self._settings, + "appliances": [], + "gns3vm": self.gns3vm.__json__(), + "appliance_templates_etag": self._appliance_templates_etag, + "version": __version__} + + for appliance in self._appliances.values(): + if not appliance.builtin: + controller_settings["appliances"].append(appliance.__json__()) + + for compute in self._computes.values(): + if compute.id != "local" and compute.id != "vm": + controller_settings["computes"].append({"host": compute.host, + "name": compute.name, + "port": compute.port, + "protocol": compute.protocol, + "user": compute.user, + "password": compute.password, + "compute_id": compute.id}) + try: os.makedirs(os.path.dirname(self._config_file), exist_ok=True) with open(self._config_file, 'w+') as f: - json.dump(data, f, indent=4) + json.dump(controller_settings, f, indent=4) except OSError as e: - log.error("Cannnot write configuration file '{}': {}".format(self._config_file, e)) + log.error("Cannot write controller configuration file '{}': {}".format(self._config_file, e)) async def _load_controller_settings(self): """ @@ -324,23 +315,37 @@ class Controller: await self._import_gns3_gui_conf() self.save() with open(self._config_file) as f: - data = json.load(f) + controller_settings = json.load(f) except (OSError, ValueError) as e: log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e)) self._settings = {} return [] - if "settings" in data and data["settings"] is not None: - self._settings = data["settings"] + if "settings" in controller_settings and controller_settings["settings"] is not None: + self._settings = controller_settings["settings"] else: self._settings = {} - if "gns3vm" in data: - self.gns3vm.settings = data["gns3vm"] - self._appliance_templates_etag = data.get("appliance_templates_etag") + # load the appliances + if "appliances" in controller_settings: + 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)) + continue + + # load GNS3 VM settings + if "gns3vm" in controller_settings: + self.gns3vm.settings = controller_settings["gns3vm"] + + self._appliance_templates_etag = controller_settings.get("appliance_templates_etag") self.load_appliance_templates() self.load_appliances() - return data.get("computes", []) + return controller_settings.get("computes", []) async def load_projects(self): """ @@ -416,18 +421,16 @@ class Controller: config_file = os.path.join(os.path.dirname(self._config_file), "gns3_gui.conf") if os.path.exists(config_file): with open(config_file) as f: - data = json.load(f) - server_settings = data.get("Servers", {}) + settings = json.load(f) + server_settings = settings.get("Servers", {}) for remote in server_settings.get("remote_servers", []): try: - await self.add_compute( - host=remote.get("host", "localhost"), - port=remote.get("port", 3080), - protocol=remote.get("protocol", "http"), - name=remote.get("url"), - user=remote.get("user"), - password=remote.get("password") - ) + await self.add_compute(host=remote.get("host", "localhost"), + port=remote.get("port", 3080), + protocol=remote.get("protocol", "http"), + name=remote.get("url"), + user=remote.get("user"), + password=remote.get("password")) except aiohttp.web.HTTPConflict: pass # if the server is broken we skip it if "vm" in server_settings: @@ -458,6 +461,70 @@ class Controller: "headless": vm_settings.get("headless", False), "vmname": vmname } + + vms = [] + for vm in settings.get("Qemu", {}).get("vms", []): + vm["node_type"] = "qemu" + vms.append(vm) + for vm in settings.get("IOU", {}).get("devices", []): + vm["node_type"] = "iou" + vms.append(vm) + for vm in settings.get("Docker", {}).get("containers", []): + vm["node_type"] = "docker" + vms.append(vm) + for vm in settings.get("Builtin", {}).get("cloud_nodes", []): + vm["node_type"] = "cloud" + vms.append(vm) + for vm in settings.get("Builtin", {}).get("ethernet_switches", []): + vm["node_type"] = "ethernet_switch" + vms.append(vm) + for vm in settings.get("Builtin", {}).get("ethernet_hubs", []): + vm["node_type"] = "ethernet_hub" + vms.append(vm) + for vm in settings.get("Dynamips", {}).get("routers", []): + vm["node_type"] = "dynamips" + vms.append(vm) + for vm in settings.get("VMware", {}).get("vms", []): + vm["node_type"] = "vmware" + vms.append(vm) + for vm in settings.get("VirtualBox", {}).get("vms", []): + vm["node_type"] = "virtualbox" + vms.append(vm) + for vm in settings.get("VPCS", {}).get("nodes", []): + vm["node_type"] = "vpcs" + vms.append(vm) + for vm in settings.get("TraceNG", {}).get("nodes", []): + vm["node_type"] = "traceng" + vms.append(vm) + + for vm in vms: + # remove deprecated properties + for prop in vm.copy(): + if prop in ["enable_remote_console", "use_ubridge", "acpi_shutdown"]: + del vm[prop] + + # remove deprecated default_symbol and hover_symbol + # and set symbol if not present + deprecated = ["default_symbol", "hover_symbol"] + if len([prop for prop in vm.keys() if prop in deprecated]) > 0: + if "default_symbol" in vm.keys(): + del vm["default_symbol"] + if "hover_symbol" in vm.keys(): + del vm["hover_symbol"] + + if "symbol" not in vm.keys(): + vm["symbol"] = ":/symbols/computer.svg" + + vm.setdefault("appliance_id", str(uuid.uuid4())) + try: + appliance = Appliance(vm["appliance_id"], vm) + 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(vm["appliance_id"], vm.get("name", "unknown"), e)) + continue + self._settings = {} @property diff --git a/gns3server/controller/appliance.py b/gns3server/controller/appliance.py index 4f40637c..63949099 100644 --- a/gns3server/controller/appliance.py +++ b/gns3server/controller/appliance.py @@ -18,8 +18,6 @@ import copy import uuid - -# Convert old GUI category to text category ID_TO_CATEGORY = { 3: "firewall", 2: "guest", @@ -30,7 +28,7 @@ ID_TO_CATEGORY = { class Appliance: - def __init__(self, appliance_id, data, builtin=False): + def __init__(self, appliance_id, settings, builtin=False): if appliance_id is None: self._id = str(uuid.uuid4()) @@ -38,18 +36,30 @@ class Appliance: self._id = str(appliance_id) else: self._id = appliance_id - self._data = data.copy() - if "appliance_id" in self._data: - del self._data["appliance_id"] + + self._settings = copy.deepcopy(settings) # Version of the gui before 2.1 use linked_base # and the server linked_clone - if "linked_base" in self._data: - linked_base = self._data.pop("linked_base") - if "linked_clone" not in self._data: - self._data["linked_clone"] = linked_base - if data["node_type"] == "iou" and "image" in data: - del self._data["image"] + if "linked_base" in self.settings: + linked_base = self._settings.pop("linked_base") + if "linked_clone" not in self._settings: + self._settings["linked_clone"] = linked_base + + # Convert old GUI category to text category + try: + self._settings["category"] = ID_TO_CATEGORY[self._settings["category"]] + except KeyError: + pass + + # The "server" setting has been replaced by "compute_id" setting in version 2.2 + if "server" in self._settings: + self._settings["compute_id"] = self._settings.pop("server") + + # Remove an old IOU setting + if settings["node_type"] == "iou" and "image" in settings: + del self._settings["image"] + self._builtin = builtin @property @@ -57,16 +67,25 @@ class Appliance: return self._id @property - def data(self): - return copy.deepcopy(self._data) + def settings(self): + return self._settings + + @settings.setter + def settings(self, settings): + + self._settings.update(settings) @property def name(self): - return self._data["name"] + return self._settings["name"] @property def compute_id(self): - return self._data.get("server") + return self._settings["compute_id"] + + @property + def node_type(self): + return self._settings["node_type"] @property def builtin(self): @@ -74,21 +93,15 @@ class Appliance: def __json__(self): """ - Appliance data (a hash) + Appliance settings. """ - try: - category = ID_TO_CATEGORY[self._data["category"]] - except KeyError: - category = self._data["category"] + 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}) - return { - "appliance_id": self._id, - "node_type": self._data["node_type"], - "name": self._data["name"], - "default_name_format": self._data.get("default_name_format", "{name}-{0}"), - "category": category, - "symbol": self._data.get("symbol", ":/symbols/computer.svg"), - "compute_id": self.compute_id, - "builtin": self._builtin, - "platform": self._data.get("platform", None) - } + if not self.builtin: + settings["compute_id"] = self.compute_id + + return settings diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 642d07af..95227606 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -271,9 +271,8 @@ class Node: if self._label is None: # Apply to label user style or default try: - style = qt_font_to_style( - self._project.controller.settings["GraphicsView"]["default_label_font"], - self._project.controller.settings["GraphicsView"]["default_label_color"]) + style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], + self._project.controller.settings["GraphicsView"]["default_label_color"]) except KeyError: style = "font-size: 10;font-familly: Verdana" diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 457473f4..04f82a6a 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -465,7 +465,7 @@ class Project: Create a node from an appliance """ try: - template = self.controller.appliances[appliance_id].data + template = self.controller.appliances[appliance_id].settings except KeyError: msg = "Appliance {} doesn't exist".format(appliance_id) log.error(msg) @@ -473,12 +473,13 @@ class Project: template["x"] = x template["y"] = y node_type = template.pop("node_type") - compute = self.controller.get_compute(template.pop("server", compute_id)) + compute = self.controller.get_compute(template.pop("compute_id", compute_id)) name = template.pop("name") default_name_format = template.pop("default_name_format", "{name}-{0}") name = default_name_format.replace("{name}", name) node_id = str(uuid.uuid4()) - node = await self.add_node(compute, name, node_id, node_type=node_type, appliance_id=appliance_id, **template) + template.pop("builtin") # not needed to add a node + node = await self.add_node(compute, name, node_id, node_type=node_type, **template) return node @open_required diff --git a/gns3server/handlers/api/controller/appliance_handler.py b/gns3server/handlers/api/controller/appliance_handler.py index c78f6cb1..eda9618f 100644 --- a/gns3server/handlers/api/controller/appliance_handler.py +++ b/gns3server/handlers/api/controller/appliance_handler.py @@ -20,6 +20,11 @@ from gns3server.controller import Controller from gns3server.schemas.node import NODE_OBJECT_SCHEMA from gns3server.schemas.appliance import APPLIANCE_USAGE_SCHEMA +from gns3server.schemas.appliance import ( + APPLIANCE_OBJECT_SCHEMA, + APPLIANCE_UPDATE_SCHEMA, + APPLIANCE_CREATE_SCHEMA +) import logging log = logging.getLogger(__name__) @@ -42,6 +47,74 @@ class ApplianceHandler: controller.load_appliance_templates() response.json([c for c in controller.appliance_templates.values()]) + @Route.post( + r"/appliances", + description="Create a new appliance", + status_codes={ + 201: "Appliance created", + 400: "Invalid request" + }, + input=APPLIANCE_CREATE_SCHEMA, + output=APPLIANCE_OBJECT_SCHEMA) + def create(request, response): + + controller = Controller.instance() + appliance = controller.add_appliance(request.json) + response.set_status(201) + response.json(appliance) + + @Route.get( + r"/appliances/{appliance_id}", + status_codes={ + 200: "Appliance found", + 400: "Invalid request", + 404: "Appliance doesn't exist" + }, + description="Get an appliance", + output=APPLIANCE_OBJECT_SCHEMA) + def get(request, response): + + controller = Controller.instance() + appliance = controller.get_appliance(request.match_info["appliance_id"]) + response.set_status(200) + response.json(appliance) + + @Route.put( + r"/appliances/{appliance_id}", + status_codes={ + 200: "Appliance updated", + 400: "Invalid request", + 404: "Appliance doesn't exist" + }, + description="Update an appliance", + input=APPLIANCE_UPDATE_SCHEMA, + output=APPLIANCE_OBJECT_SCHEMA) + def update(request, response): + + controller = Controller.instance() + appliance = controller.get_appliance(request.match_info["appliance_id"]) + #TODO: update appliance! + #appliance.settings = request.json + response.set_status(200) + response.json(appliance) + + @Route.delete( + r"/appliances/{appliance_id}", + parameters={ + "appliance_id": "Node UUID" + }, + status_codes={ + 204: "Appliance deleted", + 400: "Invalid request", + 404: "Appliance doesn't exist" + }, + description="Delete an appliance") + def delete(request, response): + + controller = Controller.instance() + controller.delete_appliance(request.match_info["appliance_id"]) + response.set_status(204) + @Route.get( r"/appliances", description="List of appliance", @@ -50,6 +123,8 @@ class ApplianceHandler: }) def list(request, response): + #old_etag = request.headers.get('If-None-Match', '') + #print("ETAG => ", old_etag) controller = Controller.instance() response.json([c for c in controller.appliances.values()]) @@ -58,11 +133,11 @@ class ApplianceHandler: description="Create a node from an appliance", parameters={ "project_id": "Project UUID", - "appliance_id": "Appliance template UUID" + "appliance_id": "Appliance UUID" }, status_codes={ 201: "Node created", - 404: "The project or template doesn't exist" + 404: "The project or appliance doesn't exist" }, input=APPLIANCE_USAGE_SCHEMA, output=NODE_OBJECT_SCHEMA) @@ -71,7 +146,7 @@ class ApplianceHandler: controller = Controller.instance() project = controller.get_project(request.match_info["project_id"]) await project.add_node_from_appliance(request.match_info["appliance_id"], - x=request.json["x"], - y=request.json["y"], - compute_id=request.json.get("compute_id")) + x=request.json["x"], + y=request.json["y"], + compute_id=request.json.get("compute_id")) response.set_status(201) diff --git a/gns3server/handlers/api/controller/node_handler.py b/gns3server/handlers/api/controller/node_handler.py index 075eda96..11567ee3 100644 --- a/gns3server/handlers/api/controller/node_handler.py +++ b/gns3server/handlers/api/controller/node_handler.py @@ -76,7 +76,7 @@ class NodeHandler: 400: "Invalid request", 404: "Node doesn't exist" }, - description="Update a node instance", + description="Get a node", output=NODE_OBJECT_SCHEMA) def get_node(request, response): project = Controller.instance().get_project(request.match_info["project_id"]) @@ -84,7 +84,6 @@ class NodeHandler: response.set_status(200) response.json(node) - @Route.put( r"/projects/{project_id}/nodes/{node_id}", status_codes={ diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index fde89ae3..7101c7f4 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -15,6 +15,54 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import copy +from .node import NODE_TYPE_SCHEMA + + +APPLIANCE_OBJECT_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "A template object", + "type": "object", + "properties": { + "appliance_id": { + "description": "Appliance UUID from which the node has been created. Read only", + "type": ["null", "string"], + "minLength": 36, + "maxLength": 36, + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" + }, + "compute_id": { + "description": "Compute identifier", + "type": "string" + }, + "node_type": NODE_TYPE_SCHEMA, + "name": { + "description": "Appliance name", + "type": "string", + "minLength": 1, + }, + "default_name_format": { + "description": "Default name format", + "type": "string", + "minLength": 1, + }, + "symbol": { + "description": "Symbol of the appliance", + "type": "string", + "minLength": 1 + }, + }, + "additionalProperties": True, #TODO: validate all properties + "required": ["appliance_id", "compute_id", "node_type", "name", "default_name_format", "symbol"] +} + +APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) +# these properties are not required to create an appliance +APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id") +APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id") +APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format") +APPLIANCE_CREATE_SCHEMA["required"].remove("symbol") +APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) APPLIANCE_USAGE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", From 089fdff4f1adb873f13aa03e10d02fd5cce54ce1 Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 13 Nov 2018 14:59:18 +0800 Subject: [PATCH 2/6] Working dedicated appliance management API. Ref https://github.com/GNS3/gns3-server/issues/1427 --- gns3server/controller/__init__.py | 43 ++++++++++--------- gns3server/controller/appliance.py | 15 +++++-- gns3server/controller/project.py | 4 +- .../api/controller/appliance_handler.py | 19 +++++--- gns3server/schemas/appliance.py | 9 ++-- 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 27c3b2cb..242f80a9 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -174,9 +174,12 @@ class Controller: """ appliance = self._appliances.get(appliance_id) + if not appliance: + raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id)) if appliance.builtin: - raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is builtin".format(appliance_id)) + raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id)) self._appliances.pop(appliance_id) + self.save() def load_appliances(self): @@ -184,17 +187,17 @@ class Controller: # Add builtins builtins = [] - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"), {"node_type": "cloud", "name": "Cloud", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "nat"), {"node_type": "nat", "name": "NAT", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "vpcs"), {"node_type": "vpcs", "name": "VPCS", "default_name_format": "PC-{0}", "category": 2, "symbol": ":/symbols/vpcs_guest.svg", "properties": {"base_script_file": "vpcs_base_config.txt"}}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_switch"), {"node_type": "ethernet_switch", "console_type": "telnet", "name": "Ethernet switch", "category": 1, "symbol": ":/symbols/ethernet_switch.svg"}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_hub"), {"node_type": "ethernet_hub", "name": "Ethernet hub", "category": 1, "symbol": ":/symbols/hub.svg"}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "frame_relay_switch"), {"node_type": "frame_relay_switch", "name": "Frame Relay switch", "category": 1, "symbol": ":/symbols/frame_relay_switch.svg"}, builtin=True)) - builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "atm_switch"), {"node_type": "atm_switch", "name": "ATM switch", "category": 1, "symbol": ":/symbols/atm_switch.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "cloud"), {"appliance_type": "cloud", "name": "Cloud", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "nat"), {"appliance_type": "nat", "name": "NAT", "category": 2, "symbol": ":/symbols/cloud.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "vpcs"), {"appliance_type": "vpcs", "name": "VPCS", "default_name_format": "PC-{0}", "category": 2, "symbol": ":/symbols/vpcs_guest.svg", "properties": {"base_script_file": "vpcs_base_config.txt"}}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_switch"), {"appliance_type": "ethernet_switch", "console_type": "telnet", "name": "Ethernet switch", "category": 1, "symbol": ":/symbols/ethernet_switch.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "ethernet_hub"), {"appliance_type": "ethernet_hub", "name": "Ethernet hub", "category": 1, "symbol": ":/symbols/hub.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "frame_relay_switch"), {"appliance_type": "frame_relay_switch", "name": "Frame Relay switch", "category": 1, "symbol": ":/symbols/frame_relay_switch.svg"}, builtin=True)) + builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "atm_switch"), {"appliance_type": "atm_switch", "name": "ATM switch", "category": 1, "symbol": ":/symbols/atm_switch.svg"}, builtin=True)) #FIXME: disable TraceNG #if sys.platform.startswith("win"): - # builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "traceng"), {"node_type": "traceng", "name": "TraceNG", "default_name_format": "TraceNG-{0}", "category": 2, "symbol": ":/symbols/traceng.svg", "properties": {}}, builtin=True)) + # builtins.append(Appliance(uuid.uuid3(uuid.NAMESPACE_DNS, "traceng"), {"appliance_type": "traceng", "name": "TraceNG", "default_name_format": "TraceNG-{0}", "category": 2, "symbol": ":/symbols/traceng.svg", "properties": {}}, builtin=True)) for b in builtins: self._appliances[b.id] = b @@ -464,37 +467,37 @@ class Controller: vms = [] for vm in settings.get("Qemu", {}).get("vms", []): - vm["node_type"] = "qemu" + vm["appliance_type"] = "qemu" vms.append(vm) for vm in settings.get("IOU", {}).get("devices", []): - vm["node_type"] = "iou" + vm["appliance_type"] = "iou" vms.append(vm) for vm in settings.get("Docker", {}).get("containers", []): - vm["node_type"] = "docker" + vm["appliance_type"] = "docker" vms.append(vm) for vm in settings.get("Builtin", {}).get("cloud_nodes", []): - vm["node_type"] = "cloud" + vm["appliance_type"] = "cloud" vms.append(vm) for vm in settings.get("Builtin", {}).get("ethernet_switches", []): - vm["node_type"] = "ethernet_switch" + vm["appliance_type"] = "ethernet_switch" vms.append(vm) for vm in settings.get("Builtin", {}).get("ethernet_hubs", []): - vm["node_type"] = "ethernet_hub" + vm["appliance_type"] = "ethernet_hub" vms.append(vm) for vm in settings.get("Dynamips", {}).get("routers", []): - vm["node_type"] = "dynamips" + vm["appliance_type"] = "dynamips" vms.append(vm) for vm in settings.get("VMware", {}).get("vms", []): - vm["node_type"] = "vmware" + vm["appliance_type"] = "vmware" vms.append(vm) for vm in settings.get("VirtualBox", {}).get("vms", []): - vm["node_type"] = "virtualbox" + vm["appliance_type"] = "virtualbox" vms.append(vm) for vm in settings.get("VPCS", {}).get("nodes", []): - vm["node_type"] = "vpcs" + vm["appliance_type"] = "vpcs" vms.append(vm) for vm in settings.get("TraceNG", {}).get("nodes", []): - vm["node_type"] = "traceng" + vm["appliance_type"] = "traceng" vms.append(vm) for vm in vms: diff --git a/gns3server/controller/appliance.py b/gns3server/controller/appliance.py index 63949099..7428d859 100644 --- a/gns3server/controller/appliance.py +++ b/gns3server/controller/appliance.py @@ -56,8 +56,12 @@ class Appliance: if "server" in self._settings: self._settings["compute_id"] = self._settings.pop("server") + # The "node_type" setting has been replaced by "appliance_type" setting in version 2.2 + if "node_type" in self._settings: + self._settings["appliance_type"] = self._settings.pop("node_type") + # Remove an old IOU setting - if settings["node_type"] == "iou" and "image" in settings: + if self._settings["appliance_type"] == "iou" and "image" in self._settings: del self._settings["image"] self._builtin = builtin @@ -72,7 +76,6 @@ class Appliance: @settings.setter def settings(self, settings): - self._settings.update(settings) @property @@ -84,17 +87,21 @@ class Appliance: return self._settings["compute_id"] @property - def node_type(self): - return self._settings["node_type"] + def appliance_type(self): + return self._settings["appliance_type"] @property def builtin(self): return self._builtin + def update(self, **kwargs): + self._settings.update(kwargs) + def __json__(self): """ Appliance settings. """ + settings = self._settings settings.update({"appliance_id": self._id, "default_name_format": settings.get("default_name_format", "{name}-{0}"), diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 04f82a6a..f7561af1 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -465,14 +465,14 @@ class Project: Create a node from an appliance """ try: - template = self.controller.appliances[appliance_id].settings + template = copy.deepcopy(self.controller.appliances[appliance_id].settings) except KeyError: msg = "Appliance {} doesn't exist".format(appliance_id) log.error(msg) raise aiohttp.web.HTTPNotFound(text=msg) template["x"] = x template["y"] = y - node_type = template.pop("node_type") + node_type = template.pop("appliance_type") compute = self.controller.get_compute(template.pop("compute_id", compute_id)) name = template.pop("name") default_name_format = template.pop("default_name_format", "{name}-{0}") diff --git a/gns3server/handlers/api/controller/appliance_handler.py b/gns3server/handlers/api/controller/appliance_handler.py index eda9618f..06e6f56c 100644 --- a/gns3server/handlers/api/controller/appliance_handler.py +++ b/gns3server/handlers/api/controller/appliance_handler.py @@ -20,6 +20,9 @@ from gns3server.controller import Controller from gns3server.schemas.node import NODE_OBJECT_SCHEMA from gns3server.schemas.appliance import APPLIANCE_USAGE_SCHEMA +import hashlib +import json + from gns3server.schemas.appliance import ( APPLIANCE_OBJECT_SCHEMA, APPLIANCE_UPDATE_SCHEMA, @@ -74,10 +77,17 @@ class ApplianceHandler: output=APPLIANCE_OBJECT_SCHEMA) def get(request, response): + request_etag = request.headers.get("If-None-Match", "") controller = Controller.instance() appliance = controller.get_appliance(request.match_info["appliance_id"]) - response.set_status(200) - response.json(appliance) + data = json.dumps(appliance.__json__()) + appliance_etag = '"' + hashlib.md5(data.encode()).hexdigest() + '"' + if appliance_etag == request_etag: + response.set_status(304) + else: + response.headers["ETag"] = appliance_etag + response.set_status(200) + response.json(appliance) @Route.put( r"/appliances/{appliance_id}", @@ -93,8 +103,7 @@ class ApplianceHandler: controller = Controller.instance() appliance = controller.get_appliance(request.match_info["appliance_id"]) - #TODO: update appliance! - #appliance.settings = request.json + appliance.update(**request.json) response.set_status(200) response.json(appliance) @@ -123,8 +132,6 @@ class ApplianceHandler: }) def list(request, response): - #old_etag = request.headers.get('If-None-Match', '') - #print("ETAG => ", old_etag) controller = Controller.instance() response.json([c for c in controller.appliances.values()]) diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index 7101c7f4..2cbb9ea8 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -18,6 +18,7 @@ import copy from .node import NODE_TYPE_SCHEMA +APPLIANCE_TYPE_SCHEMA = NODE_TYPE_SCHEMA APPLIANCE_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -35,7 +36,7 @@ APPLIANCE_OBJECT_SCHEMA = { "description": "Compute identifier", "type": "string" }, - "node_type": NODE_TYPE_SCHEMA, + "appliance_type": APPLIANCE_TYPE_SCHEMA, "name": { "description": "Appliance name", "type": "string", @@ -53,7 +54,7 @@ APPLIANCE_OBJECT_SCHEMA = { }, }, "additionalProperties": True, #TODO: validate all properties - "required": ["appliance_id", "compute_id", "node_type", "name", "default_name_format", "symbol"] + "required": ["appliance_id", "compute_id", "appliance_type", "name", "default_name_format", "symbol"] } APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) @@ -62,7 +63,9 @@ APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id") APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id") APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format") APPLIANCE_CREATE_SCHEMA["required"].remove("symbol") -APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) +APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_CREATE_SCHEMA) +#APPLIANCE_UPDATE_SCHEMA["additionalProperties"] = False +del APPLIANCE_UPDATE_SCHEMA["required"] APPLIANCE_USAGE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", From cebb56387a001c33d7e44e8c758067910db3d309 Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 13 Nov 2018 15:40:18 +0800 Subject: [PATCH 3/6] Fix tests. --- gns3server/controller/appliance.py | 2 + tests/controller/test_appliance.py | 10 ++--- tests/controller/test_controller.py | 62 ++--------------------------- tests/controller/test_project.py | 3 +- 4 files changed, 12 insertions(+), 65 deletions(-) diff --git a/gns3server/controller/appliance.py b/gns3server/controller/appliance.py index 7428d859..5616bf77 100644 --- a/gns3server/controller/appliance.py +++ b/gns3server/controller/appliance.py @@ -95,6 +95,8 @@ class Appliance: return self._builtin def update(self, **kwargs): + + #TODO: do not update appliance_id, builtin or appliance_type self._settings.update(kwargs) def __json__(self): diff --git a/tests/controller/test_appliance.py b/tests/controller/test_appliance.py index 810ea5e3..a30bd370 100644 --- a/tests/controller/test_appliance.py +++ b/tests/controller/test_appliance.py @@ -30,7 +30,7 @@ def test_appliance_json(): }) assert a.__json__() == { "appliance_id": a.id, - "node_type": "qemu", + "appliance_type": "qemu", "builtin": False, "name": "Test", "default_name_format": "{name}-{0}", @@ -53,7 +53,7 @@ def test_appliance_json_with_not_known_category(): }) assert a.__json__() == { "appliance_id": a.id, - "node_type": "qemu", + "appliance_type": "qemu", "builtin": False, "name": "Test", "default_name_format": "{name}-{0}", @@ -76,7 +76,7 @@ def test_appliance_json_with_platform(): }) assert a.__json__() == { "appliance_id": a.id, - "node_type": "dynamips", + "appliance_type": "dynamips", "builtin": False, "name": "Test", "default_name_format": "{name}-{0}", @@ -101,5 +101,5 @@ def test_appliance_fix_linked_base(): "server": "local", "linked_base": True }) - assert a.data["linked_clone"] - assert "linked_base" not in a.data + assert a.settings["linked_clone"] + assert "linked_base" not in a.settings diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 7bec5ac5..18e87fd6 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -506,25 +506,15 @@ def test_appliance_templates(controller, async_run, tmpdir): def test_load_appliances(controller): - controller._settings = { - "Qemu": { - "vms": [ - { - "name": "Test", - "node_type": "qemu", - "category": "router" - } - ] - } - } + controller._settings = {} controller.load_appliances() - assert "Test" in [appliance.name for appliance in controller.appliances.values()] + assert "Cloud" in [appliance.name for appliance in controller.appliances.values()] assert "VPCS" in [appliance.name for appliance in controller.appliances.values()] for appliance in controller.appliances.values(): if appliance.name == "VPCS": - assert appliance._data["properties"] == {"base_script_file": "vpcs_base_config.txt"} + assert appliance._settings["properties"] == {"base_script_file": "vpcs_base_config.txt"} # UUID should not change when you run again the function for appliance in controller.appliances.values(): @@ -540,52 +530,6 @@ def test_load_appliances(controller): assert cloud_uuid == appliance.id -def test_load_appliances_deprecated_features_default_symbol(controller): - controller._settings = { - "Qemu": { - "vms": [ - { - "name": "Test", - "node_type": "qemu", - "category": "router", - "default_symbol": ":/symbols/iosv_virl.normal.svg", - "hover_symbol": ":/symbols/iosv_virl.selected.svg", - } - ] - } - } - controller.load_appliances() - appliances = dict([(a.name, a) for a in controller.appliances.values()]) - - assert appliances["Test"].__json__()["symbol"] == ":/symbols/computer.svg" - assert "default_symbol" not in appliances["Test"].data.keys() - assert "hover_symbol" not in appliances["Test"].data.keys() - - -def test_load_appliances_deprecated_features_default_symbol_with_symbol(controller): - controller._settings = { - "Qemu": { - "vms": [ - { - "name": "Test", - "node_type": "qemu", - "category": "router", - "default_symbol": ":/symbols/iosv_virl.normal.svg", - "hover_symbol": ":/symbols/iosv_virl.selected.svg", - "symbol": ":/symbols/my-symbol.svg" - - } - ] - } - } - controller.load_appliances() - appliances = dict([(a.name, a) for a in controller.appliances.values()]) - - assert appliances["Test"].__json__()["symbol"] == ":/symbols/my-symbol.svg" - assert "default_symbol" not in appliances["Test"].data.keys() - assert "hover_symbol" not in appliances["Test"].data.keys() - - def test_autoidlepc(controller, async_run): controller._computes["local"] = AsyncioMagicMock() node_mock = AsyncioMagicMock() diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index d73008e7..78920ce0 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -216,7 +216,8 @@ def test_add_node_from_appliance(async_run, controller): "server": "local", "name": "Test", "default_name_format": "{name}-{0}", - "node_type": "vpcs", + "appliance_type": "vpcs", + "builtin": False, "properties": { "a": 1 } From 98f04365b7ec2b2bbb044cb3d403f5bbc2ad5c63 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 14 Nov 2018 16:24:30 +0800 Subject: [PATCH 4/6] Remove generic controller settings API endpoint. --- conf/gns3_server.conf | 2 - gns3server/compute/iou/iou_vm.py | 22 ++++++- gns3server/controller/__init__.py | 58 +++++++------------ gns3server/controller/appliance.py | 5 +- gns3server/controller/node.py | 19 +++--- .../api/controller/appliance_handler.py | 5 ++ .../api/controller/gns3_vm_handler.py | 2 - .../handlers/api/controller/server_handler.py | 53 ++++++++--------- gns3server/schemas/appliance.py | 7 ++- gns3server/schemas/iou.py | 4 ++ .../schemas/iou_license.py | 34 +++++------ gns3server/schemas/label.py | 6 +- tests/conftest.py | 2 +- tests/controller/test_controller.py | 11 +--- tests/controller/test_node.py | 8 +-- 15 files changed, 120 insertions(+), 118 deletions(-) rename tests/handlers/api/controller/test_settings.py => gns3server/schemas/iou_license.py (54%) diff --git a/conf/gns3_server.conf b/conf/gns3_server.conf index bcf188a1..ffab4ec8 100644 --- a/conf/gns3_server.conf +++ b/conf/gns3_server.conf @@ -61,8 +61,6 @@ sparse_memory_support = True ghost_ios_support = True [IOU] -; iouyap executable path, default: search in PATH -;iouyap_path = iouyap ; Path of your .iourc file. If not provided, the file is searched in $HOME/.iourc iourc_path = /home/gns3/.iourc ; Validate if the iourc license file is correct. If you turn this off and your licence is invalid IOU will not start and no errors will be shown. diff --git a/gns3server/compute/iou/iou_vm.py b/gns3server/compute/iou/iou_vm.py index 7f22d055..170d3560 100644 --- a/gns3server/compute/iou/iou_vm.py +++ b/gns3server/compute/iou/iou_vm.py @@ -76,6 +76,7 @@ class IOUVM(BaseNode): self._started = False self._nvram_watcher = None self._path = self.manager.get_abs_image_path(path) + self._license_check = True # IOU settings self._ethernet_adapters = [] @@ -358,6 +359,16 @@ class IOUVM(BaseNode): except OSError as e: raise IOUError("Could not write the iourc file {}: {}".format(path, e)) + @property + def license_check(self): + + return self._license_check + + @license_check.setter + def license_check(self, value): + + self._license_check = value + async def _library_check(self): """ Checks for missing shared library dependencies in the IOU image. @@ -379,11 +390,18 @@ class IOUVM(BaseNode): """ Checks for a valid IOU key in the iourc file (paranoid mode). """ + + # license check is sent by the controller + if self.license_check is False: + return + try: - license_check = self._config().getboolean("license_check", True) + # we allow license check to be disabled server wide + server_wide_license_check = self._config().getboolean("license_check", True) except ValueError: raise IOUError("Invalid licence check setting") - if license_check is False: + + if server_wide_license_check is False: return config = configparser.ConfigParser() diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 242f80a9..da316b52 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -54,9 +54,9 @@ class Controller: self._notification = Notification(self) self.gns3vm = GNS3VM(self) self.symbols = Symbols() - - # FIXME: store settings shared by the different GUI will be replace by dedicated API later - self._settings = None + self._iou_license_settings = {"iourc_content": "", + "license_check": True} + self._config_loaded = False self._appliances = {} self._appliance_templates = {} self._appliance_templates_etag = None @@ -150,6 +150,7 @@ class Controller: raise aiohttp.web.HTTPConflict(text="Cannot create new appliance: key '{}' is missing for appliance ID '{}'".format(e, appliance_id)) self._appliances[appliance.id] = appliance self.save() + self.notification.controller_emit("appliance.created", appliance.__json__()) return appliance def get_appliance(self, appliance_id): @@ -173,13 +174,12 @@ class Controller: :param appliance_id: appliance identifier """ - appliance = self._appliances.get(appliance_id) - if not appliance: - raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id)) + appliance = self.get_appliance(appliance_id) if appliance.builtin: raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id)) self._appliances.pop(appliance_id) self.save() + self.notification.controller_emit("appliance.deleted", appliance.__json__()) def load_appliances(self): @@ -231,7 +231,7 @@ class Controller: user=server_config.get("user", ""), password=server_config.get("password", ""), force=True) - except aiohttp.web.HTTPConflict as e: + except aiohttp.web.HTTPConflict: log.fatal("Cannot access to the local server, make sure something else is not running on the TCP port {}".format(port)) sys.exit(1) for c in computes: @@ -276,14 +276,13 @@ class Controller: Save the controller configuration on disk """ - # We don't save during the loading otherwise we could lost stuff - if self._settings is None: + if self._config_loaded is False: return controller_settings = {"computes": [], - "settings": self._settings, "appliances": [], "gns3vm": self.gns3vm.__json__(), + "iou_license": self._iou_license_settings, "appliance_templates_etag": self._appliance_templates_etag, "version": __version__} @@ -321,14 +320,8 @@ class Controller: controller_settings = json.load(f) except (OSError, ValueError) as e: log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e)) - self._settings = {} return [] - if "settings" in controller_settings and controller_settings["settings"] is not None: - self._settings = controller_settings["settings"] - else: - self._settings = {} - # load the appliances if "appliances" in controller_settings: for appliance_settings in controller_settings["appliances"]: @@ -345,9 +338,14 @@ class Controller: if "gns3vm" in controller_settings: self.gns3vm.settings = controller_settings["gns3vm"] + # load the IOU license settings + if "iou_license" in controller_settings: + self._iou_license_settings = controller_settings["iou_license"] + self._appliance_templates_etag = controller_settings.get("appliance_templates_etag") self.load_appliance_templates() self.load_appliances() + self._config_loaded = True return controller_settings.get("computes", []) async def load_projects(self): @@ -528,26 +526,6 @@ class Controller: log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(vm["appliance_id"], vm.get("name", "unknown"), e)) continue - self._settings = {} - - @property - def settings(self): - """ - Store settings shared by the different GUI will be replace by dedicated API later. Dictionnary - """ - - return self._settings - - @settings.setter - def settings(self, val): - - self._settings = val - self._settings["modification_uuid"] = str(uuid.uuid4()) # We add a modification id to the settings to help the gui to detect changes - self.save() - self.load_appliance_templates() - self.load_appliances() - self.notification.controller_emit("settings.updated", val) - async def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs): """ Add a server to the dictionary of compute servers controlled by this controller @@ -783,6 +761,14 @@ class Controller: return self._appliances + @property + def iou_license(self): + """ + :returns: The dictionary of IOU license settings + """ + + return self._iou_license_settings + def projects_directory(self): server_config = Config.instance().get_section_config("Server") diff --git a/gns3server/controller/appliance.py b/gns3server/controller/appliance.py index 5616bf77..ecc304ce 100644 --- a/gns3server/controller/appliance.py +++ b/gns3server/controller/appliance.py @@ -96,8 +96,11 @@ class Appliance: def update(self, **kwargs): - #TODO: do not update appliance_id, builtin or appliance_type self._settings.update(kwargs) + from gns3server.controller import Controller + controller = Controller.instance() + controller.notification.controller_emit("appliance.updated", self.__json__()) + controller.save() def __json__(self): """ diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 95227606..2e9047bc 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -271,16 +271,17 @@ class Node: if self._label is None: # Apply to label user style or default try: - style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], - self._project.controller.settings["GraphicsView"]["default_label_color"]) + style = None # FIXME: allow configuration of default label font & color on controller + #style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], + # self._project.controller.settings["GraphicsView"]["default_label_color"]) except KeyError: - style = "font-size: 10;font-familly: Verdana" + style = "font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;" self._label = { "y": round(self._height / 2 + 10) * -1, "text": html.escape(self._name), - "style": style, - "x": None, # None: mean the client should center it + "style": style, # None: means the client will apply its default style + "x": None, # None: means the client should center it "rotation": 0 } @@ -483,11 +484,11 @@ class Node: try: # For IOU we need to send the licence everytime if self.node_type == "iou": - try: - licence = self._project.controller.settings["IOU"]["iourc_content"] - except KeyError: + license_check = self._project.controller.iou_license.get("license_check", True) + iourc_content = self._project.controller.iou_license.get("iourc_content", None) + if license_check and not iourc_content: raise aiohttp.web.HTTPConflict(text="IOU licence is not configured") - await self.post("/start", timeout=240, data={"iourc_content": licence}) + await self.post("/start", timeout=240, data={"license_check": license_check, "iourc_content": iourc_content}) else: await self.post("/start", data=data, timeout=240) except asyncio.TimeoutError: diff --git a/gns3server/handlers/api/controller/appliance_handler.py b/gns3server/handlers/api/controller/appliance_handler.py index 06e6f56c..4c3115f7 100644 --- a/gns3server/handlers/api/controller/appliance_handler.py +++ b/gns3server/handlers/api/controller/appliance_handler.py @@ -103,6 +103,11 @@ class ApplianceHandler: controller = Controller.instance() appliance = controller.get_appliance(request.match_info["appliance_id"]) + # Ignore these because we only use them when creating a appliance + request.json.pop("appliance_id", None) + request.json.pop("appliance_type", None) + request.json.pop("compute_id", None) + request.json.pop("builtin", None) appliance.update(**request.json) response.set_status(200) response.json(appliance) diff --git a/gns3server/handlers/api/controller/gns3_vm_handler.py b/gns3server/handlers/api/controller/gns3_vm_handler.py index 5371e3d0..d1c56b6a 100644 --- a/gns3server/handlers/api/controller/gns3_vm_handler.py +++ b/gns3server/handlers/api/controller/gns3_vm_handler.py @@ -15,12 +15,10 @@ # 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 gns3server.web.route import Route from gns3server.controller import Controller from gns3server.schemas.gns3vm import GNS3VM_SETTINGS_SCHEMA - import logging log = logging.getLogger(__name__) diff --git a/gns3server/handlers/api/controller/server_handler.py b/gns3server/handlers/api/controller/server_handler.py index a7422816..ae1160c4 100644 --- a/gns3server/handlers/api/controller/server_handler.py +++ b/gns3server/handlers/api/controller/server_handler.py @@ -19,6 +19,7 @@ from gns3server.web.route import Route from gns3server.config import Config from gns3server.controller import Controller from gns3server.schemas.version import VERSION_SCHEMA +from gns3server.schemas.iou_license import IOU_LICENSE_SETTINGS_SCHEMA from gns3server.version import __version__ from aiohttp.web import HTTPConflict, HTTPForbidden @@ -102,37 +103,31 @@ class ServerHandler: response.json({"version": __version__}) @Route.get( - r"/settings", - description="Retrieve gui settings from the server. Temporary will we removed in later release") - async def read_settings(request, response): - - settings = None - while True: - # The init of the server could take some times - # we ensure settings are loaded before returning them - settings = Controller.instance().settings - - if settings is not None: - break - await asyncio.sleep(0.5) - response.json(settings) - - @Route.post( - r"/settings", - description="Write gui settings on the server. Temporary will we removed in later releases", + r"/iou_license", + description="Get the IOU license settings", status_codes={ - 201: "Settings saved" + 200: "IOU license settings returned" + }, + output_schema=IOU_LICENSE_SETTINGS_SCHEMA) + def show(request, response): + + response.json(Controller.instance().iou_license) + + @Route.put( + r"/iou_license", + description="Update the IOU license settings", + input_schema=IOU_LICENSE_SETTINGS_SCHEMA, + output_schema=IOU_LICENSE_SETTINGS_SCHEMA, + status_codes={ + 201: "IOU license settings updated" }) - def write_settings(request, response): - controller = Controller.instance() - if controller.settings is None: # Server is not loaded ignore settings update to prevent buggy client sync issue - return - try: - controller.settings = request.json - #controller.save() - except (OSError, PermissionError) as e: - raise HTTPConflict(text="Can't save the settings {}".format(str(e))) - response.json(controller.settings) + async def update(request, response): + + controller = Controller().instance() + iou_license = controller.iou_license + iou_license.update(request.json) + controller.save() + response.json(iou_license) response.set_status(201) @Route.post( diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index 2cbb9ea8..408e33c4 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -58,13 +58,16 @@ APPLIANCE_OBJECT_SCHEMA = { } APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) + +# create schema # these properties are not required to create an appliance APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id") APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id") APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format") APPLIANCE_CREATE_SCHEMA["required"].remove("symbol") -APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_CREATE_SCHEMA) -#APPLIANCE_UPDATE_SCHEMA["additionalProperties"] = False + +# update schema +APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) del APPLIANCE_UPDATE_SCHEMA["required"] APPLIANCE_USAGE_SCHEMA = { diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 3243c890..8bad7a9e 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -104,6 +104,10 @@ IOU_START_SCHEMA = { "iourc_content": { "description": "Content of the iourc file. Ignored if Null", "type": ["string", "null"] + }, + "license_check": { + "description": "Whether the license should be checked", + "type": "boolean" } } } diff --git a/tests/handlers/api/controller/test_settings.py b/gns3server/schemas/iou_license.py similarity index 54% rename from tests/handlers/api/controller/test_settings.py rename to gns3server/schemas/iou_license.py index a87df472..4b2262cf 100644 --- a/tests/handlers/api/controller/test_settings.py +++ b/gns3server/schemas/iou_license.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2015 GNS3 Technologies Inc. +# Copyright (C) 2018 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 @@ -15,19 +15,19 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" -This test suite check /version endpoint -It's also used for unittest the HTTP implementation. -""" - -from gns3server.config import Config - - -def test_settings(http_controller): - query = {"test": True} - response = http_controller.post('/settings', query, example=True) - assert response.status == 201 - response = http_controller.get('/settings', example=True) - assert response.status == 200 - assert response.json["test"] is True - assert response.json["modification_uuid"] is not None +IOU_LICENSE_SETTINGS_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "IOU license", + "type": "object", + "properties": { + "iourc_content": { + "type": "string", + "description": "Content of iourc file" + }, + "license_check": { + "type": "boolean", + "description": "Whether the license must be checked or not", + }, + }, + "additionalProperties": False +} diff --git a/gns3server/schemas/label.py b/gns3server/schemas/label.py index eae97417..eb5c2de1 100644 --- a/gns3server/schemas/label.py +++ b/gns3server/schemas/label.py @@ -20,11 +20,11 @@ LABEL_OBJECT_SCHEMA = { "properties": { "text": {"type": "string"}, "style": { - "description": "SVG style attribute", - "type": "string" + "description": "SVG style attribute. Apply default style if null", + "type": ["string", "null"] }, "x": { - "description": "Relative X position of the label. If null center it", + "description": "Relative X position of the label. Center it if null", "type": ["integer", "null"] }, "y": { diff --git a/tests/conftest.py b/tests/conftest.py index bb24dca5..2c4260c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -201,7 +201,7 @@ def controller(tmpdir, controller_config_path): Controller._instance = None controller = Controller.instance() controller._config_file = controller_config_path - controller._settings = {} + controller._config_loaded = True return controller diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 18e87fd6..0d99a534 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -35,7 +35,7 @@ def test_save(controller, controller_config_path): data = json.load(f) assert data["computes"] == [] assert data["version"] == __version__ - assert data["settings"] == {} + assert data["iou_license"] == controller.iou_license assert data["gns3vm"] == controller.gns3vm.__json__() @@ -53,12 +53,10 @@ def test_load_controller_settings(controller, controller_config_path, async_run) "compute_id": "test1" } ] - data["settings"] = {"IOU": {"test": True}} data["gns3vm"] = {"vmname": "Test VM"} with open(controller_config_path, "w+") as f: json.dump(data, f) assert len(async_run(controller._load_controller_settings())) == 1 - assert controller.settings["IOU"] assert controller.gns3vm.settings["vmname"] == "Test VM" @@ -199,13 +197,6 @@ def test_import_remote_gns3vm_1_x(controller, controller_config_path, async_run) assert controller.gns3vm.settings["vmname"] == "http://127.0.0.1:3081" -def test_settings(controller): - controller._notification = MagicMock() - controller.settings = {"a": 1} - controller._notification.controller_emit.assert_called_with("settings.updated", controller.settings) - assert controller.settings["modification_uuid"] is not None - - def test_load_projects(controller, projects_dir, async_run): controller.save() diff --git a/tests/controller/test_node.py b/tests/controller/test_node.py index a80bc9a1..715cdbbf 100644 --- a/tests/controller/test_node.py +++ b/tests/controller/test_node.py @@ -269,7 +269,7 @@ def test_symbol(node, symbols_dir): assert node.height == 71 assert node.label["x"] is None assert node.label["y"] == -40 - assert node.label["style"] == "font-size: 10;font-familly: Verdana" + assert node.label["style"] == None#"font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;" shutil.copy(os.path.join("gns3server", "symbols", "cloud.svg"), os.path.join(symbols_dir, "cloud2.svg")) node.symbol = "cloud2.svg" @@ -298,7 +298,7 @@ def test_label_with_default_label_font(node): node._label = None node.symbol = ":/symbols/dslam.svg" - assert node.label["style"] == "font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;" + assert node.label["style"] == None #"font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;" def test_update(node, compute, project, async_run, controller): @@ -405,9 +405,9 @@ def test_start_iou(compute, project, async_run, controller): with pytest.raises(aiohttp.web.HTTPConflict): async_run(node.start()) - controller.settings["IOU"] = {"iourc_content": "aa"} + controller._iou_license_settings = {"license_check": True, "iourc_content": "aa"} async_run(node.start()) - compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"iourc_content": "aa"}) + compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"license_check": True, "iourc_content": "aa"}) def test_stop(node, compute, project, async_run): From 9aafa4e62f7c81bab80c8756c148cc2feffac0b1 Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 15 Nov 2018 17:28:17 +0700 Subject: [PATCH 5/6] Schema validation for appliance API. Ref #1427. --- gns3server/schemas/appliance.py | 836 +++++++++++++++++++++++++++++- gns3server/schemas/dynamips_vm.py | 39 ++ 2 files changed, 848 insertions(+), 27 deletions(-) diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index 408e33c4..e52c1242 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -16,45 +16,827 @@ # along with this program. If not, see . import copy -from .node import NODE_TYPE_SCHEMA +from .dynamips_vm import DYNAMIPS_ADAPTERS, DYNAMIPS_WICS +from .qemu import QEMU_PLATFORMS +from .port import PORT_OBJECT_SCHEMA +from .custom_adapters import CUSTOM_ADAPTERS_ARRAY_SCHEMA -APPLIANCE_TYPE_SCHEMA = NODE_TYPE_SCHEMA + +BASE_APPLIANCE_PROPERTIES = { + "appliance_id": { + "description": "Appliance UUID from which the node has been created. Read only", + "type": ["null", "string"], + "minLength": 36, + "maxLength": 36, + "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" + }, + "compute_id": { + "description": "Compute identifier", + "type": "string" + }, + "category": { + "description": "Appliance category", + "anyOf": [ + {"type": "integer"}, # old category support + {"enum": ["router", "switch", "guest", "firewall"]} + ] + }, + "name": { + "description": "Appliance name", + "type": "string", + "minLength": 1, + }, + "default_name_format": { + "description": "Default name format", + "type": "string", + "minLength": 1, + }, + "symbol": { + "description": "Symbol of the appliance", + "type": "string", + "minLength": 1 + }, + "builtin": { + "description": "Appliance is builtin", + "type": "boolean" + }, +} + +#TODO: improve schema for Dynamips (match platform specific options, e.g. NPE allowd only for c7200) +DYNAMIPS_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["dynamips"] + }, + "image": { + "description": "Path to the IOS image", + "type": "string", + "minLength": 1 + }, + "chassis": { + "description": "Chassis type", + "enum": ["1720","1721", "1750", "1751", "1760", "2610", "2620", "2610XM", "2620XM", "2650XM", "2621", "2611XM", + "2621XM", "2651XM", "3620", "3640", "3660", ""] + }, + "platform": { + "description": "Platform type", + "enum": ["c1700", "c2600", "c2691", "c3725", "c3745", "c3600", "c7200"] + }, + "ram": { + "description": "Amount of RAM in MB", + "type": "integer" + }, + "nvram": { + "description": "Amount of NVRAM in KB", + "type": "integer" + }, + "mmap": { + "description": "MMAP feature", + "type": "boolean" + }, + "sparsemem": { + "description": "Sparse memory feature", + "type": "boolean" + }, + "exec_area": { + "description": "Exec area value", + "type": "integer", + }, + "disk0": { + "description": "Disk0 size in MB", + "type": "integer" + }, + "disk1": { + "description": "Disk1 size in MB", + "type": "integer" + }, + "mac_addr": { + "description": "Base MAC address", + "type": "string", + "anyOf": [ + {"pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$"}, + {"pattern": "^$"} + ] + }, + "system_id": { + "description": "System ID", + "type": "string", + "minLength": 1, + }, + "startup_config": { + "description": "IOS startup configuration file", + "type": "string" + }, + "private_config": { + "description": "IOS private configuration file", + "type": "string" + }, + "idlepc": { + "description": "Idle-PC value", + "type": "string", + "pattern": "^(0x[0-9a-fA-F]+)?$" + }, + "idlemax": { + "description": "Idlemax value", + "type": "integer", + }, + "idlesleep": { + "description": "Idlesleep value", + "type": "integer", + }, + "iomem": { + "description": "I/O memory percentage", + "type": "integer", + "minimum": 0, + "maximum": 100 + }, + "npe": { + "description": "NPE model", + "enum": ["npe-100", + "npe-150", + "npe-175", + "npe-200", + "npe-225", + "npe-300", + "npe-400", + "npe-g2"] + }, + "midplane": { + "description": "Midplane model", + "enum": ["std", "vxr"] + }, + "auto_delete_disks": { + "description": "Automatically delete nvram and disk files", + "type": "boolean" + }, + "wic0": DYNAMIPS_WICS, + "wic1": DYNAMIPS_WICS, + "wic2": DYNAMIPS_WICS, + "slot0": DYNAMIPS_ADAPTERS, + "slot1": DYNAMIPS_ADAPTERS, + "slot2": DYNAMIPS_ADAPTERS, + "slot3": DYNAMIPS_ADAPTERS, + "slot4": DYNAMIPS_ADAPTERS, + "slot5": DYNAMIPS_ADAPTERS, + "slot6": DYNAMIPS_ADAPTERS, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + } +} + +DYNAMIPS_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +IOU_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["iou"] + }, + "path": { + "description": "Path of IOU executable", + "type": "string", + "minLength": 1 + }, + "ethernet_adapters": { + "description": "Number of ethernet adapters", + "type": "integer", + }, + "serial_adapters": { + "description": "Number of serial adapters", + "type": "integer" + }, + "ram": { + "description": "RAM in MB", + "type": "integer" + }, + "nvram": { + "description": "NVRAM in KB", + "type": "integer" + }, + "use_default_iou_values": { + "description": "Use default IOU values", + "type": "boolean" + }, + "startup_config": { + "description": "Startup-config of IOU", + "type": "string" + }, + "private_config": { + "description": "Private-config of IOU", + "type": "string" + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, +} + +IOU_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +DOCKER_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["docker"] + }, + "image": { + "description": "Docker image name", + "type": "string", + "minLength": 1 + }, + "adapters": { + "description": "Number of adapters", + "type": "integer", + "minimum": 0, + "maximum": 99 + }, + "start_command": { + "description": "Docker CMD entry", + "type": "string", + "minLength": 1 + }, + "environment": { + "description": "Docker environment variables", + "type": "string", + "minLength": 1 + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "vnc", "http", "https", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, + "console_http_port": { + "description": "Internal port in the container for the HTTP server", + "type": "integer", + "minimum": 1, + "maximum": 65535 + }, + "console_http_path": { + "description": "Path of the web interface", + "type": "string", + "minLength": 1 + }, + "console_resolution": { + "description": "Console resolution for VNC", + "type": "string", + "pattern": "^[0-9]+x[0-9]+$" + }, + "extra_hosts": { + "description": "Docker extra hosts (added to /etc/hosts)", + "type": "string", + "minLength": 1 + }, + "custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA +} + +DOCKER_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +QEMU_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["qemu"] + }, + "usage": { + "description": "How to use the Qemu VM", + "type": "string", + "minLength": 1 + }, + "qemu_path": { + "description": "Path to QEMU", + "type": ["string", "null"], + "minLength": 1, + }, + "platform": { + "description": "Platform to emulate", + "enum": QEMU_PLATFORMS + }, + "linked_clone": { + "description": "Whether the VM is a linked clone or not", + "type": "boolean" + }, + "ram": { + "description": "Amount of RAM in MB", + "type": "integer" + }, + "cpus": { + "description": "Number of vCPUs", + "type": "integer", + "minimum": 1, + "maximum": 255 + }, + "adapters": { + "description": "Number of adapters", + "type": "integer", + "minimum": 0, + "maximum": 275 + }, + "adapter_type": { + "description": "QEMU adapter type", + "type": "string", + "minLength": 1 + }, + "mac_address": { + "description": "QEMU MAC address", + "type": "string", + "minLength": 1, + "pattern": "^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$" + }, + "first_port_name": { + "description": "Optional name of the first networking port example: eth0", + "type": "string", + "minLength": 1 + }, + "port_name_format": { + "description": "Optional formatting of the networking port example: eth{0}", + "type": "string", + "minLength": 1 + }, + "port_segment_size": { + "description": "Optional port segment size. A port segment is a block of port. For example Ethernet0/0 Ethernet0/1 is the module 0 with a port segment size of 2", + "type": "integer" + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "vnc", "spice", "spice+agent", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, + "boot_priority": { + "description": "QEMU boot priority", + "enum": ["c", "d", "n", "cn", "cd", "dn", "dc", "nc", "nd"] + }, + "hda_disk_image": { + "description": "QEMU hda disk image path", + "type": "string", + "minLength": 1 + }, + "hda_disk_interface": { + "description": "QEMU hda interface", + "type": "string", + "minLength": 1 + }, + "hdb_disk_image": { + "description": "QEMU hdb disk image path", + "type": "string", + "minLength": 1 + }, + "hdb_disk_interface": { + "description": "QEMU hdb interface", + "type": "string", + "minLength": 1 + }, + "hdc_disk_image": { + "description": "QEMU hdc disk image path", + "type": "string", + "minLength": 1 + }, + "hdc_disk_interface": { + "description": "QEMU hdc interface", + "type": "string", + "minLength": 1 + }, + "hdd_disk_image": { + "description": "QEMU hdd disk image path", + "type": "string", + "minLength": 1 + }, + "hdd_disk_interface": { + "description": "QEMU hdd interface", + "type": "string", + "minLength": 1 + }, + "cdrom_image": { + "description": "QEMU cdrom image path", + "type": "string", + "minLength": 1 + }, + "initrd": { + "description": "QEMU initrd path", + "type": "string", + "minLength": 1 + }, + "kernel_image": { + "description": "QEMU kernel image path", + "type": "string", + "minLength": 1 + }, + "bios_image": { + "description": "QEMU bios image path", + "type": "string", + "minLength": 1 + }, + "kernel_command_line": { + "description": "QEMU kernel command line", + "type": "string", + "minLength": 1 + }, + "legacy_networking": { + "description": "Use QEMU legagy networking commands (-net syntax)", + "type": "boolean" + }, + "on_close": { + "description": "Action to execute on the VM is closed", + "enum": ["power_off", "shutdown_signal", "save_vm_state"], + }, + "cpu_throttling": { + "description": "Percentage of CPU allowed for QEMU", + "minimum": 0, + "maximum": 800, + "type": "integer" + }, + "process_priority": { + "description": "Process priority for QEMU", + "enum": ["realtime", "very high", "high", "normal", "low", "very low"] + }, + "options": { + "description": "Additional QEMU options", + "type": "string", + "minLength": 1 + }, + "custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA +} + +QEMU_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +VMWARE_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["vmware"] + }, + "vmx_path": { + "description": "Path to the vmx file", + "type": "string", + "minLength": 1, + }, + "linked_clone": { + "description": "Whether the VM is a linked clone or not", + "type": "boolean" + }, + "first_port_name": { + "description": "Optional name of the first networking port example: eth0", + "type": "string", + "minLength": 1 + }, + "port_name_format": { + "description": "Optional formatting of the networking port example: eth{0}", + "type": "string", + "minLength": 1 + }, + "port_segment_size": { + "description": "Optional port segment size. A port segment is a block of port. For example Ethernet0/0 Ethernet0/1 is the module 0 with a port segment size of 2", + "type": "integer" + }, + "adapters": { + "description": "Number of adapters", + "type": "integer", + "minimum": 0, + "maximum": 10, # maximum adapters support by VMware VMs + }, + "adapter_type": { + "description": "VMware adapter type", + "type": "string", + "minLength": 1, + }, + "use_any_adapter": { + "description": "Allow GNS3 to use any VMware adapter", + "type": "boolean", + }, + "headless": { + "description": "Headless mode", + "type": "boolean" + }, + "on_close": { + "description": "Action to execute on the VM is closed", + "enum": ["power_off", "shutdown_signal", "save_vm_state"], + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, + "custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA +} + +VMWARE_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +VIRTUALBOX_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["virtualbox"] + }, + "vmname": { + "description": "VirtualBox VM name (in VirtualBox itself)", + "type": "string", + "minLength": 1, + }, + "ram": { + "description": "Amount of RAM", + "minimum": 0, + "maximum": 65535, + "type": "integer" + }, + "linked_clone": { + "description": "Whether the VM is a linked clone or not", + "type": "boolean" + }, + "adapters": { + "description": "Number of adapters", + "type": "integer", + "minimum": 0, + "maximum": 36, # maximum given by the ICH9 chipset in VirtualBox + }, + "use_any_adapter": { + "description": "Allow GNS3 to use any VirtualBox adapter", + "type": "boolean", + }, + "adapter_type": { + "description": "VirtualBox adapter type", + "type": "string", + "minLength": 1, + }, + "first_port_name": { + "description": "Optional name of the first networking port example: eth0", + "type": "string", + "minLength": 1 + }, + "port_name_format": { + "description": "Optional formatting of the networking port example: eth{0}", + "type": "string", + "minLength": 1 + }, + "port_segment_size": { + "description": "Optional port segment size. A port segment is a block of port. For example Ethernet0/0 Ethernet0/1 is the module 0 with a port segment size of 2", + "type": "integer" + }, + "headless": { + "description": "Headless mode", + "type": "boolean" + }, + "on_close": { + "description": "Action to execute on the VM is closed", + "enum": ["power_off", "shutdown_signal", "save_vm_state"], + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, + "custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA +} + +VIRTUALBOX_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +TRACENG_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["traceng"] + }, + "ip_address": { + "description": "Source IP address for tracing", + "type": ["string"], + "minLength": 1 + }, + "default_destination": { + "description": "Default destination IP address or hostname for tracing", + "type": ["string"], + "minLength": 1 + }, + "console_type": { + "description": "Console type", + "enum": ["none"] + }, +} + +TRACENG_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +VPCS_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["vpcs"] + }, + "base_script_file": { + "description": "Script file", + "type": "string", + "minLength": 1, + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, + "console_auto_start": { + "description": "Automatically start the console when the node has started", + "type": "boolean" + }, +} + +VPCS_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +ETHERNET_SWITCH_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["ethernet_switch"] + }, + "ports_mapping": { + "type": "array", + "items": [ + {"type": "object", + "oneOf": [ + { + "description": "Ethernet port", + "properties": { + "name": { + "description": "Port name", + "type": "string", + "minLength": 1 + }, + "port_number": { + "description": "Port number", + "type": "integer", + "minimum": 0 + }, + "type": { + "description": "Port type", + "enum": ["access", "dot1q", "qinq"], + }, + "vlan": {"description": "VLAN number", + "type": "integer", + "minimum": 1 + }, + "ethertype": { + "description": "QinQ Ethertype", + "enum": ["", "0x8100", "0x88A8", "0x9100", "0x9200"], + }, + }, + "required": ["name", "port_number", "type"], + "additionalProperties": False + }, + ]}, + ] + }, + "console_type": { + "description": "Console type", + "enum": ["telnet", "none"] + }, +} + +ETHERNET_SWITCH_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +ETHERNET_HUB_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["ethernet_hub"] + }, + "ports_mapping": { + "type": "array", + "items": [ + {"type": "object", + "oneOf": [ + { + "description": "Ethernet port", + "properties": { + "name": { + "description": "Port name", + "type": "string", + "minLength": 1 + }, + "port_number": { + "description": "Port number", + "type": "integer", + "minimum": 0 + }, + }, + "required": ["name", "port_number"], + "additionalProperties": False + }, + ]}, + ] + } +} + +ETHERNET_HUB_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) + +CLOUD_APPLIANCE_PROPERTIES = { + "appliance_type": { + "enum": ["cloud"] + }, + "ports_mapping": { + "type": "array", + "items": [ + PORT_OBJECT_SCHEMA + ] + }, + "remote_console_host": { + "description": "Remote console host or IP", + "type": ["string"], + "minLength": 1 + }, + "remote_console_port": { + "description": "Console TCP port", + "minimum": 1, + "maximum": 65535, + "type": "integer" + }, + "remote_console_type": { + "description": "Console type", + "enum": ["telnet", "vnc", "spice", "http", "https", "none"] + }, + "remote_console_http_path": { + "description": "Path of the remote web interface", + "type": "string", + "minLength": 1 + }, +} + +CLOUD_APPLIANCE_PROPERTIES.update(BASE_APPLIANCE_PROPERTIES) APPLIANCE_OBJECT_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "A template object", "type": "object", - "properties": { - "appliance_id": { - "description": "Appliance UUID from which the node has been created. Read only", - "type": ["null", "string"], - "minLength": 36, - "maxLength": 36, - "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" + "definitions": { + "Dynamips": { + "description": "Dynamips appliance", + "properties": DYNAMIPS_APPLIANCE_PROPERTIES, + #"additionalProperties": False, + "required": ["platform", "image", "ram"] }, - "compute_id": { - "description": "Compute identifier", - "type": "string" + "IOU": { + "description": "IOU appliance", + "properties": IOU_APPLIANCE_PROPERTIES, + "additionalProperties": False, + "required": ["path"] }, - "appliance_type": APPLIANCE_TYPE_SCHEMA, - "name": { - "description": "Appliance name", - "type": "string", - "minLength": 1, + "Docker": { + "description": "Docker appliance", + "properties": DOCKER_APPLIANCE_PROPERTIES, + "additionalProperties": False, + "required": ["image"] }, - "default_name_format": { - "description": "Default name format", - "type": "string", - "minLength": 1, + "Qemu": { + "description": "Qemu appliance", + "properties": QEMU_APPLIANCE_PROPERTIES, + "additionalProperties": False, }, - "symbol": { - "description": "Symbol of the appliance", - "type": "string", - "minLength": 1 + "VMware": { + "description": "VMware appliance", + "properties": VMWARE_APPLIANCE_PROPERTIES, + "additionalProperties": False, + "required": ["vmx_path", "linked_clone"] + }, + "VirtualBox": { + "description": "VirtualBox appliance", + "properties": VIRTUALBOX_APPLIANCE_PROPERTIES, + "additionalProperties": False, + "required": ["vmname"] + }, + "TraceNG": { + "description": "TraceNG appliance", + "properties": TRACENG_APPLIANCE_PROPERTIES, + "additionalProperties": False, + }, + "VPCS": { + "description": "VPCS appliance", + "properties": VPCS_APPLIANCE_PROPERTIES, + "additionalProperties": False, + }, + "EthernetSwitch": { + "description": "Ethernet switch appliance", + "properties": ETHERNET_SWITCH_APPLIANCE_PROPERTIES, + "additionalProperties": False, + }, + "EthernetHub": { + "description": "Ethernet hub appliance", + "properties": ETHERNET_HUB_APPLIANCE_PROPERTIES, + "additionalProperties": False, + }, + "Cloud": { + "description": "Cloud appliance", + "properties": CLOUD_APPLIANCE_PROPERTIES, + "additionalProperties": False, }, }, - "additionalProperties": True, #TODO: validate all properties - "required": ["appliance_id", "compute_id", "appliance_type", "name", "default_name_format", "symbol"] + "oneOf": [ + {"$ref": "#/definitions/Dynamips"}, + {"$ref": "#/definitions/IOU"}, + {"$ref": "#/definitions/Docker"}, + {"$ref": "#/definitions/Qemu"}, + {"$ref": "#/definitions/VMware"}, + {"$ref": "#/definitions/VirtualBox"}, + {"$ref": "#/definitions/TraceNG"}, + {"$ref": "#/definitions/VPCS"}, + {"$ref": "#/definitions/EthernetSwitch"}, + {"$ref": "#/definitions/EthernetHub"}, + {"$ref": "#/definitions/Cloud"}, + ], + "required": ["name", "appliance_id", "appliance_type", "category", "compute_id", "default_name_format", "symbol"] } APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) diff --git a/gns3server/schemas/dynamips_vm.py b/gns3server/schemas/dynamips_vm.py index a3cefb8d..3798f0e1 100644 --- a/gns3server/schemas/dynamips_vm.py +++ b/gns3server/schemas/dynamips_vm.py @@ -16,6 +16,45 @@ # along with this program. If not, see . +DYNAMIPS_ADAPTERS = { + "description": "Dynamips Network Module", + "enum": ["C7200-IO-2FE", + "C7200-IO-FE", + "C7200-IO-GE-E", + "NM-16ESW", + "NM-1E", + "NM-1FE-TX", + "NM-4E", + "NM-4T", + "PA-2FE-TX", + "PA-4E", + "PA-4T+", + "PA-8E", + "PA-8T", + "PA-A1", + "PA-FE-TX", + "PA-GE", + "PA-POS-OC3", + "C2600-MB-2FE", + "C2600-MB-1E", + "C1700-MB-1FE", + "C2600-MB-2E", + "C2600-MB-1FE", + "C1700-MB-WIC1", + "GT96100-FE", + "Leopard-2FE", + ""] +} + +DYNAMIPS_WICS = { + "description": "Dynamips WIC", + "enum": ["WIC-1ENET", + "WIC-1T", + "WIC-2T", + ""] +} + +#TODO: improve schema for Dynamips (match platform specific options, e.g. NPE allowd only for c7200) VM_CREATE_SCHEMA = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Request validation to create a new Dynamips VM instance", From 1184f29b90ac62343297713b64b48618de4001ba Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 15 Nov 2018 21:34:36 +0700 Subject: [PATCH 6/6] Add appliance API tests. Ref #1427. --- gns3server/schemas/appliance.py | 2 +- .../handlers/api/controller/test_appliance.py | 134 +++++++++++++++++- 2 files changed, 131 insertions(+), 5 deletions(-) diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index e52c1242..5ce51903 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -765,7 +765,7 @@ APPLIANCE_OBJECT_SCHEMA = { "Dynamips": { "description": "Dynamips appliance", "properties": DYNAMIPS_APPLIANCE_PROPERTIES, - #"additionalProperties": False, + "additionalProperties": False, "required": ["platform", "image", "ram"] }, "IOU": { diff --git a/tests/handlers/api/controller/test_appliance.py b/tests/handlers/api/controller/test_appliance.py index a0cadf23..05835fd3 100644 --- a/tests/handlers/api/controller/test_appliance.py +++ b/tests/handlers/api/controller/test_appliance.py @@ -45,12 +45,12 @@ def test_appliance_list(http_controller, controller): id = str(uuid.uuid4()) controller.load_appliances() controller._appliances[id] = Appliance(id, { - "node_type": "qemu", + "appliance_type": "qemu", "category": 0, "name": "test", "symbol": "guest.svg", "default_name_format": "{name}-{0}", - "server": "local" + "compute_id": "local" }) response = http_controller.get("/appliances", example=True) assert response.status == 200 @@ -66,16 +66,142 @@ def test_appliance_templates_list(http_controller, controller, async_run): assert len(response.json) > 0 +def test_cr(http_controller, controller, async_run): + + controller.load_appliance_templates() + response = http_controller.get("/appliances/templates", example=True) + assert response.status == 200 + assert len(response.json) > 0 + + +def test_appliance_create_without_id(http_controller, controller): + + params = {"base_script_file": "vpcs_base_config.txt", + "category": "guest", + "console_auto_start": False, + "console_type": "telnet", + "default_name_format": "PC{0}", + "name": "VPCS_TEST", + "compute_id": "local", + "symbol": ":/symbols/vpcs_guest.svg", + "appliance_type": "vpcs"} + + response = http_controller.post("/appliances", params, example=True) + assert response.status == 201 + assert response.route == "/appliances" + assert response.json["appliance_id"] is not None + assert len(controller.appliances) == 1 + + +def test_appliance_create_with_id(http_controller, controller): + + params = {"appliance_id": str(uuid.uuid4()), + "base_script_file": "vpcs_base_config.txt", + "category": "guest", + "console_auto_start": False, + "console_type": "telnet", + "default_name_format": "PC{0}", + "name": "VPCS_TEST", + "compute_id": "local", + "symbol": ":/symbols/vpcs_guest.svg", + "appliance_type": "vpcs"} + + response = http_controller.post("/appliances", params, example=True) + assert response.status == 201 + assert response.route == "/appliances" + assert response.json["appliance_id"] is not None + assert len(controller.appliances) == 1 + + +def test_appliance_get(http_controller, controller): + + appliance_id = str(uuid.uuid4()) + params = {"appliance_id": appliance_id, + "base_script_file": "vpcs_base_config.txt", + "category": "guest", + "console_auto_start": False, + "console_type": "telnet", + "default_name_format": "PC{0}", + "name": "VPCS_TEST", + "compute_id": "local", + "symbol": ":/symbols/vpcs_guest.svg", + "appliance_type": "vpcs"} + + response = http_controller.post("/appliances", params) + assert response.status == 201 + + response = http_controller.get("/appliances/{}".format(appliance_id), example=True) + assert response.status == 200 + assert response.json["appliance_id"] == appliance_id + + +def test_appliance_update(http_controller, controller): + + appliance_id = str(uuid.uuid4()) + params = {"appliance_id": appliance_id, + "base_script_file": "vpcs_base_config.txt", + "category": "guest", + "console_auto_start": False, + "console_type": "telnet", + "default_name_format": "PC{0}", + "name": "VPCS_TEST", + "compute_id": "local", + "symbol": ":/symbols/vpcs_guest.svg", + "appliance_type": "vpcs"} + + response = http_controller.post("/appliances", params) + assert response.status == 201 + + response = http_controller.get("/appliances/{}".format(appliance_id)) + assert response.status == 200 + assert response.json["appliance_id"] == appliance_id + + params["name"] = "VPCS_TEST_RENAMED" + response = http_controller.put("/appliances/{}".format(appliance_id), params, example=True) + + assert response.status == 200 + assert response.json["name"] == "VPCS_TEST_RENAMED" + + +def test_appliance_delete(http_controller, controller): + + appliance_id = str(uuid.uuid4()) + params = {"appliance_id": appliance_id, + "base_script_file": "vpcs_base_config.txt", + "category": "guest", + "console_auto_start": False, + "console_type": "telnet", + "default_name_format": "PC{0}", + "name": "VPCS_TEST", + "compute_id": "local", + "symbol": ":/symbols/vpcs_guest.svg", + "appliance_type": "vpcs"} + + response = http_controller.post("/appliances", params) + assert response.status == 201 + + response = http_controller.get("/appliances") + assert len(response.json) == 1 + assert len(controller.appliances) == 1 + + response = http_controller.delete("/appliances/{}".format(appliance_id), example=True) + assert response.status == 204 + + response = http_controller.get("/appliances") + assert len(response.json) == 0 + assert len(controller.appliances) == 0 + + def test_create_node_from_appliance(http_controller, controller, project, compute): id = str(uuid.uuid4()) controller._appliances = {id: Appliance(id, { - "node_type": "qemu", + "appliance_type": "qemu", "category": 0, "name": "test", "symbol": "guest.svg", "default_name_format": "{name}-{0}", - "server": "example.com" + "compute_id": "example.com" })} with asyncio_patch("gns3server.controller.project.Project.add_node_from_appliance") as mock: response = http_controller.post("/projects/{}/appliances/{}".format(project.id, id), {