diff --git a/src/DhcpBundle/Controller/DhcpController.php b/src/DhcpBundle/Controller/DhcpController.php index 6a3604f..b11adad 100644 --- a/src/DhcpBundle/Controller/DhcpController.php +++ b/src/DhcpBundle/Controller/DhcpController.php @@ -624,74 +624,82 @@ public function getSubnets(): JsonResponse } } - /** - * @OA\Get( - * path="/ogdhcp/v1/subnets/{subnetId}/hosts", - * summary="Get all hosts in a subnet", - * @OA\Parameter( - * name="subnetId", - * in="path", - * description="The ID of the subnet", - * required=true, - * @OA\Schema(type="integer") - * ), - * @OA\Response( - * response=200, - * description="List of hosts in the subnet", - * ), - * @OA\Response( - * response=400, - * description="Error occurred", - * @OA\JsonContent( - * type="object", - * @OA\Property(property="error", type="string") - * ) - * ), - * @OA\Response( - * response=500, - * description="Server error", - * @OA\JsonContent( - * type="object", - * @OA\Property(property="error", type="string") - * ) - * ) - * ) - * @Route("/ogdhcp/v1/subnets/{subnetId}/hosts", methods={"GET"}) - */ - public function getHosts($subnetId): JsonResponse - { - try { - $response = $this->curlKeaService->executeCurlCommand('config-get'); +/** + * @OA\Get( + * path="/ogdhcp/v1/subnets/{subnetId}/hosts", + * summary="Get all hosts in a subnet", + * @OA\Parameter( + * name="subnetId", + * in="path", + * description="The ID of the subnet", + * required=true, + * @OA\Schema(type="integer") + * ), + * @OA\Response( + * response=200, + * description="List of hosts in the subnet", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="success", type="string", example="Hosts obtenidos correctamente"), + * @OA\Property(property="message", type="array", + * @OA\Items( + * type="object", + * @OA\Property(property="ip-address", type="string", example="192.168.1.20"), + * @OA\Property(property="hw-address", type="string", example="00:0c:29:6b:5e:71"), + * @OA\Property(property="hostname", type="string", example="pc11") + * ) + * ) + * ) + * ), + * @OA\Response( + * response=400, + * description="Error occurred", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error: La subred con el id 'subnetId' no existe.") + * ) + * ), + * @OA\Response( + * response=500, + * description="Server error when loading Kea DHCP configuration", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="error", type="string", example="Error al obtener la configuración de Kea DHCP: unable to access Kea configuration file.") + * ) + * ) + * ) + * @Route("/ogdhcp/v1/subnets/{subnetId}/hosts", methods={"GET"}) + */ +public function getHosts($subnetId): JsonResponse +{ + try { + $response = $this->curlKeaService->executeCurlCommand('config-get'); - if (!$response) { - $responseError = 'Error: No se pudo acceder al archivo dhcpd.conf'; - return new JsonResponse(['error' => $responseError], 400); - } else { - $result_code = $response[0]["result"]; - if ($result_code == 0) { - if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) { - $responseError = 'El campo \'subnet4\' no está inicializado'; - return new JsonResponse(['error' => $responseError], 400); - } else { - $subnets = $response[0]['arguments']['Dhcp4']['subnet4']; - foreach ($subnets as $subnet) { - if ($subnet['id'] == $subnetId) { - return new JsonResponse($subnet['reservations'], 200); - } - } - $responseError = 'Error: La subred con el id \'' . $subnetId . '\' no existe.'; - return new JsonResponse(['error' => $responseError], 400); - } - } else { - $responseError = "Error kea configuration invalid: " . $response[0]["text"]; - return new JsonResponse(['error' => $responseError], 400); + if (!$response) { + throw new \Exception('Error al obtener la configuración de Kea DHCP: No se pudo acceder al archivo de configuración.'); + } + + $result_code = $response[0]["result"]; + if ($result_code == 0) { + if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) { + throw new \Exception('Error: El campo \'subnet4\' no está inicializado.'); + } + + $subnets = $response[0]['arguments']['Dhcp4']['subnet4']; + foreach ($subnets as $subnet) { + if ($subnet['id'] == $subnetId) { + return new JsonResponse($subnet['reservations'], 200); } } - } catch (Exception $e) { - $responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage(); - return new JsonResponse(['error' => $responseError], 400); + + return new JsonResponse(['error' => 'Error: La subred con el id \'' . $subnetId . '\' no existe.'], 400); + } else { + throw new \Exception('Error en la configuración de Kea: ' . $response[0]['text']); } + } catch (\Exception $e) { + return new JsonResponse(['error' => $e->getMessage()], 500); } +} /**