From 0e23464524784b34d9741c6cbe6416c2cba78e16 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 20 Apr 2026 17:59:51 +0200 Subject: [PATCH] refs #2972. Integration remove leases ogDhcp --- config/api_platform/Client.yaml | 7 +++ config/api_platform/Subnet.yaml | 19 +++++++ .../OgDhcp/Client/DeleteClientLeaseAction.php | 48 ++++++++++++++++++ .../OgDhcp/Subnet/DeleteClientLeaseAction.php | 50 +++++++++++++++++++ .../Subnet/DeleteSubnetLeasesAction.php | 43 ++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 src/Controller/OgDhcp/Client/DeleteClientLeaseAction.php create mode 100644 src/Controller/OgDhcp/Subnet/DeleteClientLeaseAction.php create mode 100644 src/Controller/OgDhcp/Subnet/DeleteSubnetLeasesAction.php diff --git a/config/api_platform/Client.yaml b/config/api_platform/Client.yaml index 229aa8c..dd820be 100644 --- a/config/api_platform/Client.yaml +++ b/config/api_platform/Client.yaml @@ -121,6 +121,13 @@ resources: uriTemplate: /clients/{uuid}/get-partition-script-data controller: App\Controller\OgAgent\GetPartitionScriptDataAction + delete_client_lease: + class: ApiPlatform\Metadata\Delete + method: DELETE + input: false + uriTemplate: /clients/{uuid}/delete-lease + controller: App\Controller\OgDhcp\Client\DeleteClientLeaseAction + properties: App\Entity\Client: diff --git a/config/api_platform/Subnet.yaml b/config/api_platform/Subnet.yaml index 28ede92..dd3e49d 100644 --- a/config/api_platform/Subnet.yaml +++ b/config/api_platform/Subnet.yaml @@ -97,6 +97,25 @@ resources: uriTemplate: /og-dhcp/server/{uuid}/delete-host/{clientUuid} controller: App\Controller\OgDhcp\Subnet\DeleteHostAction + delete_subnet_leases: + shortName: Subnet Server Leases + description: Delete all leases of a Subnet + class: ApiPlatform\Metadata\Delete + method: DELETE + input: false + uriTemplate: /og-dhcp/server/{uuid}/delete-leases + controller: App\Controller\OgDhcp\Subnet\DeleteSubnetLeasesAction + + delete_client_lease: + shortName: Subnet Server Leases + description: Delete lease of a specific client + class: ApiPlatform\Metadata\Delete + method: DELETE + input: false + read: false + uriTemplate: /og-dhcp/server/{uuid}/delete-lease/{clientUuid} + controller: App\Controller\OgDhcp\Subnet\DeleteClientLeaseAction + properties: App\Entity\Subnet: diff --git a/src/Controller/OgDhcp/Client/DeleteClientLeaseAction.php b/src/Controller/OgDhcp/Client/DeleteClientLeaseAction.php new file mode 100644 index 0000000..cea3567 --- /dev/null +++ b/src/Controller/OgDhcp/Client/DeleteClientLeaseAction.php @@ -0,0 +1,48 @@ +getIp()) { + throw new BadRequestHttpException('Client has no IP address assigned'); + } + + $subnet = $data->getSubnet(); + if (!$subnet) { + throw new BadRequestHttpException('Client has no subnet associated'); + } + + $dhcpServer = $subnet->getDhcpServer(); + if (!$dhcpServer) { + throw new BadRequestHttpException('Subnet has no DHCP server associated'); + } + + $port = $dhcpServer->getApiPort() ?? 8081; + $url = sprintf('http://%s:%d/ogdhcp/v1/leases/%s', $dhcpServer->getIp(), $port, $data->getIp()); + + $content = $this->createRequest('DELETE', $url); + + return new JsonResponse(data: $content, status: Response::HTTP_OK); + } +} diff --git a/src/Controller/OgDhcp/Subnet/DeleteClientLeaseAction.php b/src/Controller/OgDhcp/Subnet/DeleteClientLeaseAction.php new file mode 100644 index 0000000..01e9efc --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/DeleteClientLeaseAction.php @@ -0,0 +1,50 @@ +entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]); + + if (!$client) { + throw new BadRequestHttpException('Client not found'); + } + + if (!$client->getIp()) { + throw new BadRequestHttpException('Client has no IP address assigned'); + } + + $dhcpServer = $data->getDhcpServer(); + if (!$dhcpServer) { + throw new BadRequestHttpException('Subnet has no DHCP server associated'); + } + + $port = $dhcpServer->getApiPort() ?? 8081; + $url = sprintf('http://%s:%d/ogdhcp/v1/leases/%s', $dhcpServer->getIp(), $port, $client->getIp()); + + $content = $this->createRequest('DELETE', $url); + + return new JsonResponse(data: $content, status: Response::HTTP_OK); + } +} diff --git a/src/Controller/OgDhcp/Subnet/DeleteSubnetLeasesAction.php b/src/Controller/OgDhcp/Subnet/DeleteSubnetLeasesAction.php new file mode 100644 index 0000000..30c1cb5 --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/DeleteSubnetLeasesAction.php @@ -0,0 +1,43 @@ +getServerId()) { + throw new BadRequestHttpException('Subnet has no serverId assigned in DHCP server'); + } + + $dhcpServer = $data->getDhcpServer(); + if (!$dhcpServer) { + throw new BadRequestHttpException('Subnet has no DHCP server associated'); + } + + $port = $dhcpServer->getApiPort() ?? 8081; + $url = sprintf('http://%s:%d/ogdhcp/v1/subnets/%d/leases', $dhcpServer->getIp(), $port, $data->getServerId()); + + $content = $this->createRequest('DELETE', $url); + + return new JsonResponse(data: $content, status: Response::HTTP_OK); + } +}