From 36863bff7bfbde5f8c6ad11c943f890c285e5403 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 19 Aug 2024 16:03:37 +0200 Subject: [PATCH] refs #632. Create new fields and updated API --- config/api_platform/PxeBootFile.yaml | 17 +++++++--- migrations/Version20240819140045.php | 33 +++++++++++++++++++ .../OgBoot/OgLive/InstallAction.php | 9 +++-- .../OgBoot/OgLive/SetDefaultAction.php | 7 +++- ...{GetAction.php => GetCollectionAction.php} | 4 +-- .../OgBoot/PxeTemplate/PostAction.php | 7 +++- src/Dto/Output/OgLiveOutput.php | 8 +++++ src/Dto/Output/PxeBootFileOutput.php | 4 +++ src/Entity/OgLive.php | 15 +++++++++ src/Entity/PxeBootFile.php | 2 ++ 10 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 migrations/Version20240819140045.php rename src/Controller/OgBoot/PxeBootFile/{GetAction.php => GetCollectionAction.php} (94%) diff --git a/config/api_platform/PxeBootFile.yaml b/config/api_platform/PxeBootFile.yaml index 6c5886d..f8cafaa 100644 --- a/config/api_platform/PxeBootFile.yaml +++ b/config/api_platform/PxeBootFile.yaml @@ -9,20 +9,29 @@ resources: groups: ['pxe-boot-file:write'] operations: ApiPlatform\Metadata\GetCollection: - provider: App\State\Provider\PPxeBootFileProvider + provider: App\State\Provider\PxeBootFileProvider filters: - 'api_platform.filter.pxe_boot_file.order' - 'api_platform.filter.pxe_boot_file.search' ApiPlatform\Metadata\Get: - provider: App\State\Provider\PxeTemplateProvider + provider: App\State\Provider\PxeBootFileProvider ApiPlatform\Metadata\Put: - provider: App\State\Provider\PxeTemplateProvider + provider: App\State\Provider\PxeBootFileProvider ApiPlatform\Metadata\Patch: - provider: App\State\Provider\PxeTemplateProvider + provider: App\State\Provider\PxeBootFileProvider ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Delete: ~ + get_collection: + shortName: PxeBootFile Server + description: Get collection of PxeBootFile + class: ApiPlatform\Metadata\GetCollection + method: GET + input: false + uriTemplate: /pxe-boot-files/server/get-collection + controller: App\Controller\OgBoot\PxeBootFile\GetCollectionAction + properties: App\Entity\PxeBootFile: id: diff --git a/migrations/Version20240819140045.php b/migrations/Version20240819140045.php new file mode 100644 index 0000000..01a67ef --- /dev/null +++ b/migrations/Version20240819140045.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE og_live ADD `default` TINYINT(1) DEFAULT NULL'); + $this->addSql('ALTER TABLE pxe_boot_file ADD synchronized TINYINT(1) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE og_live DROP `default`'); + $this->addSql('ALTER TABLE pxe_boot_file DROP synchronized'); + } +} diff --git a/src/Controller/OgBoot/OgLive/InstallAction.php b/src/Controller/OgBoot/OgLive/InstallAction.php index 485cb4d..b2bcdac 100644 --- a/src/Controller/OgBoot/OgLive/InstallAction.php +++ b/src/Controller/OgBoot/OgLive/InstallAction.php @@ -4,6 +4,7 @@ namespace App\Controller\OgBoot\OgLive; use App\Controller\OgBoot\AbstractOgLiveController; use App\Entity\OgLive; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -22,7 +23,7 @@ class InstallAction extends AbstractOgLiveController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(OgLive $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(OgLive $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse { try { $response = $httpClient->request('POST', $this->ogBootApiUrl.'/ogboot/v1/oglives/install', [ @@ -31,13 +32,17 @@ class InstallAction extends AbstractOgLiveController 'Content-Type' => 'application/json', ], 'json' => [ - 'isoname' => $data->getName() + 'url' => $data->getDownloadUrl() ] ]); } catch (TransportExceptionInterface $e) { return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); } + $data->setInstalled(true); + $entityManager->persist($data); + $entityManager->flush(); + $data = json_decode($response->getContent(), true); return new JsonResponse( data: $data, status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/OgLive/SetDefaultAction.php b/src/Controller/OgBoot/OgLive/SetDefaultAction.php index 0f96d13..ffdf033 100644 --- a/src/Controller/OgBoot/OgLive/SetDefaultAction.php +++ b/src/Controller/OgBoot/OgLive/SetDefaultAction.php @@ -4,6 +4,7 @@ namespace App\Controller\OgBoot\OgLive; use App\Controller\OgBoot\AbstractOgLiveController; use App\Entity\OgLive; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; @@ -22,7 +23,7 @@ class SetDefaultAction extends AbstractOgLiveController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(OgLive $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(OgLive $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse { try { $response = $httpClient->request('POST', $this->ogBootApiUrl.'/ogboot/v1/oglives/default', [ @@ -37,6 +38,10 @@ class SetDefaultAction extends AbstractOgLiveController return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); } + $data->setDefault(true); + $entityManager->persist($data); + $entityManager->flush(); + $data = json_decode($response->getContent(), true); return new JsonResponse( data: $data, status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/PxeBootFile/GetAction.php b/src/Controller/OgBoot/PxeBootFile/GetCollectionAction.php similarity index 94% rename from src/Controller/OgBoot/PxeBootFile/GetAction.php rename to src/Controller/OgBoot/PxeBootFile/GetCollectionAction.php index 766da38..1b07fc6 100644 --- a/src/Controller/OgBoot/PxeBootFile/GetAction.php +++ b/src/Controller/OgBoot/PxeBootFile/GetCollectionAction.php @@ -16,7 +16,7 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; #[AsController] -class GetAction extends AbstractOgLiveController +class GetCollectionAction extends AbstractOgLiveController { /** * @throws TransportExceptionInterface @@ -27,7 +27,7 @@ class GetAction extends AbstractOgLiveController public function __invoke(PxeBootFile $data, HttpClientInterface $httpClient): JsonResponse { try { - $response = $httpClient->request('GET', $this->ogBootApiUrl.'/ogboot/v1/pxes/'.$data->getName(), [ + $response = $httpClient->request('GET', $this->ogBootApiUrl.'/ogboot/v1/pxes', [ 'headers' => [ 'accept' => 'application/json', ], diff --git a/src/Controller/OgBoot/PxeTemplate/PostAction.php b/src/Controller/OgBoot/PxeTemplate/PostAction.php index a2f996c..6daf8d8 100644 --- a/src/Controller/OgBoot/PxeTemplate/PostAction.php +++ b/src/Controller/OgBoot/PxeTemplate/PostAction.php @@ -4,6 +4,7 @@ namespace App\Controller\OgBoot\PxeTemplate; use App\Controller\OgBoot\AbstractOgLiveController; use App\Entity\PxeTemplate; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -23,7 +24,7 @@ class PostAction extends AbstractOgLiveController * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient): JsonResponse + public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse { try { $response = $httpClient->request('POST', $this->ogBootApiUrl.'/ogboot/v1/pxe-templates', [ @@ -37,6 +38,10 @@ class PostAction extends AbstractOgLiveController ], ]); + $data->setSynchronized(true); + $entityManager->persist($data); + $entityManager->flush(); + $data = json_decode($response->getContent(), true); return new JsonResponse($data, Response::HTTP_OK); diff --git a/src/Dto/Output/OgLiveOutput.php b/src/Dto/Output/OgLiveOutput.php index c4462b5..28e117e 100644 --- a/src/Dto/Output/OgLiveOutput.php +++ b/src/Dto/Output/OgLiveOutput.php @@ -16,6 +16,12 @@ final class OgLiveOutput extends AbstractOutput #[Groups(['og-live:read'])] public ?bool $synchronized = false; + #[Groups(['og-live:read'])] + public ?bool $installed = false; + + #[Groups(['og-live:read'])] + public ?bool $default = false; + #[Groups(['og-live:read'])] public ?string $downloadUrl = ''; @@ -31,6 +37,8 @@ final class OgLiveOutput extends AbstractOutput $this->name = $ogLive->getName(); $this->synchronized = $ogLive->isSynchronized(); + $this->installed = $ogLive->isInstalled(); + $this->default = $ogLive->isDefault(); $this->downloadUrl = $ogLive->getDownloadUrl(); $this->createdAt = $ogLive->getCreatedAt(); $this->createdBy = $ogLive->getCreatedBy(); diff --git a/src/Dto/Output/PxeBootFileOutput.php b/src/Dto/Output/PxeBootFileOutput.php index 9000eab..ff76bc6 100644 --- a/src/Dto/Output/PxeBootFileOutput.php +++ b/src/Dto/Output/PxeBootFileOutput.php @@ -17,6 +17,9 @@ final class PxeBootFileOutput extends AbstractOutput #[Groups(['pxe-boot-file:read'])] public array $clients; + #[Groups(['pxe-boot-file:read'])] + public ?bool $synchronized = null; + #[Groups(['pxe-boot-file:read'])] public \DateTime $createdAt; @@ -32,6 +35,7 @@ final class PxeBootFileOutput extends AbstractOutput fn(Client $client) => new ClientOutput($client) )->toArray(); + $this->synchronized = $bootFile->isSynchronized(); $this->createdAt = $bootFile->getCreatedAt(); $this->createdBy = $bootFile->getCreatedBy(); } diff --git a/src/Entity/OgLive.php b/src/Entity/OgLive.php index 14decae..2ecb106 100644 --- a/src/Entity/OgLive.php +++ b/src/Entity/OgLive.php @@ -41,6 +41,9 @@ class OgLive extends AbstractEntity #[ORM\Column(nullable: true)] private ?bool $installed = null; + #[ORM\Column(nullable: true)] + private ?bool $default = null; + public function getDownloadUrl(): ?string { return $this->downloadUrl; @@ -148,4 +151,16 @@ class OgLive extends AbstractEntity return $this; } + + public function isDefault(): ?bool + { + return $this->default; + } + + public function setDefault(?bool $default): static + { + $this->default = $default; + + return $this; + } } diff --git a/src/Entity/PxeBootFile.php b/src/Entity/PxeBootFile.php index 1498caf..b14dd3f 100644 --- a/src/Entity/PxeBootFile.php +++ b/src/Entity/PxeBootFile.php @@ -10,6 +10,8 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: PxeBootFileRepository::class)] class PxeBootFile extends AbstractEntity { + use SynchronizedTrait; + #[ORM\ManyToOne] private ?PxeTemplate $template = null;