62 lines
2.4 KiB
PHP
62 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgBoot\PxeTemplate;
|
|
|
|
use App\Controller\OgBoot\AbstractOgBootController;
|
|
use App\Entity\Client;
|
|
use App\Entity\PxeTemplate;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Attribute\AsController;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
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 DeleteAction extends AbstractOgBootController
|
|
{
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function __invoke(PxeTemplate $data): JsonResponse
|
|
{
|
|
$bootServer = $data->getBootServer();
|
|
if (!$bootServer) {
|
|
throw new BadRequestHttpException('This PxeTemplate is not attached to any BootServer.');
|
|
}
|
|
|
|
// Notify remote server first
|
|
$content = $this->createRequestFromBootServer($bootServer, 'DELETE', '/ogboot/v1/pxe-templates/'.$data->getName());
|
|
|
|
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
|
|
throw new BadRequestHttpException('Remote server error: ' . $content['error']);
|
|
}
|
|
|
|
// Delete from local DB if remote call succeeded
|
|
$this->entityManager->remove($data);
|
|
$this->entityManager->flush();
|
|
|
|
// Check for default template to inform the user
|
|
$defaultTemplateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);
|
|
|
|
$responseData = [
|
|
'status' => 'success',
|
|
'message' => 'Plantilla eliminada correctamente.'
|
|
];
|
|
|
|
if ($defaultTemplateEntity === null) {
|
|
$responseData['warning'] = 'No se ha detectado ninguna otra plantilla marcada por defecto en este servidor.';
|
|
}
|
|
|
|
return new JsonResponse($responseData, Response::HTTP_OK);
|
|
}
|
|
} |