From 6df1057b203f5719dbd8f922d1c04464f80602c7 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Thu, 8 May 2025 16:36:04 +0200 Subject: [PATCH] refs #1973. Linked image to partition --- config/services/api_platform.yaml | 2 +- src/Controller/DeployImageAction.php | 1 + .../OgAgent/Webhook/StatusController.php | 18 +++++++++ src/Dto/Output/ImageImageRepositoryOutput.php | 4 +- src/Dto/Output/PartitionOutput.php | 10 +++++ src/Entity/Image.php | 37 +++++++++++++++++++ src/Entity/Partition.php | 6 +-- 7 files changed, 72 insertions(+), 6 deletions(-) diff --git a/config/services/api_platform.yaml b/config/services/api_platform.yaml index a9eed1a..266683a 100644 --- a/config/services/api_platform.yaml +++ b/config/services/api_platform.yaml @@ -294,7 +294,7 @@ services: api_platform.filter.trace.search: parent: 'api_platform.doctrine.orm.search_filter' - arguments: [ { 'id': 'exact', 'command.id': 'exact', 'client.id': 'exact', status: 'exact' } ] + arguments: [ { 'id': 'exact', 'command': 'exact', 'client.id': 'exact', status: 'exact' } ] tags: [ 'api_platform.filter' ] api_platform.filter.trace.order: diff --git a/src/Controller/DeployImageAction.php b/src/Controller/DeployImageAction.php index 4912d29..4d49607 100644 --- a/src/Controller/DeployImageAction.php +++ b/src/Controller/DeployImageAction.php @@ -55,6 +55,7 @@ class DeployImageAction extends AbstractController 'method' => $input->method, 'client' => $client->getEntity()->getUuid(), 'image' => $image->getUuid(), + 'imageName' => $image->getName(), 'numDisk' => (string) $input->diskNumber, 'numPartition' => (string) $input->partitionNumber, ]; diff --git a/src/Controller/OgAgent/Webhook/StatusController.php b/src/Controller/OgAgent/Webhook/StatusController.php index 6871f1a..9e8956b 100644 --- a/src/Controller/OgAgent/Webhook/StatusController.php +++ b/src/Controller/OgAgent/Webhook/StatusController.php @@ -161,6 +161,24 @@ class StatusController extends AbstractController $client->setStatus(ClientStatus::OG_LIVE); $this->entityManager->persist($client); $this->entityManager->persist($trace); + + if ($data['nfn'] === self::RESTORE_IMAGE) { + $partitionData = json_decode(json_encode($trace->getInput()), true); + + $numDisk = (int) $partitionData['numDisk'] ?? null; + $numPartition = (int) $partitionData['numPartition'] ?? null; + + $imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $partitionData['image']]); + + $partition = $this->entityManager->getRepository(Partition::class) + ->findOneBy(['diskNumber' => $numDisk, 'partitionNumber' => $numPartition, 'client' => $client]); + + if ($partition) { + $partition->setImage($imageImageRepository); + $this->entityManager->persist($partition); + } + } + $this->entityManager->flush(); } diff --git a/src/Dto/Output/ImageImageRepositoryOutput.php b/src/Dto/Output/ImageImageRepositoryOutput.php index 9d4c4a0..0877055 100644 --- a/src/Dto/Output/ImageImageRepositoryOutput.php +++ b/src/Dto/Output/ImageImageRepositoryOutput.php @@ -13,12 +13,12 @@ class ImageImageRepositoryOutput extends AbstractOutput public ?ImageOutput $image = null; #[Groups(['image-image-repository:read', 'image:read'])] - public ?ImageRepositoryOutput $imageRepository= null; + public ?ImageRepositoryOutput $imageRepository = null; #[Groups(['image-image-repository:read', 'image:read'])] public string $status; - #[Groups(['image-image-repository:read', 'image:read'])] + #[Groups(['image-image-repository:read', 'image:read', 'partition:read'])] public string $name; #[Groups(['image-image-repository:read', 'image:read'])] diff --git a/src/Dto/Output/PartitionOutput.php b/src/Dto/Output/PartitionOutput.php index a9bb3d7..394abcb 100644 --- a/src/Dto/Output/PartitionOutput.php +++ b/src/Dto/Output/PartitionOutput.php @@ -3,6 +3,7 @@ namespace App\Dto\Output; use ApiPlatform\Metadata\Get; +use App\Entity\ImageImageRepository; use App\Entity\Partition; use Symfony\Component\Serializer\Annotation\Groups; @@ -30,6 +31,9 @@ class PartitionOutput extends AbstractOutput #[Groups(['partition:read', 'client:read'])] public ?OperativeSystemOutput $operativeSystem = null; + #[Groups(['partition:read', 'client:read'])] + public ?ImageImageRepositoryOutput $image = null; + #[Groups(['partition:read', 'client:read'])] public ?int $memoryUsage = null; @@ -43,9 +47,15 @@ class PartitionOutput extends AbstractOutput $this->size = $partition->getSize() / 1024 ; $this->cacheContent = $partition->getCacheContent(); $this->filesystem = $partition->getFilesystem(); + if ($partition->getOperativeSystem()) { $this->operativeSystem = new OperativeSystemOutput($partition->getOperativeSystem()); } + + if ($partition->getImage()) { + $this->image = new ImageImageRepositoryOutput($partition->getImage()); + } + $this->memoryUsage = $partition->getMemoryUsage() / 100; } } \ No newline at end of file diff --git a/src/Entity/Image.php b/src/Entity/Image.php index 727c212..1c6b8a3 100644 --- a/src/Entity/Image.php +++ b/src/Entity/Image.php @@ -49,10 +49,17 @@ class Image extends AbstractEntity #[ORM\Column(nullable: true)] private ?int $version = null; + /** + * @var Collection + */ + #[ORM\OneToMany(mappedBy: 'image', targetEntity: GitImageRepository::class,cascade: ['persist'], orphanRemoval: true)] + private Collection $gitImageRepositories; + public function __construct() { parent::__construct(); $this->imageImageRepositories = new ArrayCollection(); + $this->gitImageRepositories = new ArrayCollection(); } public function getDescription(): ?string @@ -206,4 +213,34 @@ class Image extends AbstractEntity return $this; } + + /** + * @return Collection + */ + public function getGitImageRepositories(): Collection + { + return $this->gitImageRepositories; + } + + public function addGitImageRepository(GitImageRepository $gitImageRepository): static + { + if (!$this->gitImageRepositories->contains($gitImageRepository)) { + $this->gitImageRepositories->add($gitImageRepository); + $gitImageRepository->setImage($this); + } + + return $this; + } + + public function removeGitImageRepository(GitImageRepository $gitImageRepository): static + { + if ($this->gitImageRepositories->removeElement($gitImageRepository)) { + // set the owning side to null (unless already changed) + if ($gitImageRepository->getImage() === $this) { + $gitImageRepository->setImage(null); + } + } + + return $this; + } } diff --git a/src/Entity/Partition.php b/src/Entity/Partition.php index 554763d..5814948 100644 --- a/src/Entity/Partition.php +++ b/src/Entity/Partition.php @@ -40,7 +40,7 @@ class Partition extends AbstractEntity #[ORM\ManyToOne(inversedBy: 'partitions')] #[ORM\JoinColumn(nullable: true)] - private ?Image $image = null; + private ?ImageImageRepository $image = null; public function getDiskNumber(): ?int { @@ -150,12 +150,12 @@ class Partition extends AbstractEntity return $this; } - public function getImage(): ?Image + public function getImage(): ?ImageImageRepository { return $this->image; } - public function setImage(?Image $image): static + public function setImage(?ImageImageRepository $image): static { $this->image = $image;