From 7d59467accb1cdec5cd4c11b1e15ef90a07bc14d Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 2 Jun 2025 00:15:03 +0200 Subject: [PATCH] refs #2120. Remove pxe template when removed client --- config/api_platform/Partition.yaml | 1 + src/Command/CheckClientAvailability.php | 10 ++++--- .../OgDhcp/Subnet/PostHostAction.php | 4 +-- .../OgDhcp/Subnet/PutHostAction.php | 29 +++++++++---------- src/EventSubscriber/MercureSubscriber.php | 28 ++++++++++++------ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/config/api_platform/Partition.yaml b/config/api_platform/Partition.yaml index 7ec5daa..200c8e3 100644 --- a/config/api_platform/Partition.yaml +++ b/config/api_platform/Partition.yaml @@ -4,6 +4,7 @@ resources: input: App\Dto\Input\PartitionPostInput output: App\Dto\Output\PartitionOutput order: + diskNumber: 'ASC' partitionNumber: 'ASC' normalizationContext: groups: ['default', 'partition:read'] diff --git a/src/Command/CheckClientAvailability.php b/src/Command/CheckClientAvailability.php index 1244437..fb63a9f 100644 --- a/src/Command/CheckClientAvailability.php +++ b/src/Command/CheckClientAvailability.php @@ -34,16 +34,18 @@ class CheckClientAvailability extends Command { $io = new SymfonyStyle($input, $output); $threshold = (new \DateTime())->modify(' - '.self::THRESHOLD_MINUTES . ' minutes'); - $startQueryTime = microtime(true); + $validStatuses = [ClientStatus::OG_LIVE, ClientStatus::WINDOWS, ClientStatus::LINUX, ClientStatus::MACOS]; + $query = $this->entityManager->createQuery( 'UPDATE App\Entity\Client c - SET c.status = :status - WHERE c.status = :currentStatus AND c.updatedAt < :threshold' + SET c.status = :status + WHERE c.status IN (:currentStatuses) + AND c.updatedAt < :threshold' ); $query->setParameter('status', ClientStatus::DISCONNECTED); - $query->setParameter('currentStatus', ClientStatus::OG_LIVE); + $query->setParameter('currentStatuses', $validStatuses); $query->setParameter('threshold', $threshold); $updatedCount = $query->execute(); diff --git a/src/Controller/OgDhcp/Subnet/PostHostAction.php b/src/Controller/OgDhcp/Subnet/PostHostAction.php index ba9de31..4b518c2 100644 --- a/src/Controller/OgDhcp/Subnet/PostHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostHostAction.php @@ -53,17 +53,15 @@ class PostHostAction extends AbstractOgDhcpController $params ); - // Guardar resultado exitoso $success[] = [ 'client' => $clientEntity->getName(), 'response' => $content ]; - // Persistir solo si la llamada fue exitosa $subnet->addClient($clientEntity); $this->entityManager->persist($subnet); $this->entityManager->flush(); - } catch (\Throwable $e) { // Capturar cualquier error sin interrumpir + } catch (\Throwable $e) { $errors[] = [ 'client' => $clientEntity->getName(), 'error' => $e->getMessage() diff --git a/src/Controller/OgDhcp/Subnet/PutHostAction.php b/src/Controller/OgDhcp/Subnet/PutHostAction.php index cf89d25..d7b6a45 100644 --- a/src/Controller/OgDhcp/Subnet/PutHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PutHostAction.php @@ -24,26 +24,23 @@ class PutHostAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse + public function __invoke(string $mac, Client $client): JsonResponse { - $clients = $input->clients; + $subnet = $client->getSubnet(); - foreach ($clients as $client) { - /** @var Client $clientEntity */ - $clientEntity = $client->getEntity(); - $data = [ - 'host' => $clientEntity->getName(), - 'oldMacAddress' => '', - 'macAddress' => '', - 'address' => '', - ]; + /** @var Client $clientEntity */ + $data = [ + 'hostname' => $client->getName(), + 'oldMacAddress' => strtolower($mac), + 'macAddress' => strtolower($client->getMac()), + 'address' => $client->getIp(), + ]; - $params = [ - 'json' => $data - ]; + $params = [ + 'json' => $data + ]; - $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params); - } + $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getServerId().'/hosts', $params); return new JsonResponse(status: Response::HTTP_OK); } diff --git a/src/EventSubscriber/MercureSubscriber.php b/src/EventSubscriber/MercureSubscriber.php index aaf1b3d..fe44ef7 100644 --- a/src/EventSubscriber/MercureSubscriber.php +++ b/src/EventSubscriber/MercureSubscriber.php @@ -63,15 +63,25 @@ class MercureSubscriber implements EventSubscriberInterface 'status' => $client->getStatus(), ]; - $update = new Update( - 'clients', - json_encode($data) - ); - $this->hub->publish($update); + try { + $update = new Update( + 'clients', + json_encode($data) + ); - $this->logger->info('Evento Mercure disparado', [ - 'method' => $method, - 'path' => $request->getPathInfo() - ]); + $this->hub->publish($update); + + $this->logger->info('Evento Mercure disparado', [ + 'method' => $method, + 'path' => $request->getPathInfo() + ]); + } catch (\Exception $e) { + $this->logger->error('Error setting method for Mercure update', [ + 'method' => $method, + 'path' => $request->getPathInfo(), + 'error' => $e->getMessage(), + ]); + return; + } } }