new-parameters-subnet #3

Merged
lgromero merged 2 commits from new-parameters-subnet into main 2025-03-03 08:35:02 +01:00
1 changed files with 61 additions and 13 deletions

View File

@ -140,7 +140,7 @@ class DhcpController
$output = shell_exec("df -h " . realpath($this->backup_dir) . " | tail -1 | awk '{print $2, $3, $4, $5}'"); $output = shell_exec("df -h " . realpath($this->backup_dir) . " | tail -1 | awk '{print $2, $3, $4, $5}'");
if (!$output) { if (!$output) {
$this->logger->error("Failed to execute disk usage command"); $this->logger->error("Failed to execute disk usage command");
return null; return [];
} }
list($total, $used, $available, $percentage) = explode(' ', $output); 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="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="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="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( * @OA\Property(
* property="reservations", * property="reservations",
* type="array", * type="array",
@ -242,6 +244,7 @@ class DhcpController
* @OA\Property(property="id", type="integer", example=1), * @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="subnet", type="string", example="192.168.1.0/24"), * @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="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="boot-file-name", type="string", example="pxelinux.0"),
* @OA\Property(property="pools", type="array", * @OA\Property(property="pools", type="array",
* @OA\Items( * @OA\Items(
@ -394,6 +397,8 @@ class DhcpController
* @OA\Property(property="mask", type="string", example="255.255.255.0"), * @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="address", type="string", example="192.168.1.0"),
* @OA\Property(property="nextServer", type="string", example="192.168.1.1"), * @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="bootFileName", type="string", example="pxelinux.0"),
* @OA\Property(property="router", type="string", example="192.168.1.254") * @OA\Property(property="router", type="string", example="192.168.1.254")
* ) * )
@ -467,6 +472,8 @@ public function addDhcpSubnet(Request $request): JsonResponse
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null; $nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null; $bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : 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 // 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';
@ -491,21 +498,38 @@ public function addDhcpSubnet(Request $request): JsonResponse
try { try {
$response = $this->curlKeaService->executeCurlCommand('config-get'); $response = $this->curlKeaService->executeCurlCommand('config-get');
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); $subnetCIDR = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
$newSubnet = [ $newSubnet = [
"id" => $subnetId, "id" => $subnetId,
"subnet" => $subnetName, "subnet" => $subnetCIDR,
"reservations" => [], "reservations" => [],
"option-data" => [ "option-data" => [
[ [
"name" => "routers", "name" => "routers",
"code" => 3, "code" => 3,
"data" => $gatewayIP "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" => 6,
"space" => "dhcp4",
"csv-format" => true,
"data" => $DNS
];
}
if ($nextServer) { if ($nextServer) {
$newSubnet["next-server"] = $nextServer; $newSubnet["next-server"] = $nextServer;
} }
@ -520,7 +544,7 @@ public function addDhcpSubnet(Request $request): JsonResponse
$subnets = $response[0]['arguments']['Dhcp4']['subnet4']; $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); $subnetIdExists = array_reduce($subnets, fn($exists, $subnetElement) => $exists || ($subnetElement['id'] === $subnetId), false);
if ($subnetNameExists) { if ($subnetNameExists) {
@ -529,10 +553,10 @@ public function addDhcpSubnet(Request $request): JsonResponse
'operation' => $operation, 'operation' => $operation,
'component' => $component, 'component' => $component,
'http_code' => Response::HTTP_BAD_REQUEST, '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)); ], 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) { } elseif ($subnetIdExists) {
$this->logger->error(json_encode([ $this->logger->error(json_encode([
'severity' => 'ERROR', 'severity' => 'ERROR',
@ -866,6 +890,8 @@ public function addDhcpSubnet(Request $request): JsonResponse
* type="object", * type="object",
* @OA\Property(property="mask", type="string"), * @OA\Property(property="mask", type="string"),
* @OA\Property(property="address", 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="router", type="string", example="192.168.1.254"),
* @OA\Property(property="nextServer", type="string"), * @OA\Property(property="nextServer", type="string"),
* @OA\Property(property="bootFileName", type="string") * @OA\Property(property="bootFileName", type="string")
@ -920,6 +946,8 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
$nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null; $nextServer = isset($input->nextServer) ? htmlspecialchars($input->nextServer) : null;
$bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null; $bootFileName = isset($input->bootFileName) ? htmlspecialchars($input->bootFileName) : null;
$router = isset($input->router) ? htmlspecialchars($input->router) : 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 // Calcular el gateway añadiendo .1 al final de la subred si no se proporciona el router
$gateway = $router ?: preg_replace('/\d+$/', '1', $address); $gateway = $router ?: preg_replace('/\d+$/', '1', $address);
@ -948,7 +976,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
try { try {
$response = $this->curlKeaService->executeCurlCommand('config-get'); $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'])) { if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
$errorText = "No hay subredes definidas"; $errorText = "No hay subredes definidas";
@ -982,7 +1010,7 @@ public function modifyDhcpSubnet(Request $request): JsonResponse
// Modificar la subred existente // Modificar la subred existente
$newSubnet = [ $newSubnet = [
"id" => $subnetId, "id" => $subnetId,
"subnet" => $subnetName, "subnet" => $subnetCIDR,
"reservations" => $existingSubnet['reservations'], // Mantener las reservas existentes "reservations" => $existingSubnet['reservations'], // Mantener las reservas existentes
"option-data" => [ "option-data" => [
[ [
@ -993,6 +1021,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á // Añadir next-server si está presente, o eliminarlo de las reservas si no está
if ($nextServer) { if ($nextServer) {
$newSubnet["next-server"] = $nextServer; $newSubnet["next-server"] = $nextServer;
@ -1964,6 +2010,8 @@ public function updateDhcpHost(Request $request, $subnetId): JsonResponse
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR); return new JsonResponse(['error' => $errorText], Response::HTTP_INTERNAL_SERVER_ERROR);
} }
return new JsonResponse(['error' => 'Unexpected error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR);
} }