refs #797 in delete and modify identifiques the host with subnet id and mac, changes swagger and functionality with this logic. Fix duplicated 400 output in swagger addsubnet doc
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
348c5247f4
commit
ad7cd037bf
|
|
@ -308,14 +308,6 @@ public function getSubnets(): JsonResponse
|
||||||
* type="object",
|
* type="object",
|
||||||
* @OA\Property(property="error", type="string", example="Error: La subred con el nombre 'subnetName' ya existe.")
|
* @OA\Property(property="error", type="string", example="Error: La subred con el nombre 'subnetName' ya existe.")
|
||||||
* )
|
* )
|
||||||
* ),
|
|
||||||
* @OA\Response(
|
|
||||||
* response=400,
|
|
||||||
* description="Error occurred while adding the subnet",
|
|
||||||
* @OA\JsonContent(
|
|
||||||
* type="object",
|
|
||||||
* @OA\Property(property="error", type="string", example="Error: La subred con el ID 'subnetId' ya existe.")
|
|
||||||
* )
|
|
||||||
* )
|
* )
|
||||||
* )
|
* )
|
||||||
* @Route("/ogdhcp/v1/subnets", methods={"POST"})
|
* @Route("/ogdhcp/v1/subnets", methods={"POST"})
|
||||||
|
|
@ -875,7 +867,7 @@ public function getSubnets(): JsonResponse
|
||||||
* required=true,
|
* required=true,
|
||||||
* @OA\JsonContent(
|
* @OA\JsonContent(
|
||||||
* type="object",
|
* type="object",
|
||||||
* @OA\Property(property="host", type="string", example="pc11")
|
* @OA\Property(property="macAddress", type="string", example="56:6f:c7:4f:00:4f")
|
||||||
* )
|
* )
|
||||||
* ),
|
* ),
|
||||||
* @OA\Response(
|
* @OA\Response(
|
||||||
|
|
@ -887,6 +879,14 @@ public function getSubnets(): JsonResponse
|
||||||
* )
|
* )
|
||||||
* ),
|
* ),
|
||||||
* @OA\Response(
|
* @OA\Response(
|
||||||
|
* response=404,
|
||||||
|
* description="Host or subnet not found",
|
||||||
|
* @OA\JsonContent(
|
||||||
|
* type="object",
|
||||||
|
* @OA\Property(property="error", type="string")
|
||||||
|
* )
|
||||||
|
* ),
|
||||||
|
* @OA\Response(
|
||||||
* response=400,
|
* response=400,
|
||||||
* description="Error occurred",
|
* description="Error occurred",
|
||||||
* @OA\JsonContent(
|
* @OA\JsonContent(
|
||||||
|
|
@ -901,15 +901,15 @@ public function getSubnets(): JsonResponse
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$input = json_decode($request->getContent());
|
$input = json_decode($request->getContent());
|
||||||
$host = htmlspecialchars($input->host);
|
$macAddress = htmlspecialchars($input->macAddress);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$response["message"] = $e->getMessage();
|
return new JsonResponse(['error' => $e->getMessage()], 400);
|
||||||
return new JsonResponse(['error' => $response], 400);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||||
$subnetFound = false;
|
$subnetFound = false;
|
||||||
|
$hostFound = false;
|
||||||
|
|
||||||
foreach ($response[0]['arguments']['Dhcp4']['subnet4'] as &$subnet) {
|
foreach ($response[0]['arguments']['Dhcp4']['subnet4'] as &$subnet) {
|
||||||
if ($subnet['id'] == $subnetId) {
|
if ($subnet['id'] == $subnetId) {
|
||||||
|
|
@ -919,16 +919,29 @@ public function getSubnets(): JsonResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($subnet['reservations'] as $key => $reservation) {
|
foreach ($subnet['reservations'] as $key => $reservation) {
|
||||||
if (isset($reservation['hostname']) && $reservation['hostname'] === $host) {
|
if ($reservation['hw-address'] === $macAddress) {
|
||||||
unset($subnet['reservations'][$key]);
|
unset($subnet['reservations'][$key]);
|
||||||
$subnet['reservations'] = array_values($subnet['reservations']);
|
$subnet['reservations'] = array_values($subnet['reservations']);
|
||||||
|
$hostFound = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($subnetFound) {
|
if (!$subnetFound) {
|
||||||
|
return new JsonResponse(['error' => "Error: La subred con el id '$subnetId' no existe."], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$hostFound) {
|
||||||
|
return new JsonResponse(['error' => "Error: El host con la MAC '$macAddress' no existe en las reservaciones."], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Eliminar el campo 'hash' si existe
|
||||||
|
if (isset($response[0]['arguments']['hash'])) {
|
||||||
|
unset($response[0]['arguments']['hash']);
|
||||||
|
}
|
||||||
|
|
||||||
$array_encoded = json_encode($response[0]['arguments']);
|
$array_encoded = json_encode($response[0]['arguments']);
|
||||||
$configurationParsed = str_replace('\\', '', $array_encoded);
|
$configurationParsed = str_replace('\\', '', $array_encoded);
|
||||||
$configuration = json_decode($configurationParsed);
|
$configuration = json_decode($configurationParsed);
|
||||||
|
|
@ -937,29 +950,20 @@ public function getSubnets(): 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) {
|
||||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"];
|
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseSet[0]["text"]], 400);
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
|
||||||
} 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 guardar la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
return new JsonResponse(['error' => "Error al guardar la configuración en Kea DHCP: " . $responseWrite[0]["text"]], 400);
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
|
||||||
} else {
|
} else {
|
||||||
$responseSuccess = "Configuración cargada correctamente";
|
return new JsonResponse(['success' => "Host eliminado correctamente"], 200);
|
||||||
return new JsonResponse(['success' => $responseSuccess], 200);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseTest[0]["text"];
|
return new JsonResponse(['error' => "Error en la configuración de Kea: " . $responseTest[0]["text"]], 400);
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$responseError = "Error: El host con el hostname '$host' no existe en las reservaciones.";
|
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$responseError = "Error al obtener la configuración de Kea DHCP: " . $e->getMessage();
|
return new JsonResponse(['error' => $e->getMessage()], 400);
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1028,12 +1032,10 @@ public function getSubnets(): JsonResponse
|
||||||
*/
|
*/
|
||||||
public function updateDhcpHost(Request $request, $subnetId): JsonResponse
|
public function updateDhcpHost(Request $request, $subnetId): JsonResponse
|
||||||
{
|
{
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$input = json_decode($request->getContent());
|
$input = json_decode($request->getContent());
|
||||||
$host = htmlspecialchars($input->host);
|
$host = htmlspecialchars($input->host);
|
||||||
$oldMacAddress = htmlspecialchars($input->oldMacAddress);
|
$oldMacAddress = htmlspecialchars($input->oldMacAddress);
|
||||||
$oldAddress = htmlspecialchars($input->oldAddress);
|
|
||||||
$macAddress = htmlspecialchars($input->macAddress);
|
$macAddress = htmlspecialchars($input->macAddress);
|
||||||
$address = htmlspecialchars($input->address);
|
$address = htmlspecialchars($input->address);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
|
@ -1042,6 +1044,7 @@ public function getSubnets(): JsonResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Ejecutar el comando para obtener la configuración actual
|
||||||
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
$response = $this->curlKeaService->executeCurlCommand('config-get');
|
||||||
$subnetFound = false;
|
$subnetFound = false;
|
||||||
$hostFound = false;
|
$hostFound = false;
|
||||||
|
|
@ -1056,7 +1059,7 @@ public function getSubnets(): JsonResponse
|
||||||
|
|
||||||
foreach ($subnet['reservations'] as &$reservation) {
|
foreach ($subnet['reservations'] as &$reservation) {
|
||||||
$this->logger->info('LOOKING FOR HOST');
|
$this->logger->info('LOOKING FOR HOST');
|
||||||
if ($reservation['hw-address'] == $oldMacAddress && $reservation['ip-address'] == $oldAddress) {
|
if ($reservation['hw-address'] == $oldMacAddress) {
|
||||||
$this->logger->info('FOUND HOST');
|
$this->logger->info('FOUND HOST');
|
||||||
$hostFound = true;
|
$hostFound = true;
|
||||||
$reservation['hw-address'] = $macAddress;
|
$reservation['hw-address'] = $macAddress;
|
||||||
|
|
@ -1070,36 +1073,45 @@ public function getSubnets(): JsonResponse
|
||||||
|
|
||||||
if ($subnetFound && $hostFound) {
|
if ($subnetFound && $hostFound) {
|
||||||
$this->logger->info('UPDATING HOST');
|
$this->logger->info('UPDATING HOST');
|
||||||
|
|
||||||
|
// Eliminar el campo 'hash' si existe
|
||||||
|
if (isset($response[0]['arguments']['hash'])) {
|
||||||
|
unset($response[0]['arguments']['hash']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preparar la configuración modificada
|
||||||
$array_encoded = json_encode($response[0]['arguments']);
|
$array_encoded = json_encode($response[0]['arguments']);
|
||||||
$configurationParsed = str_replace('\\', '', $array_encoded);
|
$configurationParsed = str_replace('\\', '', $array_encoded);
|
||||||
$configuration = json_decode($configurationParsed);
|
$configuration = json_decode($configurationParsed);
|
||||||
|
|
||||||
|
// Test de la configuración
|
||||||
$responseTest = $this->curlKeaService->executeCurlCommand('config-test', $configuration);
|
$responseTest = $this->curlKeaService->executeCurlCommand('config-test', $configuration);
|
||||||
if ($responseTest[0]["result"] == 0) {
|
if ($responseTest[0]["result"] == 0) {
|
||||||
|
// Guardar la configuración
|
||||||
$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);
|
return new JsonResponse(['error' => $responseError], 400);
|
||||||
} else {
|
} else {
|
||||||
|
// Escribir la configuración en disco
|
||||||
$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 guardar la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseWrite[0]["text"];
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
return new JsonResponse(['error' => $responseError], 400);
|
||||||
} else {
|
} else {
|
||||||
$responseSuccess = "Configuración cargada correctamente";
|
return new JsonResponse(['success' => "Host actualizado correctamente"], 200);
|
||||||
return new JsonResponse(['success' => $responseSuccess], 200);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$responseError = "Error al guardar la configuración en Kea DHCP: " . $responseTest[0]["text"];
|
$responseError = "Error en la configuración de Kea: " . $responseTest[0]["text"];
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
return new JsonResponse(['error' => $responseError], 400);
|
||||||
}
|
}
|
||||||
} elseif (!$subnetFound) {
|
} elseif (!$subnetFound) {
|
||||||
$responseError = "Error: La subred con el id '$subnetId' no existe.";
|
$responseError = "Error: La subred con el id '$subnetId' no existe.";
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
return new JsonResponse(['error' => $responseError], 404);
|
||||||
} elseif (!$hostFound) {
|
} elseif (!$hostFound) {
|
||||||
$responseError = "Error: La IP " . $oldAddress . " y la MAC " . $oldMacAddress . " no existe en las reservaciones.";
|
$responseError = "Error: El host con la MAC " . $oldMacAddress . " no existe en las reservaciones.";
|
||||||
return new JsonResponse(['error' => $responseError], 400);
|
return new JsonResponse(['error' => $responseError], 404);
|
||||||
}
|
}
|
||||||
} 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();
|
||||||
|
|
@ -1107,6 +1119,7 @@ public function getSubnets(): JsonResponse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function restoreDhcpConfiguration(): JsonResponse
|
public function restoreDhcpConfiguration(): JsonResponse
|
||||||
{
|
{
|
||||||
$backup_dir = '/opt/opengnsys/etc/kea/backup';
|
$backup_dir = '/opt/opengnsys/etc/kea/backup';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue