refs #1205 adds gateway paremeter in subnet
testing/og-dhcp-API/pipeline/head There was a failure building this commit Details

ogdhcp-log opengnsys_devel-0.0.18
Luis Gerardo Romero Garcia 2024-12-05 11:53:05 +01:00
parent d58744c595
commit 27f8babdf7
1 changed files with 134 additions and 134 deletions

View File

@ -28,7 +28,6 @@ class DhcpController
$this->backup_dir = $params->get('backup_dir');
}
/**
* @Route("/ogdhcp/v1/status", name="getDhcpStatus", methods={"GET"})
* @OA\Get(
@ -300,7 +299,8 @@ public function getSubnets(): JsonResponse
* @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\Property(property="bootFileName", type="string", example="pxelinux.0"),
* @OA\Property(property="router", type="string", example="192.168.1.254")
* )
* ),
* @OA\Response(
@ -357,19 +357,21 @@ public function getSubnets(): JsonResponse
* )
* @Route("/ogdhcp/v1/subnets", methods={"POST"})
*/
public function addDhcpSubnet(Request $request): JsonResponse
{
public function addDhcpSubnet(Request $request): JsonResponse
{
try {
$input = json_decode($request->getContent());
$subnetId = (int) htmlspecialchars($input->subnetId);
$mask = htmlspecialchars($input->mask);
$address = htmlspecialchars($input->address);
// Verificar si next-server y boot-file-name están presentes
// Verificar si next-server, boot-file-name y router están presentes
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : null;
// Calcular el gatewayIP si no se proporciona el router
$gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1';
} catch (Exception $e) {
$response["message"] = $e->getMessage();
if (strpos($e->getMessage(), 'Undefined property') !== false) {
@ -383,9 +385,6 @@ public function getSubnets(): JsonResponse
$response = $this->curlKeaService->executeCurlCommand('config-get');
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
// Crear el campo option-data para la puerta de enlace (gateway)
$gatewayIP = substr($address, 0, strrpos($address, '.')) . '.1';
// Crear el array newSubnet con los valores obligatorios
$newSubnet = [
"id" => $subnetId,
@ -477,7 +476,7 @@ public function getSubnets(): JsonResponse
} catch (Exception $e) {
return new JsonResponse(['error' => "Error al obtener la configuración de Kea DHCP: " . $e->getMessage()], 500);
}
}
}
@ -617,7 +616,7 @@ public function getSubnets(): JsonResponse
}
}
/**
/**
* @Route("/ogdhcp/v1/subnets/{subnetId}", methods={"PUT"})
* @OA\Put(
* path="/ogdhcp/v1/subnets/{subnetId}",
@ -636,6 +635,7 @@ public function getSubnets(): JsonResponse
* type="object",
* @OA\Property(property="mask", type="string"),
* @OA\Property(property="address", type="string"),
* @OA\Property(property="router", type="string", example="192.168.1.254"),
* @OA\Property(property="nextServer", type="string"),
* @OA\Property(property="bootFileName", type="string")
* )
@ -650,12 +650,12 @@ public function getSubnets(): JsonResponse
* ),
* @OA\Response(
* response=400,
* description="Error occurred while deleting the subnet",
* description="Error occurred while modifying the subnet",
* @OA\JsonContent(
* oneOf={
* @OA\Schema(@OA\Property(property="error", type="string", example="Falta un parámetro requerido: $paramFaltante")),
* @OA\Schema(@OA\Property(property="error", type="string", example="No hay subredes definidas")),
* @OA\Schema(@OA\Property(property="error", type="string", example="Error configuracion de kea invalido")),
* @OA\Schema(@OA\Property(property="error", type="string", example="Error configuración de Kea inválido")),
* @OA\Schema(@OA\Property(property="error", type="string", example="Error al guardar la configuración en Kea DHCP")),
* @OA\Schema(@OA\Property(property="error", type="string", example="Error al escribir en la configuración en Kea DHCP"))
* }
@ -682,9 +682,13 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
$mask = htmlspecialchars($input->mask);
$address = htmlspecialchars($input->address);
// Verificar si next-server y boot-file-name están presentes
// Verificar si next-server, boot-file-name y router están presentes
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : null;
// Calcular el gateway añadiendo .1 al final de la subred si no se proporciona el router
$gateway = $router ?: preg_replace('/\d+$/', '1', $address);
} catch (Exception $e) {
if (strpos($e->getMessage(), 'Undefined property') !== false) {
preg_match('/Undefined property: stdClass::\$(\w+)/', $e->getMessage(), $matches);
@ -707,9 +711,6 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
if ($subnetIndex === false) {
return new JsonResponse(['error' => "La subred con el id '$subnetId' no existe"], 400);
} else {
// Calcular el gateway añadiendo .1 al final de la subred
$gateway = preg_replace('/\d+$/', '1', $address);
// Obtener la subred actual para mantener las reservas existentes
$existingSubnet = $response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex];
@ -731,7 +732,6 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
if ($nextServer) {
$newSubnet["next-server"] = $nextServer;
} else {
// Eliminar next-server de las reservas si no está presente
foreach ($newSubnet['reservations'] as &$reservation) {
unset($reservation['next-server']);
}
@ -741,7 +741,6 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
if ($bootFileName) {
$newSubnet["boot-file-name"] = $bootFileName;
} else {
// Eliminar boot-file-name de las reservas si no está presente
foreach ($newSubnet['reservations'] as &$reservation) {
unset($reservation['boot-file-name']);
}
@ -750,10 +749,10 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
// Actualizar las reservas con los nuevos valores de next-server y boot-file-name
foreach ($newSubnet['reservations'] as &$reservation) {
if ($nextServer) {
$reservation['next-server'] = $nextServer; // Actualizar el next-server del host
$reservation['next-server'] = $nextServer;
}
if ($bootFileName) {
$reservation['boot-file-name'] = $bootFileName; // Actualizar el boot-file-name del host
$reservation['boot-file-name'] = $bootFileName;
}
}
@ -790,7 +789,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
}
}
} else {
return new JsonResponse(['error' => "Error configuracion de kea invalido: " . $responseTest[0]["text"]], 400);
return new JsonResponse(['error' => "Error configuración de Kea inválida: " . $responseTest[0]["text"]], 400);
}
}
} catch (Exception $e) {
@ -800,6 +799,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
/**
* @OA\Get(
* path="/ogdhcp/v1/subnets/{subnetId}/hosts",