From 16eedd954665461bb34981f0af4200c0939b4723 Mon Sep 17 00:00:00 2001 From: lgromero Date: Fri, 28 Feb 2025 09:32:45 +0100 Subject: [PATCH 1/2] refs #1568 adds new parameters subnetName and dns to kea dhcp --- src/DhcpBundle/Controller/DhcpController.php | 68 ++++++++++++++++---- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/src/DhcpBundle/Controller/DhcpController.php b/src/DhcpBundle/Controller/DhcpController.php index 4156ea8..5201251 100644 --- a/src/DhcpBundle/Controller/DhcpController.php +++ b/src/DhcpBundle/Controller/DhcpController.php @@ -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; -- 2.40.1 From 96783a972f6264e725057d6b824aaf409abcf03d Mon Sep 17 00:00:00 2001 From: lgromero Date: Mon, 3 Mar 2025 08:30:42 +0100 Subject: [PATCH 2/2] refs #1543 fix swagger annotation --- src/DhcpBundle/Controller/DhcpController.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/DhcpBundle/Controller/DhcpController.php b/src/DhcpBundle/Controller/DhcpController.php index 5201251..a65e318 100644 --- a/src/DhcpBundle/Controller/DhcpController.php +++ b/src/DhcpBundle/Controller/DhcpController.php @@ -140,7 +140,7 @@ class DhcpController $output = shell_exec("df -h " . realpath($this->backup_dir) . " | tail -1 | awk '{print $2, $3, $4, $5}'"); if (!$output) { $this->logger->error("Failed to execute disk usage command"); - return null; + return []; } list($total, $used, $available, $percentage) = explode(' ', $output); @@ -217,6 +217,8 @@ class DhcpController * @OA\Property(property="subnet", type="string", description="The name of the subnet"), * @OA\Property(property="next-server", type="string", description="The next server in the subnet"), * @OA\Property(property="boot-file-name", type="string", description="The boot file name for the subnet"), + * @OA\Property(property="DNS", type="string", description="DNS subnet"), + * @OA\Property(property="subnetName", type="string", description="subnetName subnet"), * @OA\Property( * property="reservations", * type="array", @@ -395,7 +397,7 @@ 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="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") @@ -521,7 +523,7 @@ public function addDhcpSubnet(Request $request): JsonResponse if (!empty($DNS)) { $newSubnet["option-data"][] = [ "name" => "domain-name-servers", - "code" => 23, + "code" => 6, "space" => "dhcp4", "csv-format" => true, "data" => $DNS @@ -888,8 +890,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="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") @@ -2008,6 +2010,8 @@ public function updateDhcpHost(Request $request, $subnetId): JsonResponse ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR); } + + return new JsonResponse(['error' => 'Unexpected error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR); } -- 2.40.1