Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-133152 / 25.04 / remove bidict from routing.py (and simplify dramatically) #15233

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/middlewared/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Build-Depends: alembic,
python3-apps-validation,
python3-asyncssh,
python3-aws-requests-auth,
python3-bidict,
python3-boto3,
python3-certbot-dns-cloudflare,
python3-certbot-dns-digitalocean,
Expand Down Expand Up @@ -108,7 +107,6 @@ Depends: alembic,
python3-apps-validation,
python3-asyncssh,
python3-aws-requests-auth,
python3-bidict,
python3-boto3,
python3-certbot-dns-cloudflare,
python3-certbot-dns-digitalocean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding=utf-8 -*-
import collections
import enum
import ipaddress
import logging
import os
import socket

import bidict
from pyroute2 import IPRoute
from pyroute2.netlink.exceptions import NetlinkDumpInterrupted

Expand Down Expand Up @@ -209,7 +207,7 @@ def delete(self, route):
self._op("delete", route)

def _interfaces(self):
return bidict.bidict({i["index"]: dict(i["attrs"]).get("IFLA_IFNAME") for i in self._ip_links()})
return {i["index"]: dict(i["attrs"]).get("IFLA_IFNAME") for i in self._ip_links()}

def _ip_links(self):
retries = 5
Expand All @@ -229,19 +227,20 @@ def _op(self, op, route):
else:
raise RuntimeError()

kwargs = dict(dst=f"{route.network}/{prefixlen}", gateway=str(route.gateway) if route.gateway else None)
for key, value in map(
lambda v: [v[0], v[1]() if isinstance(v[1], collections.abc.Callable) else v[1]],
filter(
lambda v: v[2] if len(v) == 3 else v[1], (
("oif", lambda: self._interfaces().inv[route.interface], route.interface is not None),
("table", route.table_id),
("scope", route.scope),
("prefsrc", route.preferred_source),
)
)
):
kwargs[key] = value
kwargs = {
"dst": f"{route.network}/{prefixlen}",
"gateway": str(route.gateway) if route.gateway else None,
"oif": None,
"table": route.table_id,
"scope": route.scope,
"prefsrc": route.preferred_source,
}
if route.interface is not None:
for i in self._ip_links():
ifname = dict(i["attrs"]).get("IFLA_IFNAME")
if ifname is not None and ifname == route.interface:
kwargs["oif"] = i["index"]
break

ip.route(op, **kwargs)

Expand Down
Loading