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
|
type: console
|
||||||
process_psr_3_messages: false
|
process_psr_3_messages: false
|
||||||
channels: ["!event", "!doctrine", "!console"]
|
channels: ["!event", "!doctrine", "!console"]
|
||||||
|
syslog:
|
||||||
|
type: syslog
|
||||||
|
ident: "ogdhcp"
|
||||||
|
level: info
|
||||||
|
channels: ["!event"]
|
||||||
|
|
||||||
when@test:
|
when@test:
|
||||||
monolog:
|
monolog:
|
||||||
|
|
|
@ -258,33 +258,56 @@ class DhcpController
|
||||||
* @Route("/ogdhcp/v1/subnets", methods={"GET"})
|
* @Route("/ogdhcp/v1/subnets", methods={"GET"})
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public function getSubnets(): JsonResponse
|
public function getSubnets(): JsonResponse
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$this->logger->info('Iniciando la obtención de subredes.');
|
||||||
|
|
||||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||||
|
|
||||||
if (!$response) {
|
if (!$response) {
|
||||||
return new JsonResponse(['error' => 'No se pudo acceder al archivo de configuración de Kea'], 500);
|
$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"];
|
$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'])) {
|
||||||
return new JsonResponse(['error' => 'El campo "subnet4" no está inicializado'], 400);
|
$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 {
|
} else {
|
||||||
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
||||||
|
$this->logger->info('Subredes obtenidas correctamente.', [
|
||||||
|
'http_code' => Response::HTTP_OK,
|
||||||
|
'subnets_count' => count($subnets)
|
||||||
|
]);
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'success' => 'Subredes obtenidas correctamente',
|
'success' => 'Subredes obtenidas correctamente',
|
||||||
'message' => $subnets
|
'message' => $subnets
|
||||||
], 200);
|
], Response::HTTP_OK);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $response[0]["text"]], 500);
|
$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) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @OA\Post(
|
* @OA\Post(
|
||||||
|
@ -373,12 +396,16 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
// Calcular el gatewayIP si no se proporciona el router
|
// Calcular el gatewayIP si no se proporciona el router
|
||||||
$gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1';
|
$gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1';
|
||||||
} catch (Exception $e) {
|
} 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) {
|
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
$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 {
|
try {
|
||||||
|
@ -425,9 +452,17 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
if ($subnetNameExists) {
|
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) {
|
} 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 {
|
} else {
|
||||||
$response[0]['arguments']['Dhcp4']['subnet4'][] = $newSubnet;
|
$response[0]['arguments']['Dhcp4']['subnet4'][] = $newSubnet;
|
||||||
|
|
||||||
|
@ -444,11 +479,21 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
if ($responseTest[0]["result"] == 0) {
|
if ($responseTest[0]["result"] == 0) {
|
||||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
// Realizar una nueva consulta a Kea para obtener la subred recién creada
|
// Realizar una nueva consulta a Kea para obtener la subred recién creada
|
||||||
$configGetResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
$configGetResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
||||||
|
@ -463,23 +508,41 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($createdSubnet === null) {
|
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 {
|
} 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) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @OA\Delete(
|
* @OA\Delete(
|
||||||
* path="/ogdhcp/v1/subnets/{subnetId}",
|
* path="/ogdhcp/v1/subnets/{subnetId}",
|
||||||
|
@ -569,16 +632,25 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
|
|
||||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
||||||
$responseError = "No hay subredes definidas";
|
$responseError = "No hay subredes definidas";
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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'));
|
$subnetIndex = array_search($subnetId, array_column($response[0]['arguments']['Dhcp4']['subnet4'], 'id'));
|
||||||
|
|
||||||
if ($subnetIndex === false) {
|
if ($subnetIndex === false) {
|
||||||
$responseError = "La subred con el id '$subnetId' no existe";
|
$responseError = "La subred con el id '$subnetId' no existe";
|
||||||
return new JsonResponse(['error' => $responseError], 404);
|
$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 {
|
} else {
|
||||||
//Borramos la subred con ese id
|
// Borramos la subred con ese id
|
||||||
unset($response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex]);
|
unset($response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex]);
|
||||||
// Eliminar el campo 'hash' si existe
|
// Eliminar el campo 'hash' si existe
|
||||||
if (isset($response[0]['arguments']['hash'])) {
|
if (isset($response[0]['arguments']['hash'])) {
|
||||||
|
@ -594,28 +666,49 @@ public function addDhcpSubnet(Request $request): JsonResponse
|
||||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
||||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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 {
|
} else {
|
||||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
||||||
$responseError = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
$responseError = "Error al escribir en la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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 {
|
} else {
|
||||||
$responseSuccess = "Subred eliminada correctamente";
|
$responseSuccess = "Subred eliminada correctamente";
|
||||||
return new JsonResponse(['success' => $responseSuccess], 200);
|
$this->logger->info('Subred eliminada con éxito.', [
|
||||||
|
'http_code' => Response::HTTP_OK,
|
||||||
|
'subnet_id' => $subnetId
|
||||||
|
]);
|
||||||
|
return new JsonResponse(['success' => $responseSuccess], Response::HTTP_OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$responseError = "Error configuracion de kea invalido: " . $responseTest[0]["text"];
|
$responseError = "Error configuración de Kea inválido: " . $responseTest[0]["text"];
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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) {
|
} catch (Exception $e) {
|
||||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||||
return new JsonResponse(['error' => $responseError], 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' => $responseError], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"PUT"})
|
* @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"PUT"})
|
||||||
* @OA\Put(
|
* @OA\Put(
|
||||||
|
@ -693,9 +786,17 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
||||||
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
$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 {
|
try {
|
||||||
|
@ -703,13 +804,24 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
||||||
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
|
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
|
||||||
|
|
||||||
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
|
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'));
|
$subnetIndex = array_search($subnetId, array_column($response[0]['arguments']['Dhcp4']['subnet4'], 'id'));
|
||||||
|
|
||||||
if ($subnetIndex === false) {
|
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 {
|
} else {
|
||||||
// Obtener la subred actual para mantener las reservas existentes
|
// Obtener la subred actual para mantener las reservas existentes
|
||||||
$existingSubnet = $response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex];
|
$existingSubnet = $response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex];
|
||||||
|
@ -773,27 +885,45 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
|
||||||
if ($responseTest[0]["result"] == 0) {
|
if ($responseTest[0]["result"] == 0) {
|
||||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
// Volvemos a consultar la configuración para devolver la subred modificada
|
$responseSuccess = "Subred modificada correctamente";
|
||||||
$updatedResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
$this->logger->info('Subred modificada con éxito.', [
|
||||||
$updatedSubnet = array_filter($updatedResponse[0]['arguments']['Dhcp4']['subnet4'], function ($subnet) use ($subnetId) {
|
'http_code' => Response::HTTP_OK,
|
||||||
return $subnet['id'] == $subnetId;
|
'subnet_id' => $subnetId
|
||||||
});
|
]);
|
||||||
|
return new JsonResponse(['success' => $responseSuccess], Response::HTTP_OK);
|
||||||
return new JsonResponse(['success' => "Subred modificada correctamente", 'message' => reset($updatedSubnet)], 200);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -848,37 +978,65 @@ public function getHosts($subnetId): JsonResponse
|
||||||
|
|
||||||
if (!$response) {
|
if (!$response) {
|
||||||
$responseError = "Error: No hay subredes definidas";
|
$responseError = "Error: No hay subredes definidas";
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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"];
|
$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'])) {
|
||||||
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'];
|
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
|
||||||
foreach ($subnets as $subnet) {
|
foreach ($subnets as $subnet) {
|
||||||
if ($subnet['id'] == $subnetId) {
|
if ($subnet['id'] == $subnetId) {
|
||||||
|
$this->logger->info('Hosts obtenidos correctamente.', [
|
||||||
|
'http_code' => Response::HTTP_OK,
|
||||||
|
'subnet_id' => $subnetId
|
||||||
|
]);
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'success' => 'Hosts obtenidos correctamente correctamente',
|
'success' => 'Hosts obtenidos correctamente',
|
||||||
'message' => $subnet['reservations']
|
'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 {
|
} else {
|
||||||
$responseError = "Error: No hay subredes definidas";
|
$responseError = "Error: No hay subredes definidas";
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
$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) {
|
} catch (\Exception $e) {
|
||||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
$errorText = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||||
return new JsonResponse(['error' => $responseError], 500);
|
$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(
|
* @OA\Post(
|
||||||
* path="/ogdhcp/v1/subnets/{subnetId}/hosts",
|
* path="/ogdhcp/v1/subnets/{subnetId}/hosts",
|
||||||
|
@ -986,12 +1144,18 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
$macAddress = htmlspecialchars($input->macAddress);
|
$macAddress = htmlspecialchars($input->macAddress);
|
||||||
$address = htmlspecialchars($input->address);
|
$address = htmlspecialchars($input->address);
|
||||||
} catch (Exception $e) {
|
} 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) {
|
if (strpos($e->getMessage(), 'Undefined property') !== false) {
|
||||||
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
|
||||||
$paramFaltante = $matches[1] ?? 'desconocido';
|
$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 {
|
try {
|
||||||
|
@ -1025,7 +1189,12 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($exists) {
|
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 {
|
} else {
|
||||||
$subnet['reservations'][] = $newHost;
|
$subnet['reservations'][] = $newHost;
|
||||||
break;
|
break;
|
||||||
|
@ -1034,7 +1203,13 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$subnetFound) {
|
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
|
// Eliminar el campo 'hash' si existe
|
||||||
|
@ -1050,11 +1225,21 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
if ($responseTest[0]["result"] == 0) {
|
if ($responseTest[0]["result"] == 0) {
|
||||||
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
$responseSet = $this->curlKeaService->executeCurlCommand('config-set', $configuration);
|
||||||
if ($responseSet == false || $responseSet[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
$responseWrite = $this->curlKeaService->executeCurlCommand('config-write', $configuration);
|
||||||
if ($responseWrite == false || $responseWrite[0]["result"] != 0) {
|
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 {
|
} else {
|
||||||
// Ahora volvemos a consultar el host recién creado
|
// Ahora volvemos a consultar el host recién creado
|
||||||
$updatedResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
$updatedResponse = $this->curlKeaService->executeCurlCommand('config-get');
|
||||||
|
@ -1071,18 +1256,39 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($createdHost) {
|
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 {
|
} 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 {
|
} 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) {
|
} catch (Exception $e) {
|
||||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
$errorText = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
||||||
return new JsonResponse(['error' => $responseError], 500);
|
$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