60 lines
2.3 KiB
PHP
60 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgBoot\PxeTemplate;
|
|
|
|
use App\Controller\OgBoot\AbstractOgBootController;
|
|
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 PostAction extends AbstractOgBootController
|
|
{
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function __invoke(PxeTemplate $data): JsonResponse
|
|
{
|
|
$params = [
|
|
'json' => [
|
|
'name_template' => $data->getName(),
|
|
'content_template' => $data->getTemplateContent(),
|
|
]
|
|
];
|
|
|
|
$bootServer = $data->getBootServer();
|
|
if (!$bootServer) {
|
|
throw new BadRequestHttpException('This PxeTemplate is not attached to any BootServer.');
|
|
}
|
|
|
|
$this->logger->info(sprintf('Synchronizing PxeTemplate "%s" to BootServer "%s" at IP %s', $data->getName(), $bootServer->getName(), $bootServer->getIp()));
|
|
|
|
$content = $this->createRequestFromBootServer($bootServer, 'POST', '/ogboot/v1/pxe-templates' , $params);
|
|
|
|
if (isset($content['error']) ) {
|
|
$data->setSynchronized(false);
|
|
$this->entityManager->persist($data);
|
|
$this->entityManager->flush();
|
|
throw new BadRequestHttpException('Sync failed: ' . ($content['message'] ?? $content['error']));
|
|
}
|
|
|
|
$data->setSynchronized(true);
|
|
$this->entityManager->persist($data);
|
|
$this->entityManager->flush();
|
|
|
|
return new JsonResponse(data: $content, status: Response::HTTP_OK);
|
|
}
|
|
} |