diff --git a/config/api_platform/PxeBootFile.yaml b/config/api_platform/PxeBootFile.yaml new file mode 100644 index 0000000..6c5886d --- /dev/null +++ b/config/api_platform/PxeBootFile.yaml @@ -0,0 +1,31 @@ +resources: + App\Entity\PxeBootFile: + processor: App\State\Processor\PxeBootFileProcessor + input: App\Dto\Input\PxeBootFileInput + output: App\Dto\Output\PxeBootFileOutput + normalizationContext: + groups: ['default', 'pxe-boot-file:read'] + denormalizationContext: + groups: ['pxe-boot-file:write'] + operations: + ApiPlatform\Metadata\GetCollection: + provider: App\State\Provider\PPxeBootFileProvider + filters: + - 'api_platform.filter.pxe_boot_file.order' + - 'api_platform.filter.pxe_boot_file.search' + + ApiPlatform\Metadata\Get: + provider: App\State\Provider\PxeTemplateProvider + ApiPlatform\Metadata\Put: + provider: App\State\Provider\PxeTemplateProvider + ApiPlatform\Metadata\Patch: + provider: App\State\Provider\PxeTemplateProvider + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\PxeBootFile: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 16b3247..e03a116 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -114,3 +114,7 @@ services: $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' $itemProvider: '@api_platform.doctrine.orm.state.item_provider' + App\State\Provider\PxeBootFileProvider: + bind: + $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' + $itemProvider: '@api_platform.doctrine.orm.state.item_provider' diff --git a/src/Dto/Input/PxeBootFileInput.php b/src/Dto/Input/PxeBootFileInput.php new file mode 100644 index 0000000..936ba5f --- /dev/null +++ b/src/Dto/Input/PxeBootFileInput.php @@ -0,0 +1,55 @@ +template = $bootFile->getTemplate(); + if ($bootFile->getClients()) { + foreach ($bootFile->getClients() as $client) { + $this->clients[] = new ClientOutput($client); + } + } + } + + public function createOrUpdateEntity(?PxeBootFile $bootFile = null): PxeBootFile + { + if (!$bootFile) { + $bootFile = new PxeBootFile(); + } + + $bootFile->setTemplate($this->template); + + foreach ($this->clients as $client) { + $clientsToAdd[] = $client->getEntity(); + } + $bootFile->setClients( $clientsToAdd ?? [] ); + + return $bootFile; + } +} \ No newline at end of file diff --git a/src/Dto/Output/PxeBootFileOutput.php b/src/Dto/Output/PxeBootFileOutput.php new file mode 100644 index 0000000..9000eab --- /dev/null +++ b/src/Dto/Output/PxeBootFileOutput.php @@ -0,0 +1,38 @@ +template = new PxeTemplateOutput($bootFile->getTemplate()); + $this->clients = $bootFile->getClients()->map( + fn(Client $client) => new ClientOutput($client) + )->toArray(); + + $this->createdAt = $bootFile->getCreatedAt(); + $this->createdBy = $bootFile->getCreatedBy(); + } +} \ No newline at end of file diff --git a/src/Entity/PxeBootFile.php b/src/Entity/PxeBootFile.php index 7316351..1498caf 100644 --- a/src/Entity/PxeBootFile.php +++ b/src/Entity/PxeBootFile.php @@ -66,4 +66,15 @@ class PxeBootFile extends AbstractEntity return $this; } + + public function setClients(array $clients): static + { + $this->clients->clear(); + + foreach ($clients as $client){ + $this->addClient($client); + } + + return $this; + } } diff --git a/src/Repository/PxeBootFileRepository.php b/src/Repository/PxeBootFileRepository.php index f71c3c5..5733c80 100644 --- a/src/Repository/PxeBootFileRepository.php +++ b/src/Repository/PxeBootFileRepository.php @@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -class PxeBootFileRepository extends ServiceEntityRepository +class PxeBootFileRepository extends AbstractRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, PxeBootFile::class); } - - // /** - // * @return PxeBootFile[] Returns an array of PxeBootFile objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('p.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } - - // public function findOneBySomeField($value): ?PxeBootFile - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } } diff --git a/src/Service/OgBoot/PxeBootFile/PostService.php b/src/Service/OgBoot/PxeBootFile/PostService.php new file mode 100644 index 0000000..cc19f4d --- /dev/null +++ b/src/Service/OgBoot/PxeBootFile/PostService.php @@ -0,0 +1,70 @@ + false, // Ignorar la verificación del certificado SSL + 'verify_host' => false, // Ignorar la verificación del nombre del host + ]); + + foreach ($bootFile->getClients() as $client) { + $data = [ + 'template_name' => $bootFile->getTemplate()->getName(), + 'mac' => $client->getMac(), + 'ip' => $client->getIp(), + 'server_ip' => '', + 'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(), + 'netmask' => $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask(), + 'computer_name' => $client->getName(), + 'netiface' => $client->getNetiface(), + 'group' => $client->getOrganizationalUnit()->getName(), + 'ogrepo' => $client->getRepository() ? $client->getRepository()->getIpAddress() : $client->getOrganizationalUnit()->getNetworkSettings()->getRepository()->getIpAddress(), + 'oglive' => '', + 'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()->getDns(), + 'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()->getProxy(), + 'ogunit' => '' + ]; + + try { + $response = $httpClient->request('POST', $this->ogBootApiUrl.'/ogboot/v1/pxes', [ + 'headers' => [ + 'accept' => 'application/json', + ], + 'json' => $data + ]); + } catch (TransportExceptionInterface $e) { + return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); + } + + return json_decode($response->getContent(), true); + } + + return 1; + } +} \ No newline at end of file diff --git a/src/State/Processor/PxeBootFileProcessor.php b/src/State/Processor/PxeBootFileProcessor.php new file mode 100644 index 0000000..9b03645 --- /dev/null +++ b/src/State/Processor/PxeBootFileProcessor.php @@ -0,0 +1,72 @@ +processCreateOrUpdate($data, $operation, $uriVariables, $context); + case $operation instanceof Delete: + return $this->processDelete($data, $operation, $uriVariables, $context); + } + } + + /** + * @throws \Exception + */ + private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): PxeBootFileOutput + { + if (!($data instanceof PxeBootFileInput)) { + throw new \Exception(sprintf('data is not instance of %s', PxeBootFileInput::class)); + } + + $entity = null; + if (isset($uriVariables['uuid'])) { + $entity = $this->bootFileRepository->findOneByUuid($uriVariables['uuid']); + } + + $pxeTemplate = $data->createOrUpdateEntity($entity); + $this->validator->validate($pxeTemplate); + $this->bootFileRepository->save($pxeTemplate); + + $this->postService->__invoke($pxeTemplate); + + return new PxeBootFileOutput($pxeTemplate); + } + + private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null + { + $bootFile = $this->bootFileRepository->findOneByUuid($uriVariables['uuid']); + $this->bootFileRepository->delete($bootFile); + + return null; + } +} diff --git a/src/State/Provider/PxeBootFileProvider.php b/src/State/Provider/PxeBootFileProvider.php new file mode 100644 index 0000000..f25bef9 --- /dev/null +++ b/src/State/Provider/PxeBootFileProvider.php @@ -0,0 +1,71 @@ +provideCollection($operation, $uriVariables, $context); + case $operation instanceof Patch: + case $operation instanceof Put: + return $this->provideInput($operation, $uriVariables, $context); + case $operation instanceof Get: + return $this->provideItem($operation, $uriVariables, $context); + } + } + + private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object + { + $paginator = $this->collectionProvider->provide($operation, $uriVariables, $context); + + $items = new \ArrayObject(); + foreach ($paginator->getIterator() as $item){ + $items[] = new PxeBootFileOutput($item); + } + + return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems()); + } + + public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + $item = $this->itemProvider->provide($operation, $uriVariables, $context); + + if (!$item) { + throw new NotFoundHttpException('PxeBootFile not found'); + } + + return new PxeBootFileOutput($item); + } + + public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + if (isset($uriVariables['uuid'])) { + $item = $this->itemProvider->provide($operation, $uriVariables, $context); + + return $item !== null ? new PxeBootFileInput($item) : null; + } + + return new PxeBootFileInput(); + } +}