ogcore/src/Controller/OgDhcp/Subnet/PutAction.php

58 lines
2.1 KiB
PHP

<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\Subnet;
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 PutAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data): JsonResponse
{
if (!$data->getId()) {
throw new BadRequestHttpException('Id is required');
}
$params = [
'json' => [
'mask' => $data->getNetmask(),
'address' => $data->getIpAddress(),
'nextServer' => $data->getNextServer(),
'bootFileName' => $data->getBootFileName(),
'router' => $data->getRouter(),
'DNS' => $data->getDns()
]
];
$dhcpServer = $data->getDhcpServer();
if (!$dhcpServer) {
throw new \Exception('La subred no tiene un servidor DHCP asociado.');
}
$port = $dhcpServer->getApiPort() ?? 8081;
$url = sprintf('http://%s:%d/ogdhcp/v1/subnets/%d', $dhcpServer->getIp(), $port, $data->getServerId());
$content = $this->createRequest('PUT', $url, $params);
$this->entityManager->persist($data);
$this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}