82 lines
3.8 KiB
PHP
82 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgBoot\PxeBootFile;
|
|
|
|
use App\Controller\OgBoot\AbstractOgBootController;
|
|
use App\Dto\Input\PxeTemplateSyncClientInput;
|
|
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\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(PxeTemplateSyncClientInput $input, PxeTemplate $pxeTemplate, HttpClientInterface $httpClient): JsonResponse
|
|
{
|
|
/** @var Client $client */
|
|
$client = $input->client->getEntity();
|
|
|
|
$data = [
|
|
'template_name' => $pxeTemplate->getName(),
|
|
'mac' => strtolower($client->getMac()),
|
|
'lang' => 'es_ES.UTF_8',
|
|
'ip' => $client->getIp(),
|
|
'server_ip' => $this->ogBootApiUrl,
|
|
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
|
|
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
|
|
'computer_name' => $client->getName(),
|
|
'netiface' => $client->getNetiface(),
|
|
'group' => $client->getOrganizationalUnit()->getName(),
|
|
'ogrepo' => $client->getRepository()->getIp() ,
|
|
'ogcore' => $this->ogCoreIP,
|
|
'oglive' => $this->ogBootApiUrl,
|
|
'oglog' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgLog(),
|
|
'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
|
|
? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl,
|
|
'oglivedir' => $client->getOgLive()->getFilename(),
|
|
'ogprof' => 'false',
|
|
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
|
|
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), //optional
|
|
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), //optional
|
|
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), //optional
|
|
// 'ogunit' => '', //eliminar
|
|
'resolution' => '788'
|
|
];
|
|
|
|
try {
|
|
$response = $httpClient->request('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes', [
|
|
'headers' => [
|
|
'accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'json' => $data
|
|
]);
|
|
} catch (TransportExceptionInterface $e) {
|
|
$client->setPxeSync(false);
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
return new JsonResponse( data: $e->getMessage(), status: Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
$client->setPxeSync(true);
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return new JsonResponse(data: json_decode($response->getContent(), true), status: Response::HTTP_OK);
|
|
}
|
|
} |