diff --git a/src/Controller/OgDhcp/AbstractOgDhcpController.php b/src/Controller/OgDhcp/AbstractOgDhcpController.php index 9288698..bedb32d 100644 --- a/src/Controller/OgDhcp/AbstractOgDhcpController.php +++ b/src/Controller/OgDhcp/AbstractOgDhcpController.php @@ -26,6 +26,7 @@ abstract class AbstractOgDhcpController extends AbstractController protected readonly string $ogDhcpApiUrl, protected readonly EntityManagerInterface $entityManager, protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService, + protected readonly HttpClientInterface $httpClient, ) { } @@ -36,7 +37,7 @@ abstract class AbstractOgDhcpController extends AbstractController * @throws RedirectionExceptionInterface * @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, [ 'headers' => [ @@ -46,7 +47,7 @@ abstract class AbstractOgDhcpController extends AbstractController ]); try { - $response = $httpClient->request($method, $url, $params); + $response = $this->httpClient->request($method, $url, $params); return json_decode($response->getContent(), true); } catch (ClientExceptionInterface | ServerExceptionInterface $e) { diff --git a/src/Controller/OgDhcp/Subnet/DeleteAction.php b/src/Controller/OgDhcp/Subnet/DeleteAction.php index 7b9554c..6a184f3 100644 --- a/src/Controller/OgDhcp/Subnet/DeleteAction.php +++ b/src/Controller/OgDhcp/Subnet/DeleteAction.php @@ -29,7 +29,7 @@ class DeleteAction extends AbstractOgDhcpController 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->flush(); diff --git a/src/Controller/OgDhcp/Subnet/DeleteHostAction.php b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php index 8b2c251..1dc1a1e 100644 --- a/src/Controller/OgDhcp/Subnet/DeleteHostAction.php +++ b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php @@ -24,7 +24,7 @@ class DeleteHostAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @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]); @@ -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); $this->entityManager->persist($data); diff --git a/src/Controller/OgDhcp/Subnet/GetAction.php b/src/Controller/OgDhcp/Subnet/GetAction.php index 257e26b..fcb1cf7 100644 --- a/src/Controller/OgDhcp/Subnet/GetAction.php +++ b/src/Controller/OgDhcp/Subnet/GetAction.php @@ -23,13 +23,13 @@ class GetAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { 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); } diff --git a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php index 95381d0..51fb4d9 100644 --- a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php +++ b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php @@ -21,9 +21,9 @@ class GetCollectionAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @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); } diff --git a/src/Controller/OgDhcp/Subnet/GetHostsAction.php b/src/Controller/OgDhcp/Subnet/GetHostsAction.php index 8056945..2d553bb 100644 --- a/src/Controller/OgDhcp/Subnet/GetHostsAction.php +++ b/src/Controller/OgDhcp/Subnet/GetHostsAction.php @@ -23,13 +23,13 @@ class GetHostsAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { 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); } diff --git a/src/Controller/OgDhcp/Subnet/PostAction.php b/src/Controller/OgDhcp/Subnet/PostAction.php index 8cc41d1..7d3f6be 100644 --- a/src/Controller/OgDhcp/Subnet/PostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostAction.php @@ -23,11 +23,11 @@ class PostAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(Subnet $data): JsonResponse { $params = [ 'json' => [ - 'subnetId' => $data->getId(), + 'subnetId' => rand(1, 1000), 'mask' => $data->getNetmask(), 'address' => $data->getIpAddress(), '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->setSynchronized(true); diff --git a/src/Controller/OgDhcp/Subnet/PostHostAction.php b/src/Controller/OgDhcp/Subnet/PostHostAction.php index c6e3872..e2f4027 100644 --- a/src/Controller/OgDhcp/Subnet/PostHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostHostAction.php @@ -27,7 +27,7 @@ class PostHostAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(SubnetAddHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse + public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse { $client = $input->client; @@ -44,7 +44,7 @@ class PostHostAction extends AbstractOgDhcpController '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); $this->entityManager->persist($subnet); diff --git a/src/Controller/OgDhcp/Subnet/PutAction.php b/src/Controller/OgDhcp/Subnet/PutAction.php index 080f0bc..c7e39b8 100644 --- a/src/Controller/OgDhcp/Subnet/PutAction.php +++ b/src/Controller/OgDhcp/Subnet/PutAction.php @@ -23,7 +23,7 @@ class PutAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { 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->flush(); diff --git a/src/Controller/OgDhcp/Subnet/PutHostAction.php b/src/Controller/OgDhcp/Subnet/PutHostAction.php index 5ba614a..cf89d25 100644 --- a/src/Controller/OgDhcp/Subnet/PutHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PutHostAction.php @@ -24,7 +24,7 @@ class PutHostAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(SubnetAddHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse + public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse { $clients = $input->clients; @@ -42,7 +42,7 @@ class PutHostAction extends AbstractOgDhcpController '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); diff --git a/src/Controller/OgDhcp/Subnet/SyncAction.php b/src/Controller/OgDhcp/Subnet/SyncAction.php index 0c301d7..8b2c945 100644 --- a/src/Controller/OgDhcp/Subnet/SyncAction.php +++ b/src/Controller/OgDhcp/Subnet/SyncAction.php @@ -24,9 +24,9 @@ class SyncAction extends AbstractOgDhcpController * @throws RedirectionExceptionInterface * @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 = []; @@ -63,11 +63,11 @@ class SyncAction extends AbstractOgDhcpController { $getParsedData = $this->getIpAddressAndNetmaskFromCIDRService->__invoke($subnet['subnet']); - $subnetEntity->setName($subnet['boot-file-name']); - $subnetEntity->setBootFileName($subnet['boot-file-name']); + $subnetEntity->setName($subnet['id'].' - '.$subnet['subnet']); + $subnetEntity->setBootFileName($subnet['boot-file-name'] ?? null); $subnetEntity->setIpAddress($getParsedData['ip']); $subnetEntity->setNetmask($getParsedData['mask']); - $subnetEntity->setNextServer($subnet['next-server']); + $subnetEntity->setNextServer($subnet['next-server'] ?? null); foreach ($subnet['reservations'] as $host) { $hostEntity = $this->entityManager->getRepository(Client::class)->findOneBy(['mac' => $host['hw-address']]); diff --git a/src/State/Processor/SubnetProcessor.php b/src/State/Processor/SubnetProcessor.php index 0fef5cf..d8e9fec 100644 --- a/src/State/Processor/SubnetProcessor.php +++ b/src/State/Processor/SubnetProcessor.php @@ -9,16 +9,21 @@ use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Put; use ApiPlatform\State\ProcessorInterface; use ApiPlatform\Validator\ValidatorInterface; +use App\Controller\OgDhcp\Subnet\PostAction; +use App\Controller\OgDhcp\Subnet\PutAction; use App\Dto\Input\SubnetInput; use App\Dto\Output\SubnetOutput; use App\Repository\SubnetRepository; use App\Service\OgBoot\PxeBootFile\PostService; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; readonly class SubnetProcessor implements ProcessorInterface { public function __construct( 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 TransportExceptionInterface */ private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): SubnetOutput { @@ -54,6 +60,13 @@ readonly class SubnetProcessor implements ProcessorInterface $subnet = $data->createOrUpdateEntity($entity); $this->validator->validate($subnet); + + if (!$subnet->getId() ) { + $this->postAction->__invoke($subnet); + } else { + $this->putAction->__invoke($subnet); + } + $this->subnetRepository->save($subnet); return new SubnetOutput($subnet);