refs #1568 adds new parameters subnetName and dns to kea dhcp
testing/og-dhcp-API/pipeline/head There was a failure building this commit Details

pull/3/head
Luis Gerardo Romero Garcia 2025-02-28 09:32:45 +01:00
parent 82eddcc8e9
commit 16eedd9546
1 changed files with 56 additions and 12 deletions

View File

@ -242,6 +242,7 @@ class DhcpController
* @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="subnet", type="string", example="192.168.1.0/24"),
* @OA\Property(property="next-server", type="string", example="192.168.1.1"),
* @OA\Property(property="DNS", type="string", example="192.168.1.1"),
* @OA\Property(property="boot-file-name", type="string", example="pxelinux.0"),
* @OA\Property(property="pools", type="array",
* @OA\Items(
@ -394,6 +395,8 @@ class DhcpController
* @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="subnetName", type="string", example="Aula informatica),
* @OA\Property(property="DNS", type="string", example="192.168.1.1"),
* @OA\Property(property="bootFileName", type="string", example="pxelinux.0"),
* @OA\Property(property="router", type="string", example="192.168.1.254")
* )
@ -467,6 +470,8 @@ public function addDhcpSubnet(Request $request): JsonResponse
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : null;
$DNS = isset($input->DNS) ? htmlspecialchars($input->DNS) : null;
$subnetName = isset($input->subnetName) ? htmlspecialchars($input->subnetName) : null;
// Calcular el gatewayIP si no se proporciona el router
$gatewayIP = $router ?: substr($address, 0, strrpos($address, '.')) . '.1';
@ -491,21 +496,38 @@ public function addDhcpSubnet(Request $request): JsonResponse
try {
$response = $this->curlKeaService->executeCurlCommand('config-get');
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
$subnetCIDR = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
$newSubnet = [
"id" => $subnetId,
"subnet" => $subnetName,
"subnet" => $subnetCIDR,
"reservations" => [],
"option-data" => [
[
"name" => "routers",
"code" => 3,
"data" => $gatewayIP
]
[
"name" => "routers",
"code" => 3,
"data" => $gatewayIP
]
]
];
// Agregar "user-context" solo si $subnetName tiene valor
if (!empty($subnetName)) {
$newSubnet["user-context"] = [
"subnetName" => $subnetName
];
}
if (!empty($DNS)) {
$newSubnet["option-data"][] = [
"name" => "domain-name-servers",
"code" => 23,
"space" => "dhcp4",
"csv-format" => true,
"data" => $DNS
];
}
if ($nextServer) {
$newSubnet["next-server"] = $nextServer;
}
@ -520,7 +542,7 @@ public function addDhcpSubnet(Request $request): JsonResponse
$subnets = $response[0]['arguments']['Dhcp4']['subnet4'];
$subnetNameExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['subnet'] === $subnetName), false);
$subnetNameExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['subnet'] === $subnetCIDR), false);
$subnetIdExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['id'] === $subnetId), false);
if ($subnetNameExists) {
@ -529,10 +551,10 @@ public function addDhcpSubnet(Request $request): JsonResponse
'operation' => $operation,
'component' => $component,
'http_code' => Response::HTTP_BAD_REQUEST,
'desc' => "La subred con la IP '$subnetName' ya existe."
'desc' => "La subred con la IP '$subnetCIDR' ya existe."
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return new JsonResponse(['error' => "La subred con la ip '$subnetName' ya existe."], Response::HTTP_BAD_REQUEST);
return new JsonResponse(['error' => "La subred con la ip '$subnetCIDR' ya existe."], Response::HTTP_BAD_REQUEST);
} elseif ($subnetIdExists) {
$this->logger->error(json_encode([
'severity' => 'ERROR',
@ -866,6 +888,8 @@ public function addDhcpSubnet(Request $request): JsonResponse
* type="object",
* @OA\Property(property="mask", type="string"),
* @OA\Property(property="address", type="string"),
* @OA\Property(property="DNS", type="string")
* @OA\Property(property="subnetName", 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")
@ -920,6 +944,8 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : null;
$DNS = isset($input->DNS) ? htmlspecialchars($input->DNS) : null;
$subnetName = isset($input->subnetName) ? htmlspecialchars($input->subnetName) : 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);
@ -948,7 +974,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
try {
$response = $this->curlKeaService->executeCurlCommand('config-get');
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
$subnetCIDR = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
$errorText = "No hay subredes definidas";
@ -982,7 +1008,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
// Modificar la subred existente
$newSubnet = [
"id" => $subnetId,
"subnet" => $subnetName,
"subnet" => $subnetCIDR,
"reservations" => $existingSubnet['reservations'], // Mantener las reservas existentes
"option-data" => [
[
@ -993,6 +1019,24 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
]
];
// Agregar "user-context" solo si $subnetName tiene valor
if (!empty($subnetName)) {
$newSubnet["user-context"] = [
"subnetName" => $subnetName
];
}
// Agregar DNS si está presente
if (!empty($DNS)) {
$newSubnet["option-data"][] = [
"name" => "domain-name-servers",
"code" => 23,
"space" => "dhcp4",
"csv-format" => true,
"data" => $DNS
];
}
// Añadir next-server si está presente, o eliminarlo de las reservas si no está
if ($nextServer) {
$newSubnet["next-server"] = $nextServer;