refs #614. Itnegration ogDhcp

feature/integration-dhcp
Manuel Aranda Rosales 2024-08-23 12:52:01 +02:00
parent b26b215ffc
commit f88bb46f3e
10 changed files with 223 additions and 28 deletions

View File

@ -23,6 +23,42 @@ resources:
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
get_collection:
shortName: Subnet Server
description: Get collection of Subnet
class: ApiPlatform\Metadata\GetCollection
method: GET
input: false
uriTemplate: /subnets/server/get-collection
controller: App\Controller\OgDhcp\Subnet\GetCollectionAction
get:
shortName: Subnet Server
description: Get Subnet
class: ApiPlatform\Metadata\Get
method: GET
input: false
uriTemplate: /subnets/server/{uuid}/get
controller: App\Controller\OgDhcp\Subnet\GetAction
post:
shortName: Subnet Server
description: Create Subnet
class: ApiPlatform\Metadata\Post
method: POST
input: false
uriTemplate: /subnets/server/{uuid}/post
controller: App\Controller\OgDhcp\Subnet\PostAction
delete:
shortName: Subnet Server
description: Delete Subnet
class: ApiPlatform\Metadata\Get
method: GET
input: false
uriTemplate: /subnets/server/{uuid}/delete
controller: App\Controller\OgDhcp\Subnet\DeleteAction
properties:
App\Entity\Subnet:
id:

View File

@ -6,7 +6,15 @@ namespace App\Controller\OgDhcp;
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\HttpException;
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]
abstract class AbstractOgDhcpController extends AbstractController
@ -17,4 +25,32 @@ abstract class AbstractOgDhcpController extends AbstractController
)
{
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function createRequest (HttpClientInterface $httpClient, string $method, string $url, array $params = []): JsonResponse|array
{
$params = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json'
],
]);
try {
$response = $httpClient->request($method, $url, $params);
return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
$response = $e->getResponse();
$content = json_decode($response->getContent(false), true);
throw new HttpException($response->getStatusCode(), $content['error'] ?? 'An error occurred');
} catch (TransportExceptionInterface $e) {
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, $e->getMessage());
}
}
}

View File

@ -0,0 +1,48 @@
<?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\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 AddHostAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{
if (!$data) {
throw new ValidatorException('Data URL is required');
}
$params = [
'json' => [
'host' => '',
'macAddress' => '',
'address' => '',
'nextServer' => '',
]
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/opengnsys3/rest/subnets/'.$data->getId().'/hosts', $params);
$this->entityManager->persist($data);
$this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -3,10 +3,37 @@
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\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 AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{
if (!$data->getChecksum()) {
throw new ValidatorException('Checksum is required');
}
$content = $this->createRequest($httpClient, 'DELETE', $this->ogDhcpApiUrl.'/opengnsys3/rest/dhcp/subnets/'.$data->getChecksum());
$this->entityManager->remove($data);
$this->entityManager->flush();
return new JsonResponse(status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
class DeleteHostAction
{
}

View File

@ -2,12 +2,12 @@
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgBoot\AbstractOgLiveController;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\OgLive;
use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -23,20 +23,14 @@ class GetAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(OgLive $data, HttpClientInterface $httpClient): JsonResponse
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{
try {
$response = $httpClient->request('GET', $this->ogDhcpApiUrl.'/opengnsys3/rest/dhcp/subnets/'.$data->getChecksum(), [
'headers' => [
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
if (!$data->getId()) {
throw new ValidatorException('Checksum is required');
}
$data = json_decode($response->getContent(), true);
$content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl.'/opengnsys3/rest/dhcp/subnets/'.$data->getId());
return new JsonResponse( data: $data, status: Response::HTTP_OK);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -2,7 +2,6 @@
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgBoot\AbstractOgLiveController;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
@ -24,18 +23,8 @@ class GetCollectionAction extends AbstractOgDhcpController
*/
public function __invoke(HttpClientInterface $httpClient): JsonResponse
{
try {
$response = $httpClient->request('GET', $this->ogDhcpApiUrl.'/opengnsys3/rest/subnets', [
'headers' => [
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
$content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl . '/opengnsys3/rest/subnets');
$data = json_decode($response->getContent(), true);
return new JsonResponse( data: $data, status: Response::HTTP_OK);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use Symfony\Component\HttpKernel\Attribute\AsController;
#[AsController]
class GetHostAction extends AbstractOgDhcpController
{
}

View File

@ -2,7 +2,44 @@
namespace App\Controller\OgDhcp\Subnet;
class PostAction
{
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\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 AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{
if (!$data) {
throw new ValidatorException('Data URL is required');
}
$params = [
'json' => [
'url' => ''
]
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/opengnsys3/rest/subnets', $params);
$this->entityManager->persist($data);
$this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
class PutHostAction
{
}