mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-09-05 01:25:15 +03:00
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.
This commit is contained in:
parent
9721660cc3
commit
0ed1715fe6
@ -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")
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user