From 0ed1715fe667864308388c2aec9b1c468e75794f Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Fri, 10 Jul 2026 14:55:21 +0800 Subject: [PATCH] Use typed InterfaceStatus enum and simplify stats fallback in interfaces() - Change `status: str` to `status: InterfaceStatus` (str Enum) in the HostInterface schema, matching the existing IPAddressFamily pattern. - Use default-then-override pattern in interfaces() to eliminate the `else` branch and deduplicate defaults. --- gns3server/schemas/compute/cloud_nodes.py | 8 +++++++- gns3server/utils/interfaces.py | 17 +++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/gns3server/schemas/compute/cloud_nodes.py b/gns3server/schemas/compute/cloud_nodes.py index 1f2877fc6..28e01c72d 100644 --- a/gns3server/schemas/compute/cloud_nodes.py +++ b/gns3server/schemas/compute/cloud_nodes.py @@ -34,6 +34,12 @@ class IPAddressFamily(str, Enum): ipv6 = "ipv6" +class InterfaceStatus(str, Enum): + + up = "up" + down = "down" + + class HostInterfaceIPAddress(BaseModel): """ An IP address (with optional netmask) bound to a host interface. @@ -55,7 +61,7 @@ class HostInterface(BaseModel): ip_addresses: List[HostInterfaceIPAddress] = Field( default_factory=list, description="All IPv4 and IPv6 addresses on this interface" ) - status: str = Field("down", description="Interface status (up or down)") + status: InterfaceStatus = Field(InterfaceStatus.down, description="Interface status (up or down)") speed: int = Field(0, description="Interface speed in Mbit/s (0 if unknown)") mtu: int = Field(0, description="Interface MTU") flags: List[str] = Field(default_factory=list, description="Interface flags") diff --git a/gns3server/utils/interfaces.py b/gns3server/utils/interfaces.py index e7292bea5..5564e2deb 100644 --- a/gns3server/utils/interfaces.py +++ b/gns3server/utils/interfaces.py @@ -233,6 +233,10 @@ def interfaces(): # Operational state and link attributes (speed/mtu/flags) come from # psutil.net_if_stats(). An interface present in net_if_addrs is normally # also present here; fall back to neutral defaults when it is not. + status = "down" + speed = 0 + mtu = 0 + flags = [] stats = net_if_stats.get(interface) if stats is not None: status = "up" if stats.isup else "down" @@ -240,14 +244,11 @@ def interfaces(): mtu = stats.mtu # psutil returns flags either as a comma-separated string (>= 6.0) or # as a list (older versions); normalize to a list for a stable shape. - flags = stats.flags - if isinstance(flags, str): - flags = [flag for flag in flags.split(",") if flag] - else: - status = "down" - speed = 0 - mtu = 0 - flags = [] + f = stats.flags + if isinstance(f, str): + flags = [flag for flag in f.split(",") if flag] + else: + flags = f results.append( { "id": interface,