Skip to content

Commit

Permalink
Get DHCP subnets from shared-networks
Browse files Browse the repository at this point in the history
  • Loading branch information
devon-mar committed Aug 19, 2023
1 parent 28d1832 commit 3dd56d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
5 changes: 3 additions & 2 deletions netbox_kea/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ class SubnetTable(GenericTable):
if record.get("id")
else None,
)
shared_network = tables.Column(verbose_name="Shared Network")
actions = ActionsColumn(SUBNET_ACTIONS)

class Meta(GenericTable.Meta):
empty_text = "No subnets"
fields = ("id", "subnet", "actions")
default_columns = ("id", "subnet")
fields = ("id", "subnet", "shared_network", "actions")
default_columns = ("id", "subnet", "shared_network")

def __init__(self, leases_view: str, server_pk: int, *args, **kwargs):
self.leases_view = leases_view
Expand Down
35 changes: 26 additions & 9 deletions netbox_kea/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,14 @@ class BaseServerLeasesDeleteView(
queryset = Server.objects.all()
default_return_url = "plugins:netbox_kea:server_list"

def delete_lease(self, client: KeaClient, ip: str) -> None:
client.command(
f"lease{self.dhcp_version}-del",
arguments={"ip-address": ip},
service=[f"dhcp{self.dhcp_version}"],
check=(0, 3),
)
def delete_leases(self, client: KeaClient, ips: list[str]) -> None:
for ip in ips:
client.command(
f"lease{self.dhcp_version}-del",
arguments={"ip-address": ip},
service=[f"dhcp{self.dhcp_version}"],
check=(0, 3),
)

def get(self, request: HttpRequest, **kwargs):
return redirect(self.get_return_url(request, obj=self.get_object(**kwargs)))
Expand Down Expand Up @@ -482,12 +483,20 @@ def get_subnets(self, client: KeaClient) -> List[Dict[str, Any]]:
config = client.command("config-get", service=["dhcp6"])
assert config[0]["arguments"] is not None
subnets = config[0]["arguments"]["Dhcp6"]["subnet6"]
return [
subnet_list = [
{"id": s["id"], "subnet": s["subnet"]}
for s in subnets
if "id" in s and "subnet" in s
]

for sn in config[0]["arguments"]["Dhcp6"]["shared-networks"]:
for s in sn["subnet6"]:
subnet_list.append(
dict(id=s["id"], subnet=s["subnet"], shared_network=sn["name"])
)

return subnet_list


@register_model_view(Server, "subnets4")
class ServerDHCP4SubnetsView(BaseServerDHCPSubnetsView):
Expand All @@ -500,8 +509,16 @@ def get_subnets(self, client: KeaClient) -> List[Dict[str, Any]]:
config = client.command("config-get", service=["dhcp4"])
assert config[0]["arguments"] is not None
subnets = config[0]["arguments"]["Dhcp4"]["subnet4"]
return [
subnet_list = [
{"id": s["id"], "subnet": s["subnet"]}
for s in subnets
if "id" in s and "subnet" in s
]

for sn in config[0]["arguments"]["Dhcp4"]["shared-networks"]:
for s in sn["subnet4"]:
subnet_list.append(
dict(id=s["id"], subnet=s["subnet"], shared_network=sn["name"])
)

return subnet_list

0 comments on commit 3dd56d8

Please sign in to comment.