From 348c5247f4a720dcb5e2f66f4f6ad9b0eca6b917 Mon Sep 17 00:00:00 2001 From: lgromero Date: Thu, 3 Oct 2024 10:52:58 +0200 Subject: [PATCH] refs #797 changes swagger doc in modify hosts, get subnets and delete subnets, unset hash in delete subnets, adds a new error in case the id already exists in post subnet --- src/DhcpBundle/Controller/DhcpController.php | 229 ++++++++++++++----- 1 file changed, 171 insertions(+), 58 deletions(-) diff --git a/src/DhcpBundle/Controller/DhcpController.php b/src/DhcpBundle/Controller/DhcpController.php index 20bcd5d..aa325e6 100644 --- a/src/DhcpBundle/Controller/DhcpController.php +++ b/src/DhcpBundle/Controller/DhcpController.php @@ -275,7 +275,7 @@ public function getSubnets(): JsonResponse * path="/ogdhcp/v1/subnets", * summary="Add a new DHCP subnet", * @OA\RequestBody( - * description="JSON payload", + * description="JSON payload to create a new subnet", * required=true, * @OA\JsonContent( * type="object", @@ -291,20 +291,36 @@ public function getSubnets(): JsonResponse * description="Subnet added successfully", * @OA\JsonContent( * type="object", - * @OA\Property(property="success", type="string") + * @OA\Property(property="success", type="string", example="Subnet added successfully"), + * @OA\Property(property="message", type="object", + * @OA\Property(property="subnetId", type="integer", example=2), + * @OA\Property(property="subnet", type="string", example="192.168.1.0/24"), + * @OA\Property(property="nextServer", type="string", example="192.168.1.1"), + * @OA\Property(property="bootFileName", type="string", example="pxelinux.0"), + * @OA\Property(property="reservations", type="array", @OA\Items(type="object")) + * ) * ) * ), * @OA\Response( * response=400, - * description="Error occurred", + * description="Error occurred while adding the subnet", * @OA\JsonContent( * type="object", - * @OA\Property(property="error", type="string") + * @OA\Property(property="error", type="string", example="Error: La subred con el nombre 'subnetName' ya existe.") + * ) + * ), + * @OA\Response( + * response=400, + * description="Error occurred while adding the subnet", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: La subred con el ID 'subnetId' ya existe.") * ) * ) * ) * @Route("/ogdhcp/v1/subnets", methods={"POST"}) */ + public function addDhcpSubnet(Request $request): JsonResponse { try { @@ -334,12 +350,72 @@ public function getSubnets(): JsonResponse $response[0]['arguments']['Dhcp4']['subnet4'] = []; } - $exists = array_reduce($response[0]['arguments']['Dhcp4']['subnet4'], function ($exists, $subnetElement) use ($subnetName, $subnetId) { - return $exists || ($subnetElement['subnet'] === $subnetName) || ($subnetElement['id'] === $subnetId);; - }); + $subnets = $response[0]['arguments']['Dhcp4']['subnet4']; - if ($exists) { - $responseError = "Error: La subred el subnet '$subnetName' ya existe en las subredes"; + // Verificar si el nombre de la subred o el ID ya existe + $subnetNameExists = array_reduce($subnets, function ($exists, $subnetElement) use ($subnetName) { + return $exists || ($subnetElement['subnet'] === $subnetName); + }, false); + + $subnetIdExists = array_reduce($subnets, function ($exists, $subnetElement) use ($subnetId) { + return $exists || ($subnetElement['id'] === $subnetId); + }, false); /** + * @OA\Post( + * path="/ogdhcp/v1/subnets", + * summary="Add a new DHCP subnet", + * @OA\RequestBody( + * description="JSON payload to create a new subnet", + * required=true, + * @OA\JsonContent( + * type="object", + * @OA\Property(property="subnetId", type="integer", example=2), + * @OA\Property(property="mask", type="string", example="255.255.255.0"), + * @OA\Property(property="address", type="string", example="192.168.1.0"), + * @OA\Property(property="nextServer", type="string", example="192.168.1.1"), + * @OA\Property(property="bootFileName", type="string", example="pxelinux.0") + * ) + * ), + * @OA\Response( + * response=200, + * description="Subnet added successfully", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="success", type="string", example="Subnet added successfully"), + * @OA\Property(property="message", type="object", + * @OA\Property(property="subnetId", type="integer", example=2), + * @OA\Property(property="subnet", type="string", example="192.168.1.0/24"), + * @OA\Property(property="nextServer", type="string", example="192.168.1.1"), + * @OA\Property(property="bootFileName", type="string", example="pxelinux.0"), + * @OA\Property(property="reservations", type="array", @OA\Items(type="object")) + * ) + * ) + * ), + * @OA\Response( + * response=400, + * description="Error occurred while adding the subnet", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: La subred con el nombre 'subnetName' ya existe.") + * ) + * ), + * @OA\Response( + * response=400, + * description="Error occurred while adding the subnet", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: La subred con el ID 'subnetId' ya existe.") + * ) + * ) + * ) + * @Route("/ogdhcp/v1/subnets", methods={"POST"}) + */ + + // Controlar las salidas para los diferentes casos + if ($subnetNameExists) { + $responseError = "Error: La subred con el nombre '$subnetName' ya existe."; + return new JsonResponse(['error' => $responseError], 400); + } elseif ($subnetIdExists) { + $responseError = "Error: La subred con el ID '$subnetId' ya existe."; return new JsonResponse(['error' => $responseError], 400); } else { $response[0]['arguments']['Dhcp4']['subnet4'][] = $newSubnet; @@ -379,7 +455,6 @@ public function getSubnets(): JsonResponse } /** - * @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"DELETE"}) * @OA\Delete( * path="/ogdhcp/v1/subnets/{subnetId}", * summary="Delete a DHCP subnet", @@ -388,16 +463,14 @@ public function getSubnets(): JsonResponse * in="path", * description="ID of the subnet to delete", * required=true, - * @OA\Schema( - * type="integer" - * ) + * @OA\Schema(type="integer") * ), * @OA\Response( * response=200, * description="Subnet deleted successfully", * @OA\JsonContent( * type="object", - * @OA\Property(property="success", type="string") + * @OA\Property(property="success", type="string", example="Subred eliminada correctamente") * ) * ), * @OA\Response( @@ -405,10 +478,27 @@ public function getSubnets(): JsonResponse * description="Error occurred", * @OA\JsonContent( * type="object", - * @OA\Property(property="error", type="string") + * @OA\Property(property="error", type="string", example="Error: No hay subredes definidas") + * ) + * ), + * @OA\Response( + * response=404, + * description="Subnet not found", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: La subred con el id '2' no existe") + * ) + * ), + * @OA\Response( + * response=500, + * description="Error saving configuration in Kea DHCP", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error al guardar la configuración en Kea DHCP: Unable to save configuration") * ) * ) * ) + * @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"DELETE"}) */ @@ -431,6 +521,10 @@ public function getSubnets(): JsonResponse return new JsonResponse(['error' => $responseError], 400); } else { unset($response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex]); + // Eliminar el campo 'hash' si existe + if (isset($response[0]['arguments']['hash'])) { + unset($response[0]['arguments']['hash']); + } $response[0]['arguments']['Dhcp4']['subnet4'] = array_values($response[0]['arguments']['Dhcp4']['subnet4']); $array_encoded = json_encode($response[0]['arguments']); $configurationParsed = str_replace('\\', '', $array_encoded); @@ -870,49 +964,68 @@ public function getSubnets(): JsonResponse } - /** - * @OA\Put( - * path="/ogdhcp/v1/subnets/{subnetId}/hosts", - * summary="Update a DHCP host", - * @OA\Parameter( - * name="subnetId", - * in="path", - * description="The ID of the subnet", - * required=true, - * @OA\Schema(type="integer") - * ), - * @OA\RequestBody( - * description="Data for the host to update", - * required=true, - * @OA\JsonContent( - * type="object", - * @OA\Property(property="host", type="string", example="pc11"), - * @OA\Property(property="oldMacAddress", type="string", example="56:6f:c7:4f:00:4f"), - * @OA\Property(property="oldAddress", type="string", example="192.168.1.11"), - * @OA\Property(property="macAddress", type="string", example="56:6f:c7:4f:01:01"), - * @OA\Property(property="address", type="string", example="192.168.1.11") - * ) - * ), - * @OA\Response( - * response=200, - * description="Host updated successfully", - * @OA\JsonContent( - * type="object", - * @OA\Property(property="success", type="string") - * ) - * ), - * @OA\Response( - * response=400, - * description="Error occurred", - * @OA\JsonContent( - * type="object", - * @OA\Property(property="error", type="string") - * ) - * ) - * ) - * - * @Route("/ogdhcp/v1/subnets/{subnetId}/hosts", methods={"PUT"}) - */ +/** + * @OA\Put( + * path="/ogdhcp/v1/subnets/{subnetId}/hosts", + * summary="Update a DHCP host", + * @OA\Parameter( + * name="subnetId", + * in="path", + * description="The ID of the subnet", + * required=true, + * @OA\Schema(type="integer") + * ), + * @OA\RequestBody( + * description="Data for the host to update", + * required=true, + * @OA\JsonContent( + * type="object", + * @OA\Property(property="oldMacAddress", type="string", example="56:6f:c7:4f:00:4f", description="Old MAC address of the host to update"), + * @OA\Property(property="macAddress", type="string", example="56:6f:c7:4f:01:01", description="New MAC address"), + * @OA\Property(property="address", type="string", example="192.168.1.11", description="New IP address"), + * @OA\Property(property="hostname", type="string", example="PC11", description="New or existing hostname") + * ) + * ), + * @OA\Response( + * response=200, + * description="Host updated successfully", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="success", type="string", example="Host updated successfully"), + * @OA\Property(property="message", type="object", + * @OA\Property(property="hostname", type="string", example="PC11"), + * @OA\Property(property="macAddress", type="string", example="56:6f:c7:4f:01:01"), + * @OA\Property(property="ipAddress", type="string", example="192.168.1.12") + * ) + * ) + * ), + * @OA\Response( + * response=400, + * description="Error occurred", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Invalid input data") + * ) + * ), + * @OA\Response( + * response=404, + * description="Host not found", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: The host with MAC '56:6f:c7:4f:00:4f' was not found") + * ) + * ), + * @OA\Response( + * response=500, + * description="Server error", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Internal server error") + * ) + * ) + * ) + * @Route("/ogdhcp/v1/subnets/{subnetId}/hosts", methods={"PUT"}) + */ public function updateDhcpHost(Request $request, $subnetId): JsonResponse {