Creacion de plantillas pxe en la propagacion de propiedades de una OU
parent
8205c3dbae
commit
9a819cbe51
|
|
@ -71,6 +71,15 @@ resources:
|
|||
uriTemplate: /boot-servers/{uuid}/pxe-templates/get-collection
|
||||
controller: App\Controller\OgBoot\PxeTemplate\GetCollectionAction
|
||||
|
||||
boot_server_status:
|
||||
shortName: BootServer
|
||||
description: Get status of OgBoot server
|
||||
class: ApiPlatform\Metadata\Get
|
||||
method: GET
|
||||
input: false
|
||||
uriTemplate: /boot-servers/server/{uuid}/status
|
||||
controller: App\Controller\OgBoot\StatusAction
|
||||
|
||||
properties:
|
||||
App\Entity\BootServer:
|
||||
id:
|
||||
|
|
|
|||
|
|
@ -69,21 +69,16 @@ abstract class AbstractOgBootController extends AbstractController
|
|||
|
||||
return json_decode($response->getContent(), true);
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $e->getMessage()));
|
||||
|
||||
return [
|
||||
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error' => 'Client/Server error',
|
||||
'details' => $e->getMessage(),
|
||||
];
|
||||
$response = $e->getResponse();
|
||||
$statusCode = $response->getStatusCode();
|
||||
$content = json_decode($response->getContent(false), true);
|
||||
$errorMessage = $content['error'] ?? $content['message'] ?? $e->getMessage();
|
||||
|
||||
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $errorMessage));
|
||||
throw new HttpException($statusCode, "External API Error ($statusCode): $errorMessage");
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
|
||||
|
||||
return [
|
||||
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error' => 'Transport error',
|
||||
'details' => $e->getMessage(),
|
||||
];
|
||||
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, "Transport Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,22 +101,17 @@ abstract class AbstractOgBootController extends AbstractController
|
|||
$response = $this->httpClient->request($method, 'http://'.$bootServer->getIp().':'.$bootServer->getApiPort().$url, $params);
|
||||
|
||||
return json_decode($response->getContent(), true);
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface | TransportExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $e->getMessage()));
|
||||
|
||||
return [
|
||||
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error' => 'Client/Server error',
|
||||
'details' => $e->getMessage(),
|
||||
];
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
|
||||
$response = $e->getResponse();
|
||||
$statusCode = $response->getStatusCode();
|
||||
$content = json_decode($response->getContent(false), true);
|
||||
$errorMessage = $content['error'] ?? $content['message'] ?? $e->getMessage();
|
||||
|
||||
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $errorMessage));
|
||||
throw new HttpException($statusCode, "External API Error ($statusCode): $errorMessage");
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
|
||||
|
||||
return [
|
||||
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error' => 'Transport error',
|
||||
'details' => $e->getMessage(),
|
||||
];
|
||||
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, "Transport Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\OgBoot;
|
||||
|
||||
use App\Entity\BootServer;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
#[AsController]
|
||||
class StatusAction extends AbstractOgBootController
|
||||
{
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
public function __invoke(BootServer $data): JsonResponse
|
||||
{
|
||||
$content = $this->createRequestFromBootServer($data,'GET', '/ogboot/v1/status');
|
||||
|
||||
if (isset($content['error'])) {
|
||||
throw new BadRequestHttpException('An error occurred while fetching the status: ' . ($content['details'] ?? $content['message'] ?? 'Unknown error'));
|
||||
}
|
||||
|
||||
return new JsonResponse( data: $content, status: Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Client;
|
||||
use App\Entity\OrganizationalUnit;
|
||||
use App\Entity\PxeTemplate;
|
||||
use App\Repository\ClientRepository;
|
||||
use App\Repository\OrganizationalUnitRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
|
@ -15,6 +18,9 @@ class PropagateNetworkSettingsAction extends AbstractController
|
|||
public function __construct(
|
||||
private readonly OrganizationalUnitRepository $organizationalUnitRepository,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly \App\Controller\OgBoot\PxeBootFile\PostAction $postAction,
|
||||
private readonly \App\Controller\OgDhcp\Subnet\PutHostAction $putHostAction,
|
||||
private readonly ClientRepository $clientRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -46,6 +52,27 @@ class PropagateNetworkSettingsAction extends AbstractController
|
|||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$clients = $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($organizationalUnit->getId(), []);
|
||||
|
||||
/** @var Client $client */
|
||||
foreach ($clients as $client) {
|
||||
$template = $client->getTemplate() ?
|
||||
$client->getTemplate() :
|
||||
$client->getOrganizationalUnit()?->getNetworkSettings()?->getPxeTemplate();
|
||||
|
||||
if ($template === null) {
|
||||
$template = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);
|
||||
}
|
||||
|
||||
if ($template !== null) {
|
||||
$this->postAction->__invoke($client, $template);
|
||||
}
|
||||
|
||||
if ($client->getSubnet()) {
|
||||
$this->putHostAction->__invoke($client->getMac(), $client);
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonResponse(
|
||||
data: 'Network settings propagated successfully',
|
||||
status: Response::HTTP_OK
|
||||
|
|
|
|||
|
|
@ -30,57 +30,6 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte
|
|||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
KernelEvents::VIEW => ['updateNetworkSettings', EventPriorities::POST_WRITE],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
public function updateNetworkSettings(ViewEvent $event): void
|
||||
{
|
||||
$organizationalUnitOutput = $event->getControllerResult();
|
||||
$method = $event->getRequest()->getMethod();
|
||||
|
||||
if (!$organizationalUnitOutput instanceof OrganizationalUnitOutput ||
|
||||
!in_array($method, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH])) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var OrganizationalUnit $organizationalUnitEntity */
|
||||
$organizationalUnitEntity = $organizationalUnitOutput->getEntity();
|
||||
|
||||
if ($organizationalUnitEntity->getType() === OrganizationalUnitTypes::CLASSROOM) {
|
||||
$this->syncOgBoot($organizationalUnitEntity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
private function syncOgBoot(OrganizationalUnit $organizationalUnitEntity): void
|
||||
{
|
||||
$clients = $this->entityManager->getRepository(Client::class)->findClientsByOrganizationalUnitAndDescendants($organizationalUnitEntity->getId(), []);
|
||||
|
||||
if (empty($clients)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Client $client */
|
||||
foreach ($clients as $client) {
|
||||
if ($client->getTemplate() === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->postAction->__invoke($client, $client->getTemplate());
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue