Big updates dhcp subnets
testing/ogcore-api/pipeline/head This commit looks good Details
testing/ogcore-api/pipeline/tag This commit looks good Details

pull/16/head^2 opengnsysy_devel-0.0.11
Manuel Aranda Rosales 2024-11-22 12:27:12 +01:00
parent 1c13c8fe7d
commit 4638c07416
12 changed files with 40 additions and 26 deletions

View File

@ -26,6 +26,7 @@ abstract class AbstractOgDhcpController extends AbstractController
protected readonly string $ogDhcpApiUrl, protected readonly string $ogDhcpApiUrl,
protected readonly EntityManagerInterface $entityManager, protected readonly EntityManagerInterface $entityManager,
protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService, protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService,
protected readonly HttpClientInterface $httpClient,
) )
{ {
} }
@ -36,7 +37,7 @@ abstract class AbstractOgDhcpController extends AbstractController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function createRequest (HttpClientInterface $httpClient, string $method, string $url, array $params = []): JsonResponse|array public function createRequest (string $method, string $url, array $params = []): JsonResponse|array
{ {
$params = array_merge($params, [ $params = array_merge($params, [
'headers' => [ 'headers' => [
@ -46,7 +47,7 @@ abstract class AbstractOgDhcpController extends AbstractController
]); ]);
try { try {
$response = $httpClient->request($method, $url, $params); $response = $this->httpClient->request($method, $url, $params);
return json_decode($response->getContent(), true); return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) { } catch (ClientExceptionInterface | ServerExceptionInterface $e) {

View File

@ -29,7 +29,7 @@ class DeleteAction extends AbstractOgDhcpController
throw new ValidatorException('Data Id is required'); throw new ValidatorException('Data Id is required');
} }
$content = $this->createRequest($httpClient, 'DELETE', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId()); $content = $this->createRequest('DELETE', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId());
$this->entityManager->remove($data); $this->entityManager->remove($data);
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -24,7 +24,7 @@ class DeleteHostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, string $clientUuid, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data, string $clientUuid): JsonResponse
{ {
$client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]); $client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]);
@ -42,7 +42,7 @@ class DeleteHostAction extends AbstractOgDhcpController
] ]
]; ];
$content = $this->createRequest($httpClient, 'DELETE', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId().'/hosts', $params); $content = $this->createRequest('DELETE', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId().'/hosts', $params);
$data->removeClient($client); $data->removeClient($client);
$this->entityManager->persist($data); $this->entityManager->persist($data);

View File

@ -23,13 +23,13 @@ class GetAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data): JsonResponse
{ {
if (!$data->getId()) { if (!$data->getId()) {
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Checksum is required');
} }
$content = $this->createRequest($httpClient, 'GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId()); $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId());
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -21,9 +21,9 @@ class GetCollectionAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(HttpClientInterface $httpClient): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest($httpClient, 'GET', 'http://'.$this->ogDhcpApiUrl . '/ogdhcp/v1/subnets'); $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl . '/ogdhcp/v1/subnets');
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -23,13 +23,13 @@ class GetHostsAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data): JsonResponse
{ {
if (!$data->getId()) { if (!$data->getId()) {
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Checksum is required');
} }
$content = $this->createRequest($httpClient, 'GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId().'/hosts'); $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId().'/hosts');
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -23,11 +23,11 @@ class PostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data): JsonResponse
{ {
$params = [ $params = [
'json' => [ 'json' => [
'subnetId' => $data->getId(), 'subnetId' => rand(1, 1000),
'mask' => $data->getNetmask(), 'mask' => $data->getNetmask(),
'address' => $data->getIpAddress(), 'address' => $data->getIpAddress(),
'nextServer' => $data->getNextServer(), 'nextServer' => $data->getNextServer(),
@ -35,7 +35,7 @@ class PostAction extends AbstractOgDhcpController
] ]
]; ];
$content = $this->createRequest($httpClient, 'POST', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets' , $params); $content = $this->createRequest('POST', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets' , $params);
$data->setServerId($content['message']['id']); $data->setServerId($content['message']['id']);
$data->setSynchronized(true); $data->setSynchronized(true);

View File

@ -27,7 +27,7 @@ class PostHostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(SubnetAddHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse
{ {
$client = $input->client; $client = $input->client;
@ -44,7 +44,7 @@ class PostHostAction extends AbstractOgDhcpController
'json' => $data 'json' => $data
]; ];
$content = $this->createRequest($httpClient, 'POST', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getServerId().'/hosts', $params); $content = $this->createRequest('POST', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getServerId().'/hosts', $params);
$subnet->addClient($clientEntity); $subnet->addClient($clientEntity);
$this->entityManager->persist($subnet); $this->entityManager->persist($subnet);

View File

@ -23,7 +23,7 @@ class PutAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data): JsonResponse
{ {
if (!$data->getId()) { if (!$data->getId()) {
throw new ValidatorException('Id is required'); throw new ValidatorException('Id is required');
@ -38,7 +38,7 @@ class PutAction extends AbstractOgDhcpController
] ]
]; ];
$content = $this->createRequest($httpClient, 'PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId(), $params); $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId(), $params);
$this->entityManager->persist($data); $this->entityManager->persist($data);
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -24,7 +24,7 @@ class PutHostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(SubnetAddHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse
{ {
$clients = $input->clients; $clients = $input->clients;
@ -42,7 +42,7 @@ class PutHostAction extends AbstractOgDhcpController
'json' => $data 'json' => $data
]; ];
$content = $this->createRequest($httpClient, 'PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params); $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
} }
return new JsonResponse(status: Response::HTTP_OK); return new JsonResponse(status: Response::HTTP_OK);

View File

@ -24,9 +24,9 @@ class SyncAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse public function __invoke(EntityManagerInterface $entityManager): JsonResponse
{ {
$content = $this->createRequest($httpClient, 'GET', 'http://'.$this->ogDhcpApiUrl . '/ogdhcp/v1/subnets'); $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl . '/ogdhcp/v1/subnets');
$arraySync = []; $arraySync = [];
@ -63,11 +63,11 @@ class SyncAction extends AbstractOgDhcpController
{ {
$getParsedData = $this->getIpAddressAndNetmaskFromCIDRService->__invoke($subnet['subnet']); $getParsedData = $this->getIpAddressAndNetmaskFromCIDRService->__invoke($subnet['subnet']);
$subnetEntity->setName($subnet['boot-file-name']); $subnetEntity->setName($subnet['id'].' - '.$subnet['subnet']);
$subnetEntity->setBootFileName($subnet['boot-file-name']); $subnetEntity->setBootFileName($subnet['boot-file-name'] ?? null);
$subnetEntity->setIpAddress($getParsedData['ip']); $subnetEntity->setIpAddress($getParsedData['ip']);
$subnetEntity->setNetmask($getParsedData['mask']); $subnetEntity->setNetmask($getParsedData['mask']);
$subnetEntity->setNextServer($subnet['next-server']); $subnetEntity->setNextServer($subnet['next-server'] ?? null);
foreach ($subnet['reservations'] as $host) { foreach ($subnet['reservations'] as $host) {
$hostEntity = $this->entityManager->getRepository(Client::class)->findOneBy(['mac' => $host['hw-address']]); $hostEntity = $this->entityManager->getRepository(Client::class)->findOneBy(['mac' => $host['hw-address']]);

View File

@ -9,16 +9,21 @@ use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface; use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface; use ApiPlatform\Validator\ValidatorInterface;
use App\Controller\OgDhcp\Subnet\PostAction;
use App\Controller\OgDhcp\Subnet\PutAction;
use App\Dto\Input\SubnetInput; use App\Dto\Input\SubnetInput;
use App\Dto\Output\SubnetOutput; use App\Dto\Output\SubnetOutput;
use App\Repository\SubnetRepository; use App\Repository\SubnetRepository;
use App\Service\OgBoot\PxeBootFile\PostService; use App\Service\OgBoot\PxeBootFile\PostService;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
readonly class SubnetProcessor implements ProcessorInterface readonly class SubnetProcessor implements ProcessorInterface
{ {
public function __construct( public function __construct(
private SubnetRepository $subnetRepository, private SubnetRepository $subnetRepository,
private ValidatorInterface $validator private ValidatorInterface $validator,
private PostAction $postAction,
private PutAction $putAction,
) )
{ {
} }
@ -40,6 +45,7 @@ readonly class SubnetProcessor implements ProcessorInterface
/** /**
* @throws \Exception * @throws \Exception
* @throws TransportExceptionInterface
*/ */
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): SubnetOutput private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): SubnetOutput
{ {
@ -54,6 +60,13 @@ readonly class SubnetProcessor implements ProcessorInterface
$subnet = $data->createOrUpdateEntity($entity); $subnet = $data->createOrUpdateEntity($entity);
$this->validator->validate($subnet); $this->validator->validate($subnet);
if (!$subnet->getId() ) {
$this->postAction->__invoke($subnet);
} else {
$this->putAction->__invoke($subnet);
}
$this->subnetRepository->save($subnet); $this->subnetRepository->save($subnet);
return new SubnetOutput($subnet); return new SubnetOutput($subnet);