refs #1204 Adds symfony logs with format in ogdhcp controller
testing/og-dhcp-API/pipeline/head There was a failure building this commit
Details
testing/og-dhcp-API/pipeline/head There was a failure building this commit
Details
parent
27f8babdf7
commit
48a231ff7c
|
@ -22,6 +22,11 @@ when@dev:
|
|||
type: console
|
||||
process_psr_3_messages: false
|
||||
channels: ["!event", "!doctrine", "!console"]
|
||||
syslog:
|
||||
type: syslog
|
||||
ident: "ogdhcp"
|
||||
level: info
|
||||
channels: ["!event"]
|
||||
|
||||
when@test:
|
||||
monolog:
|
||||
|
|
|
@ -258,33 +258,56 @@ class DhcpController
|
|||
* @Route("/ogdhcp/v1/subnets", methods={"GET"})
|
||||
*/
|
||||
|
||||
public function getSubnets(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
||||
if (!$response) {
|
||||
return new JsonResponse(['error' => 'No se pudo acceder al archivo de configuración de Kea'], 500);
|
||||
}
|
||||
|
||||
$result_code = $response[0]["result"];
|
||||
if ($result_code == 0) {
|
||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
||||
return new JsonResponse(['error' => 'El campo "subnet4" no está inicializado'], 400);
|
||||
} else {
|
||||
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
||||
return new JsonResponse([
|
||||
'success' => 'Subredes obtenidas correctamente',
|
||||
'message' => $subnets
|
||||
], 200);
|
||||
}
|
||||
} else {
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $response[0]["text"]], 500);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
public function getSubnets(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->logger->info('Iniciando la obtención de subredes.');
|
||||
|
||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
||||
if (!$response) {
|
||||
$this->logger->error('No se pudo acceder al archivo de configuración de Kea.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR
|
||||
]);
|
||||
return new JsonResponse(['error' => 'No se pudo acceder al archivo de configuración de Kea'], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
$result_code = $response[0]["result"];
|
||||
if ($result_code == 0) {
|
||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
||||
$this->logger->error('El campo "subnet4" no está inicializado.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST
|
||||
]);
|
||||
return new JsonResponse(['error' => 'El campo "subnet4" no está inicializado'], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
||||
$this->logger->info('Subredes obtenidas correctamente.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'subnets_count' => count($subnets)
|
||||
]);
|
||||
return new JsonResponse([
|
||||
'success' => 'Subredes obtenidas correctamente',
|
||||
'message' => $subnets
|
||||
], Response::HTTP_OK);
|
||||
}
|
||||
} else {
|
||||
$errorText = $response[0]["text"] ?? 'Unknown error';
|
||||
$this->logger->error('Error al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $errorText], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error('Excepción al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
|
@ -373,12 +396,16 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
// Calcular el gatewayIP si no se proporciona el router
|
||||
$gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1';
|
||||
} catch (Exception $e) {
|
||||
$response["message"] = $e->getMessage();
|
||||
$this->logger->error('Error al procesar los datos de entrada para agregar una subred.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], 400);
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -425,9 +452,17 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
}, false);
|
||||
|
||||
if ($subnetNameExists) {
|
||||
return new JsonResponse(['error' => "La subred con la ip '$subnetName' ya existe."], 400);
|
||||
$this->logger->error('La subred con la IP ya existe.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'subnet_name' => $subnetName
|
||||
]);
|
||||
return new JsonResponse(['error' => "La subred con la ip '$subnetName' ya existe."], Response::HTTP_BAD_REQUEST);
|
||||
} elseif ($subnetIdExists) {
|
||||
return new JsonResponse(['error' => "La subred con el ID '$subnetId' ya existe."], 400);
|
||||
$this->logger->error('La subred con el ID ya existe.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['error' => "La subred con el ID '$subnetId' ya existe."], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$response[0]['arguments']['Dhcp4']['subnet4'][] = $newSubnet;
|
||||
|
||||
|
@ -444,11 +479,21 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
if ($responseTest[0]["result"] == 0) {
|
||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"]], 400);
|
||||
$errorText = $responseSet[0]["text"] ?? 'Unknown error';
|
||||
$this->logger->error('Error al guardar la configuración en Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseWrite[0]["text"]], 400);
|
||||
$errorText = $responseWrite[0]["text"] ?? 'Unknown error';
|
||||
$this->logger->error('Error al escribir la configuración en Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al escribir la configuración en Kea DHCP: " . $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
// Realizar una nueva consulta a Kea para obtener la subred recién creada
|
||||
$configGetResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
@ -463,21 +508,39 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
}
|
||||
|
||||
if ($createdSubnet === null) {
|
||||
return new JsonResponse(['error' => "No se pudo encontrar la subred creada"], 400);
|
||||
$this->logger->error('No se pudo encontrar la subred creada.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['error' => "No se pudo encontrar la subred creada"], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return new JsonResponse(['success' => "Subred agregada correctamente", 'message' => $createdSubnet], 200);
|
||||
$this->logger->info('Subred agregada correctamente.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'subnet' => $createdSubnet
|
||||
]);
|
||||
return new JsonResponse(['success' => "Subred agregada correctamente", 'message' => $createdSubnet], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return new JsonResponse(['error' => "Error en la configuración de Kea: " . $responseTest[0]["text"]], 400);
|
||||
$errorText = $responseTest[0]["text"] ?? 'Unknown error';
|
||||
$this->logger->error('Error en la configuración de Kea.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error en la configuración de Kea: " . $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], 500);
|
||||
$this->logger->error('Excepción al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -563,22 +626,31 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
public function deleteDhcpSubnet(Request $request): JsonResponse
|
||||
{
|
||||
$subnetId = (int) $request->get('subnetId');
|
||||
|
||||
|
||||
try {
|
||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
||||
|
||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
||||
$responseError = "No hay subredes definidas";
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "No hay subredes definidas";
|
||||
$this->logger->error('No se encontraron subredes definidas.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
$subnetIndex = array_search($subnetId, array_column($response[0]['arguments']['Dhcp4']['subnet4'], 'id'));
|
||||
|
||||
|
||||
if ($subnetIndex === false) {
|
||||
$responseError = "La subred con el id '$subnetId' no existe";
|
||||
return new JsonResponse(['error' => $responseError], 404);
|
||||
$responseError = "La subred con el id '$subnetId' no existe";
|
||||
$this->logger->error('La subred no existe.', [
|
||||
'http_code' => Response::HTTP_NOT_FOUND,
|
||||
'subnet_id' => $subnetId,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_NOT_FOUND);
|
||||
} else {
|
||||
//Borramos la subred con ese id
|
||||
// Borramos la subred con ese id
|
||||
unset($response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex]);
|
||||
// Eliminar el campo 'hash' si existe
|
||||
if (isset($response[0]['arguments']['hash'])) {
|
||||
|
@ -589,32 +661,53 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
|||
$configurationParsed = str_replace('\\', '', $array_encoded);
|
||||
$configuration = json_decode($configurationParsed);
|
||||
$responseTest = $this->curlKeaService->executeCurlCommand('config-test', $configuration);
|
||||
|
||||
|
||||
if ($responseTest[0]["result"] == 0) {
|
||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
||||
$this->logger->error('Error al guardar la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
||||
$responseError = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||
$this->logger->error('Error al escribir la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseSuccess = "Subred eliminada correctamente";
|
||||
return new JsonResponse(['success' => $responseSuccess], 200);
|
||||
$responseSuccess = "Subred eliminada correctamente";
|
||||
$this->logger->info('Subred eliminada con éxito.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['success' => $responseSuccess], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$responseError = "Error configuracion de kea invalido: " . $responseTest[0]["text"];
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "Error configuración de Kea inválido: " . $responseTest[0]["text"];
|
||||
$this->logger->error('Error en la configuración de Kea.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
return new JsonResponse(['error' => $responseError], 500);
|
||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
$this->logger->error('Excepción al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"PUT"})
|
||||
|
@ -693,9 +786,17 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
|||
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], 400);
|
||||
$this->logger->error('Falta un parámetro requerido.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => "Falta un parámetro requerido: $paramFaltante"
|
||||
]);
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
return new JsonResponse(['error' => $e->getMessage()], 400);
|
||||
$this->logger->error('Error al procesar la solicitud.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -703,13 +804,24 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
|||
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
|
||||
|
||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
||||
return new JsonResponse(['error' => "No hay subredes definidas"], 400);
|
||||
$errorText = "No hay subredes definidas";
|
||||
$this->logger->error('Error al obtener subredes.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$subnetIndex = array_search($subnetId, array_column($response[0]['arguments']['Dhcp4']['subnet4'], 'id'));
|
||||
|
||||
if ($subnetIndex === false) {
|
||||
return new JsonResponse(['error' => "La subred con el id '$subnetId' no existe"], 400);
|
||||
$errorText = "La subred con el id '$subnetId' no existe";
|
||||
$this->logger->error('Subred no encontrada.', [
|
||||
'http_code' => Response::HTTP_NOT_FOUND,
|
||||
'subnet_id' => $subnetId,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_NOT_FOUND);
|
||||
} else {
|
||||
// Obtener la subred actual para mantener las reservas existentes
|
||||
$existingSubnet = $response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex];
|
||||
|
@ -773,27 +885,45 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
|||
if ($responseTest[0]["result"] == 0) {
|
||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"]], 400);
|
||||
$errorText = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
||||
$this->logger->error('Error al guardar la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"]], 400);
|
||||
$errorText = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||
$this->logger->error('Error al escribir la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
// Volvemos a consultar la configuración para devolver la subred modificada
|
||||
$updatedResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
$updatedSubnet = array_filter($updatedResponse[0]['arguments']['Dhcp4']['subnet4'], function ($subnet) use ($subnetId) {
|
||||
return $subnet['id'] == $subnetId;
|
||||
});
|
||||
|
||||
return new JsonResponse(['success' => "Subred modificada correctamente", 'message' => reset($updatedSubnet)], 200);
|
||||
$responseSuccess = "Subred modificada correctamente";
|
||||
$this->logger->info('Subred modificada con éxito.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['success' => $responseSuccess], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return new JsonResponse(['error' => "Error configuración de Kea inválida: " . $responseTest[0]["text"]], 400);
|
||||
$errorText = "Error configuración de Kea inválida: " . $responseTest[0]["text"];
|
||||
$this->logger->error('Error en la configuración de Kea.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], 500);
|
||||
$this->logger->error('Excepción al modificar la subred.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -847,38 +977,66 @@ public function getHosts($subnetId): JsonResponse
|
|||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
||||
if (!$response) {
|
||||
$responseError = "Error: No hay subredes definidas";
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "Error: No hay subredes definidas";
|
||||
$this->logger->error('Error al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$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.');
|
||||
$errorText = "Error: El campo 'subnet4' no está inicializado.";
|
||||
$this->logger->error('Error al obtener las subredes.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
throw new \Exception($errorText);
|
||||
}
|
||||
|
||||
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
||||
foreach ($subnets as $subnet) {
|
||||
if ($subnet['id'] == $subnetId) {
|
||||
$this->logger->info('Hosts obtenidos correctamente.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse([
|
||||
'success' => 'Hosts obtenidos correctamente correctamente',
|
||||
'success' => 'Hosts obtenidos correctamente',
|
||||
'message' => $subnet['reservations']
|
||||
], 200);
|
||||
], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonResponse(['error' => 'Error: La subred con el id \'' . $subnetId . '\' no existe.'], 400);
|
||||
$errorText = "Error: La subred con el id '$subnetId' no existe.";
|
||||
$this->logger->error('Subred no encontrada.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'subnet_id' => $subnetId,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseError = "Error: No hay subredes definidas";
|
||||
return new JsonResponse(['error' => $responseError], 400);
|
||||
$responseError = "Error: No hay subredes definidas";
|
||||
$this->logger->error('Error al obtener las subredes.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $responseError
|
||||
]);
|
||||
return new JsonResponse(['error' => $responseError], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
return new JsonResponse(['error' => $responseError], 500);
|
||||
$errorText = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
$this->logger->error('Excepción al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/ogdhcp/v1/subnets/{subnetId}/hosts",
|
||||
|
@ -986,12 +1144,18 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
|||
$macAddress = htmlspecialchars($input->macAddress);
|
||||
$address = htmlspecialchars($input->address);
|
||||
} catch (Exception $e) {
|
||||
$response["message"] = $e->getMessage();
|
||||
$this->logger->error('Falta un parámetro requerido.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $e->getMessage()
|
||||
]);
|
||||
|
||||
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], 400);
|
||||
return new JsonResponse(['error' => "Falta un parámetro requerido: $paramFaltante"], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -1025,7 +1189,12 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
|||
});
|
||||
|
||||
if ($exists) {
|
||||
return new JsonResponse(['error' => "Error: El host con el hostname '$host' ya existe en las reservaciones."], 400);
|
||||
$this->logger->error('Host duplicado.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'host' => $host,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['error' => "Error: El host con el hostname '$host' ya existe en las reservaciones."], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$subnet['reservations'][] = $newHost;
|
||||
break;
|
||||
|
@ -1034,7 +1203,13 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
|||
}
|
||||
|
||||
if (!$subnetFound) {
|
||||
return new JsonResponse(['error' => "Error: No se encontró la subnet con id '$subnetId'."], 400);
|
||||
$errorText = "Error: No se encontró la subnet con id '$subnetId'.";
|
||||
$this->logger->error('Subred no encontrada.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'subnet_id' => $subnetId,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
// Eliminar el campo 'hash' si existe
|
||||
|
@ -1050,11 +1225,21 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
|||
if ($responseTest[0]["result"] == 0) {
|
||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"]], 400);
|
||||
$errorText = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
||||
$this->logger->error('Error al guardar la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
||||
return new JsonResponse(['error' => "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"]], 400);
|
||||
$errorText = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||
$this->logger->error('Error al escribir la configuración.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
} else {
|
||||
// Ahora volvemos a consultar el host recién creado
|
||||
$updatedResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
||||
|
@ -1071,18 +1256,39 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
|||
}
|
||||
}
|
||||
if ($createdHost) {
|
||||
return new JsonResponse(['success' => "Host agregado correctamente", 'message' => $createdHost], 200);
|
||||
$this->logger->info('Host agregado correctamente.', [
|
||||
'http_code' => Response::HTTP_OK,
|
||||
'host' => $host,
|
||||
'subnet_id' => $subnetId
|
||||
]);
|
||||
return new JsonResponse(['success' => "Host agregado correctamente", 'message' => $createdHost], Response::HTTP_OK);
|
||||
} else {
|
||||
return new JsonResponse(['error' => "No se pudo encontrar el host recién creado"], 400);
|
||||
$errorText = "No se pudo encontrar el host recién creado.";
|
||||
$this->logger->error('Host recién creado no encontrado.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'host' => $host,
|
||||
'subnet_id' => $subnetId,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return new JsonResponse(['error' => "Error kea configuration invalid: " . $responseTest[0]["text"]], 400);
|
||||
$errorText = "Error configuración de Kea inválida: " . $responseTest[0]["text"];
|
||||
$this->logger->error('Configuración inválida.', [
|
||||
'http_code' => Response::HTTP_BAD_REQUEST,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
return new JsonResponse(['error' => $responseError], 500);
|
||||
$errorText = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||
$this->logger->error('Excepción al obtener la configuración de Kea DHCP.', [
|
||||
'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error_text' => $errorText
|
||||
]);
|
||||
return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue