140 lines
5.4 KiB
PHP
140 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgAgent\Webhook;
|
|
|
|
use App\Controller\OgRepository\Image\CreateAuxFilesAction;
|
|
use App\Entity\Image;
|
|
use App\Entity\OperativeSystem;
|
|
use App\Entity\Partition;
|
|
use App\Entity\Software;
|
|
use App\Entity\SoftwareProfile;
|
|
use App\Entity\Trace;
|
|
use App\Model\ClientStatus;
|
|
use App\Model\ImageStatus;
|
|
use App\Model\SoftwareTypes;
|
|
use App\Model\TraceStatus;
|
|
use App\Service\CreatePartitionService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Attribute\AsController;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
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 ClientsController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
public readonly CreateAuxFilesAction $createAuxFilesAction,
|
|
protected readonly CreatePartitionService $createPartitionService
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
#[Route('/opengnsys/rest/clients/status/webhook', methods: ['POST'])]
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$data = $request->toArray();
|
|
$requiredFields = ['nfn', 'idi', 'dsk', 'par', 'ids', 'res', 'der', 'job_id'];
|
|
|
|
foreach ($requiredFields as $field) {
|
|
if (!isset($data[$field])) {
|
|
return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
if ($data['nfn'] === 'RESPUESTA_CrearImagen') {
|
|
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
|
$image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]);
|
|
|
|
if (!$image) {
|
|
return new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if ($data['res'] === 1) {
|
|
$trace->setStatus(TraceStatus::SUCCESS);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$image->setStatus(ImageStatus::AUX_FILES_PENDING);
|
|
$image->setCreated(true);
|
|
if (isset($data['cfg'])) {
|
|
$this->createPartitionService->__invoke($data, $image->getClient());
|
|
}
|
|
$this->createSoftwareProfile($data['inv_sft'], $image);
|
|
$this->createAuxFilesAction->__invoke($image);
|
|
} else {
|
|
$trace->setStatus(TraceStatus::FAILED);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$trace->setOutput($data['der']);
|
|
$image->setCreated(false);
|
|
}
|
|
$this->entityManager->persist($image);
|
|
|
|
$client = $trace->getClient();
|
|
$client->setStatus(ClientStatus::OG_LIVE);
|
|
|
|
$this->entityManager->persist($trace);
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
if ($data['nfn'] === 'RESPUESTA_RestaurarImagen') {
|
|
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
|
$image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]);
|
|
|
|
if ($data['res'] === 1) {
|
|
$trace->setStatus(TraceStatus::SUCCESS);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$image->setStatus(ImageStatus::PENDING);
|
|
} else {
|
|
$trace->setStatus(TraceStatus::FAILED);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$trace->setOutput($data['der']);
|
|
}
|
|
|
|
$this->entityManager->persist($image);
|
|
$this->entityManager->persist($trace);
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
return new JsonResponse([], Response::HTTP_OK);
|
|
}
|
|
|
|
public function createSoftwareProfile (string $base64Data, Image $image): void
|
|
{
|
|
$decodedData = base64_decode($base64Data);
|
|
|
|
$softwareList = explode("\n", $decodedData);
|
|
|
|
$softwareProfile = new SoftwareProfile();
|
|
$softwareProfile->setDescription('Perfil : '.$image->getName());
|
|
$softwareProfile->setOrganizationalUnit($image->getClient()->getOrganizationalUnit());
|
|
|
|
foreach ($softwareList as $software) {
|
|
$software = trim($software);
|
|
$softwareEntity = $this->entityManager->getRepository(Software::class)->findOneBy(['name' => $software]);
|
|
if (!$softwareEntity) {
|
|
$softwareEntity = new Software();
|
|
$softwareEntity->setName($software);
|
|
$softwareEntity->setType(SoftwareTypes::APPLICATION);
|
|
$this->entityManager->persist($softwareEntity);
|
|
}
|
|
|
|
$softwareEntity->addSoftwareProfile($softwareProfile);
|
|
}
|
|
|
|
$this->entityManager->persist($softwareProfile);
|
|
$this->entityManager->flush();
|
|
}
|
|
} |