Skip to content

Commit

Permalink
NAS-133152 / 25.04 / remove bidict from routing.py (and simplify dram…
Browse files Browse the repository at this point in the history
…atically) (#15233)

* remove bidict from routing.py

* remove bidict dependency
  • Loading branch information
yocalebo authored Dec 18, 2024
1 parent 59fb14f commit e977671
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
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

0 comments on commit e977671

Please sign in to comment.