mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02: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:
2 files changed
+16
-9
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user