refs #1797. Rename image. Crete image
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/27/head
Manuel Aranda Rosales 2025-04-01 19:44:59 +02:00
parent bc036b65cf
commit 912cf9b008
3 changed files with 95 additions and 3 deletions

View File

@ -0,0 +1,81 @@
<?php
namespace App\Controller\OgRepository\Image;
use App\Controller\OgRepository\AbstractOgRepositoryController;
use App\Dto\Input\DeployImageInput;
use App\Entity\Image;
use App\Entity\ImageImageRepository;
use App\Model\CommandTypes;
use App\Model\ImageStatus;
use App\Model\TraceStatus;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController]
class RenameAction extends AbstractOgRepositoryController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Image $image): JsonResponse
{
$repositories = $image->getImageImageRepositories();
if ($repositories->count() === 0) {
return new JsonResponse(data: ['error' => 'Image is not in any repository', 'code' => Response::HTTP_INTERNAL_SERVER_ERROR], status: Response::HTTP_BAD_REQUEST);
}
$allGood = true;
foreach ($repositories as $repository) {
try {
$content = $this->createRequest('GET', 'http://'.$repository->getRepository()->getIp(). ':8006/ogrepository/v1/status');
} catch (TransportExceptionInterface $e) {
$allGood = false;
break;
}
}
if (!$allGood) {
$this->logger->info('Image is not available in all repositories', ['image' => $image->getName()]);
return new JsonResponse(data: ['error' => 'Image is not available in all repositories', 'code' => Response::HTTP_INTERNAL_SERVER_ERROR], status: Response::HTTP_BAD_REQUEST);
}
$conditional = false;
foreach ($repositories as $repository) {
$params = [
'json' => [
'ID_img' => $repository->getImageFullsum(),
'image_new_name' => $image->getName().'_bkp',
]
];
$content = $this->createRequest('POST', 'http://'.$repository->getRepository()->getIp().':8006/ogrepository/v1/images/rename', $params);
if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
$conditional = true;
break;
}
}
if ($conditional) {
return new JsonResponse(data: ['error' => 'Error renaming image'], status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
$image->setName($image->getName().'_bkp');
$this->entityManager->persist($image);
$this->entityManager->flush();
return new JsonResponse(data: [], status: Response::HTTP_OK);
}
}

View File

@ -5,6 +5,7 @@ namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\ImageImageRepositoryOutput;
use App\Dto\Output\ImageOutput;
use App\Dto\Output\ImageRepositoryOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Dto\Output\PartitionOutput;
@ -20,7 +21,6 @@ use Symfony\Component\Validator\Constraints as Assert;
final class ImageInput
{
#[Assert\NotBlank(message: 'validators.image.name.not_blank')]
#[Groups(['image:write'])]
#[ApiProperty(description: 'The name of the image', example: "Image 1")]
public ?string $name = null;
@ -37,6 +37,10 @@ final class ImageInput
#[ApiProperty(description: 'The type of the image', example: "Server")]
public ?string $source = 'input';
#[Groups(['image:write'])]
#[ApiProperty(description: 'The optional selected image')]
public ?ImageOutput $selectedImage = null;
#[Groups(['image:write'])]
#[ApiProperty(description: 'The software profile of the image')]
public ?SoftwareProfileOutput $softwareProfile = null;

View File

@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Controller\OgAgent\CreateImageAction;
use App\Controller\OgRepository\Image\RenameAction;
use App\Controller\OgRepository\Image\TransferAction;
use App\Dto\Input\ImageInput;
use App\Dto\Input\ImageRepositoryInput;
@ -26,6 +27,7 @@ readonly class ImageProcessor implements ProcessorInterface
private ImageRepository $imageRepository,
private ValidatorInterface $validator,
private CreateImageAction $createImageActionController,
private RenameAction $renameActionController,
)
{
}
@ -64,10 +66,15 @@ readonly class ImageProcessor implements ProcessorInterface
$image = $data->createOrUpdateEntity($entity);
$this->validator->validate($image);
if ($data->source !== 'input') {
$response = $this->createImageActionController->__invoke($image);
if ($data->selectedImage){
$content = $this->renameActionController->__invoke($image);
if ($content->getStatusCode() !== 200){
throw new \Exception('Error renaming image');
}
}
$response = $this->createImageActionController->__invoke($image);
$this->imageRepository->save($image);
return new ImageOutput($image);