refs #812 adds 500 error to getHost
testing/og-dhcp-API/pipeline/head There was a failure building this commit Details

nginx_conf
Luis Gerardo Romero Garcia 2024-10-04 13:13:51 +02:00
parent f43f309f1e
commit ff1449ff33
1 changed files with 71 additions and 63 deletions

View File

@ -624,7 +624,7 @@ public function getSubnets(): JsonResponse
} }
} }
/** /**
* @OA\Get( * @OA\Get(
* path="/ogdhcp/v1/subnets/{subnetId}/hosts", * path="/ogdhcp/v1/subnets/{subnetId}/hosts",
* summary="Get all hosts in a subnet", * summary="Get all hosts in a subnet",
@ -638,60 +638,68 @@ public function getSubnets(): JsonResponse
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="List of hosts in the subnet", * 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( * @OA\Response(
* response=400, * response=400,
* description="Error occurred", * description="Error occurred",
* @OA\JsonContent( * @OA\JsonContent(
* type="object", * type="object",
* @OA\Property(property="error", type="string") * @OA\Property(property="error", type="string", example="Error: La subred con el id 'subnetId' no existe.")
* ) * )
* ), * ),
* @OA\Response( * @OA\Response(
* response=500, * response=500,
* description="Server error", * description="Server error when loading Kea DHCP configuration",
* @OA\JsonContent( * @OA\JsonContent(
* type="object", * type="object",
* @OA\Property(property="error", type="string") * @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"}) * @Route("/ogdhcp/v1/subnets/{subnetId}/hosts", methods={"GET"})
*/ */
public function getHosts($subnetId): JsonResponse public function getHosts($subnetId): JsonResponse
{ {
try { try {
$response = $this->curlKeaService->executeCurlCommand('config-get'); $response = $this->curlKeaService->executeCurlCommand('config-get');
if (!$response) { if (!$response) {
$responseError = 'Error: No se pudo acceder al archivo dhcpd.conf'; throw new \Exception('Error al obtener la configuración de Kea DHCP: No se pudo acceder al archivo de configuración.');
return new JsonResponse(['error' => $responseError], 400); }
} else {
$result_code = $response[0]["result"]; $result_code = $response[0]["result"];
if ($result_code == 0) { if ($result_code == 0) {
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) { if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
$responseError = 'El campo \'subnet4\' no está inicializado'; throw new \Exception('Error: El campo \'subnet4\' no está inicializado.');
return new JsonResponse(['error' => $responseError], 400); }
} else {
$subnets = $response[0]['arguments']['Dhcp4']['subnet4']; $subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
foreach ($subnets as $subnet) { foreach ($subnets as $subnet) {
if ($subnet['id'] == $subnetId) { if ($subnet['id'] == $subnetId) {
return new JsonResponse($subnet['reservations'], 200); return new JsonResponse($subnet['reservations'], 200);
} }
} }
$responseError = 'Error: La subred con el id \'' . $subnetId . '\' no existe.';
return new JsonResponse(['error' => $responseError], 400); return new JsonResponse(['error' => 'Error: La subred con el id \'' . $subnetId . '\' no existe.'], 400);
}
} else { } else {
$responseError = "Error kea configuration invalid: " . $response[0]["text"]; throw new \Exception('Error en la configuración de Kea: ' . $response[0]['text']);
return new JsonResponse(['error' => $responseError], 400);
}
}
} catch (Exception $e) {
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
return new JsonResponse(['error' => $responseError], 400);
} }
} catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], 500);
} }
}
/** /**