From 6289f81298ea48a09ef61eeb77e0b042236d9510 Mon Sep 17 00:00:00 2001 From: grossmj Date: Fri, 13 Nov 2026 12:08:36 +0700 Subject: [PATCH] feat: add support for symbol resolution based on active theme in appliance templates --- .../controller/appliance_to_template.py | 33 ++++------ gns3server/controller/symbol_themes.py | 7 ++ .../controller/test_appliance_to_template.py | 65 ++++++++++++++++--- 3 files changed, 78 insertions(+), 27 deletions(-) diff --git a/gns3server/controller/appliance_to_template.py b/gns3server/controller/appliance_to_template.py index 8f2f5e175..7d1b6c5b5 100644 --- a/gns3server/controller/appliance_to_template.py +++ b/gns3server/controller/appliance_to_template.py @@ -49,6 +49,12 @@ class ApplianceToTemplate: Appliance installation. """ + def __init__(self): + + from . import Controller + + self._controller = Controller.instance() + def new_template(self, appliance_config, version, server): """ Creates a new template from an appliance. @@ -89,17 +95,11 @@ class ApplianceToTemplate: if new_template.get("symbol") is None: if appliance_config["category"] == "guest": if "docker" in appliance_config: - new_template["symbol"] = ":/symbols/docker_guest.svg" + new_template["symbol"] = self._controller.symbols.resolve_symbol("docker_guest") else: - new_template["symbol"] = ":/symbols/qemu_guest.svg" - elif appliance_config["category"] == "router": - new_template["symbol"] = ":/symbols/router.svg" - elif appliance_config["category"] == "switch": - new_template["symbol"] = ":/symbols/ethernet_switch.svg" - elif appliance_config["category"] == "multilayer_switch": - new_template["symbol"] = ":/symbols/multilayer_switch.svg" - elif appliance_config["category"] == "firewall": - new_template["symbol"] = ":/symbols/firewall.svg" + new_template["symbol"] = self._controller.symbols.resolve_symbol("qemu_guest") + else: + new_template["symbol"] = self._controller.symbols.resolve_symbol(appliance_config.get("category")) if "qemu" in appliance_config: new_template["template_type"] = "qemu" @@ -114,6 +114,7 @@ class ApplianceToTemplate: new_template["template_type"] = "docker" self._add_docker_config(new_template, appliance_config) + print(new_template) return new_template def _add_qemu_config(self, new_config, appliance_config, version): @@ -225,17 +226,11 @@ class ApplianceToTemplate: # apply a default symbol based on the effective category and template type if category_before_remap == "guest": if settings["template_type"] == "docker": - new_template["symbol"] = ":/symbols/docker_guest.svg" + new_template["symbol"] = self._controller.symbols.resolve_symbol("docker_guest") else: - new_template["symbol"] = ":/symbols/qemu_guest.svg" + new_template["symbol"] = self._controller.symbols.resolve_symbol("qemu_guest") else: - symbols = { - "router": ":/symbols/router.svg", - "switch": ":/symbols/ethernet_switch.svg", - "multilayer_switch": ":/symbols/multilayer_switch.svg", - "firewall": ":/symbols/firewall.svg", - } - new_template["symbol"] = symbols.get(category_before_remap) + new_template["symbol"] = self._controller.symbols.resolve_symbol(category_before_remap) if version and version.get("images"): if settings["template_type"] == "iou": diff --git a/gns3server/controller/symbol_themes.py b/gns3server/controller/symbol_themes.py index 8d1a58277..7840ca891 100644 --- a/gns3server/controller/symbol_themes.py +++ b/gns3server/controller/symbol_themes.py @@ -20,6 +20,7 @@ CLASSIC_SYMBOL_THEME = { "cloud": ":/symbols/classic/cloud.svg", "nat": ":/symbols/classic/nat.svg", "ethernet_switch": ":/symbols/classic/ethernet_switch.svg", + "switch": ":/symbols/classic/ethernet_switch.svg", "hub": ":/symbols/classic/hub.svg", "frame_relay_switch": ":/symbols/classic/frame_relay_switch.svg", "atm_switch": ":/symbols/classic/atm_switch.svg", @@ -38,6 +39,7 @@ AFFINITY_SQUARE_BLUE_SYMBOL_THEME = { "cloud": ":/symbols/affinity/square/blue/cloud.svg", "nat": ":/symbols/affinity/square/blue/nat.svg", "ethernet_switch": ":/symbols/affinity/square/blue/switch.svg", + "switch": ":/symbols/affinity/square/blue/switch.svg", "hub": ":/symbols/affinity/square/blue/hub.svg", "frame_relay_switch": ":/symbols/affinity/square/blue/isdn.svg", "atm_switch": ":/symbols/affinity/square/blue/atm.svg", @@ -56,6 +58,7 @@ AFFINITY_SQUARE_RED_SYMBOL_THEME = { "cloud": ":/symbols/affinity/square/red/cloud.svg", "nat": ":/symbols/affinity/square/red/nat.svg", "ethernet_switch": ":/symbols/affinity/square/red/switch.svg", + "switch": ":/symbols/affinity/square/red/switch.svg", "hub": ":/symbols/affinity/square/red/hub.svg", "frame_relay_switch": ":/symbols/affinity/square/red/isdn.svg", "atm_switch": ":/symbols/affinity/square/red/atm.svg", @@ -74,6 +77,7 @@ AFFINITY_SQUARE_GRAY_SYMBOL_THEME = { "cloud": ":/symbols/affinity/square/gray/cloud.svg", "nat": ":/symbols/affinity/square/gray/nat.svg", "ethernet_switch": ":/symbols/affinity/square/gray/switch.svg", + "switch": ":/symbols/affinity/square/gray/switch.svg", "hub": ":/symbols/affinity/square/gray/hub.svg", "frame_relay_switch": ":/symbols/affinity/square/gray/isdn.svg", "atm_switch": ":/symbols/affinity/square/gray/atm.svg", @@ -92,6 +96,7 @@ AFFINITY_CIRCLE_BLUE_SYMBOL_THEME = { "cloud": ":/symbols/affinity/circle/blue/cloud.svg", "nat": ":/symbols/affinity/circle/blue/nat.svg", "ethernet_switch": ":/symbols/affinity/circle/blue/switch.svg", + "switch": ":/symbols/affinity/circle/blue/switch.svg", "hub": ":/symbols/affinity/circle/blue/hub.svg", "frame_relay_switch": ":/symbols/affinity/circle/blue/isdn.svg", "atm_switch": ":/symbols/affinity/circle/blue/atm.svg", @@ -110,6 +115,7 @@ AFFINITY_CIRCLE_RED_SYMBOL_THEME = { "cloud": ":/symbols/affinity/circle/red/cloud.svg", "nat": ":/symbols/affinity/circle/red/nat.svg", "ethernet_switch": ":/symbols/affinity/circle/red/switch.svg", + "switch": ":/symbols/affinity/circle/red/switch.svg", "hub": ":/symbols/affinity/circle/red/hub.svg", "frame_relay_switch": ":/symbols/affinity/circle/red/isdn.svg", "atm_switch": ":/symbols/affinity/circle/red/atm.svg", @@ -128,6 +134,7 @@ AFFINITY_CIRCLE_GRAY_SYMBOL_THEME = { "cloud": ":/symbols/affinity/circle/gray/cloud.svg", "nat": ":/symbols/affinity/circle/gray/nat.svg", "ethernet_switch": ":/symbols/affinity/circle/gray/switch.svg", + "switch": ":/symbols/affinity/circle/gray/switch.svg", "hub": ":/symbols/affinity/circle/gray/hub.svg", "frame_relay_switch": ":/symbols/affinity/circle/gray/isdn.svg", "atm_switch": ":/symbols/affinity/circle/gray/atm.svg", diff --git a/tests/controller/test_appliance_to_template.py b/tests/controller/test_appliance_to_template.py index a488c2fa0..9940297c2 100644 --- a/tests/controller/test_appliance_to_template.py +++ b/tests/controller/test_appliance_to_template.py @@ -205,11 +205,12 @@ def test_v8_multilayer_switch_category_mapping(): assert template["category"] == "switch" -def test_v8_default_symbol_fallback(): +def test_v8_default_symbol_fallback(controller): """ Without any symbol, a docker guest gets the docker symbol, other guests the qemu one. """ + controller.symbols.theme = "Classic" appliance = { "registry_version": 8, "name": "Test", @@ -217,14 +218,38 @@ def test_v8_default_symbol_fallback(): "settings": [{"name": "only", "template_type": "docker", "template_properties": {"image": "test:latest"}}], } template = ApplianceToTemplate().new_template(appliance, None, "local") - assert template["symbol"] == ":/symbols/docker_guest.svg" + assert template["symbol"] == ":/symbols/classic/docker_guest.svg" assert template["template_type"] == "docker" assert template["image"] == "test:latest" appliance["settings"][0]["template_type"] = "qemu" appliance["settings"][0]["template_properties"] = {"ram": 512} template = ApplianceToTemplate().new_template(appliance, None, "local") - assert template["symbol"] == ":/symbols/qemu_guest.svg" + assert template["symbol"] == ":/symbols/classic/qemu_guest.svg" + + +@pytest.mark.parametrize( + ("category", "template_type", "expected_symbol"), + [ + ("router", "qemu", ":/symbols/affinity/square/blue/router.svg"), + ("switch", "qemu", ":/symbols/affinity/square/blue/switch.svg"), + ("guest", "docker", ":/symbols/affinity/square/blue/docker.svg"), + ("guest", "qemu", ":/symbols/affinity/square/blue/client_vm.svg"), + ], +) +def test_v8_default_symbol_uses_active_theme(controller, category, template_type, expected_symbol): + controller.symbols.theme = "Affinity-square-blue" + properties = {"ram": 512} if template_type == "qemu" else {"image": "test:latest"} + appliance = { + "registry_version": 8, + "name": "Test", + "category": category, + "settings": [{"name": "only", "template_type": template_type, "template_properties": properties}], + } + + template = ApplianceToTemplate().new_template(appliance, None, "local") + + assert template["symbol"] == expected_symbol def test_v8_no_inheritance_when_disabled(): @@ -262,7 +287,8 @@ def test_v8_multiple_settings_without_default_raises(): ApplianceToTemplate().new_template(appliance, VYOS_V8["versions"][1], "local") -def test_v8_single_settings_selected_without_default_flag(): +def test_v8_single_settings_selected_without_default_flag(controller): + controller.symbols.theme = "Classic" appliance = { "registry_version": 8, "name": "Test", @@ -271,7 +297,7 @@ def test_v8_single_settings_selected_without_default_flag(): } template = ApplianceToTemplate().new_template(appliance, None, "local") assert template["ram"] == 1024 - assert template["symbol"] == ":/symbols/router.svg" + assert template["symbol"] == ":/symbols/classic/router.svg" def test_v6_path_unchanged(): @@ -315,6 +341,28 @@ def test_v6_netmiko_device_type_copied_to_template(): assert template["netmiko_device_type"] == "nokia_srl" +@pytest.mark.parametrize( + ("category", "template_type", "expected_symbol"), + [ + ("router", "qemu", ":/symbols/affinity/square/blue/router.svg"), + ("guest", "qemu", ":/symbols/affinity/square/blue/client_vm.svg"), + ("guest", "docker", ":/symbols/affinity/square/blue/docker.svg"), + ], +) +def test_v6_default_symbol_uses_active_theme(controller, category, template_type, expected_symbol): + controller.symbols.theme = "Affinity-square-blue" + appliance = { + "registry_version": 6, + "name": "Test", + "category": category, + template_type: {"ram": 512} if template_type == "qemu" else {"image": "test:latest"}, + } + + template = ApplianceToTemplate().new_template(appliance, {"images": {}}, "local") + + assert template["symbol"] == expected_symbol + + def test_v8_iou_version_images_mapped_to_path(): """ An IOU template takes the image as a path: the version image name must be @@ -478,12 +526,13 @@ def test_v8_multiple_defaults_inherit_first_same_type(): assert template["cpus"] == 2 -def test_v8_symbol_fallback_uses_effective_category(): +def test_v8_symbol_fallback_uses_effective_category(controller): """ The default symbol must reflect the effective category (template_properties, then version, then appliance), not the appliance-level one. """ + controller.symbols.theme = "Classic" appliance = { "registry_version": 8, "name": "Test", @@ -494,7 +543,7 @@ def test_v8_symbol_fallback_uses_effective_category(): } template = ApplianceToTemplate().new_template(appliance, None, "local") assert template["category"] == "guest" - assert template["symbol"] == ":/symbols/qemu_guest.svg" + assert template["symbol"] == ":/symbols/classic/qemu_guest.svg" appliance = { "registry_version": 8, @@ -507,7 +556,7 @@ def test_v8_symbol_fallback_uses_effective_category(): version = {"name": "1.0", "category": "router"} template = ApplianceToTemplate().new_template(appliance, version, "local") assert template["category"] == "router" - assert template["symbol"] == ":/symbols/router.svg" + assert template["symbol"] == ":/symbols/classic/router.svg" def test_v8_reserved_keys_cannot_be_overridden():