From 3dd56d876f042e0886338069b0cacf7801cfa3ab Mon Sep 17 00:00:00 2001 From: Devon Mar Date: Fri, 18 Aug 2023 22:34:05 -0700 Subject: [PATCH] Get DHCP subnets from shared-networks --- netbox_kea/tables.py | 5 +++-- netbox_kea/views.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/netbox_kea/tables.py b/netbox_kea/tables.py index 9b5082b..042d91d 100644 --- a/netbox_kea/tables.py +++ b/netbox_kea/tables.py @@ -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 diff --git a/netbox_kea/views.py b/netbox_kea/views.py index c888b87..461e475 100644 --- a/netbox_kea/views.py +++ b/netbox_kea/views.py @@ -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))) @@ -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): @@ -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