From f88bb46f3e808bbc3e176129688feb6ec1d99078 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Fri, 23 Aug 2024 12:52:01 +0200 Subject: [PATCH] refs #614. Itnegration ogDhcp --- config/api_platform/Subnet.yaml | 36 ++++++++++++++ .../OgDhcp/AbstractOgDhcpController.php | 36 ++++++++++++++ .../OgDhcp/Subnet/AddHostAction.php | 48 +++++++++++++++++++ src/Controller/OgDhcp/Subnet/DeleteAction.php | 27 +++++++++++ .../OgDhcp/Subnet/DeleteHostAction.php | 8 ++++ src/Controller/OgDhcp/Subnet/GetAction.php | 20 +++----- .../OgDhcp/Subnet/GetCollectionAction.php | 15 +----- .../OgDhcp/Subnet/GetHostAction.php | 12 +++++ src/Controller/OgDhcp/Subnet/PostAction.php | 41 +++++++++++++++- .../OgDhcp/Subnet/PutHostAction.php | 8 ++++ 10 files changed, 223 insertions(+), 28 deletions(-) create mode 100644 src/Controller/OgDhcp/Subnet/AddHostAction.php create mode 100644 src/Controller/OgDhcp/Subnet/DeleteHostAction.php create mode 100644 src/Controller/OgDhcp/Subnet/GetHostAction.php create mode 100644 src/Controller/OgDhcp/Subnet/PutHostAction.php diff --git a/config/api_platform/Subnet.yaml b/config/api_platform/Subnet.yaml index 7125a1a..c9bd475 100644 --- a/config/api_platform/Subnet.yaml +++ b/config/api_platform/Subnet.yaml @@ -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: diff --git a/src/Controller/OgDhcp/AbstractOgDhcpController.php b/src/Controller/OgDhcp/AbstractOgDhcpController.php index 8874a9f..cfd81b6 100644 --- a/src/Controller/OgDhcp/AbstractOgDhcpController.php +++ b/src/Controller/OgDhcp/AbstractOgDhcpController.php @@ -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()); + } + } } diff --git a/src/Controller/OgDhcp/Subnet/AddHostAction.php b/src/Controller/OgDhcp/Subnet/AddHostAction.php new file mode 100644 index 0000000..18cc882 --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/AddHostAction.php @@ -0,0 +1,48 @@ + [ + '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); + } +} \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/DeleteAction.php b/src/Controller/OgDhcp/Subnet/DeleteAction.php index 81080b8..462b412 100644 --- a/src/Controller/OgDhcp/Subnet/DeleteAction.php +++ b/src/Controller/OgDhcp/Subnet/DeleteAction.php @@ -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); + } } \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/DeleteHostAction.php b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php new file mode 100644 index 0000000..95a3f1d --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php @@ -0,0 +1,8 @@ +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); } } \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php index d29c5d0..2b2ee50 100644 --- a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php +++ b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php @@ -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); } } \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/GetHostAction.php b/src/Controller/OgDhcp/Subnet/GetHostAction.php new file mode 100644 index 0000000..1211d8a --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/GetHostAction.php @@ -0,0 +1,12 @@ + [ + '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); + } } \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/PutHostAction.php b/src/Controller/OgDhcp/Subnet/PutHostAction.php new file mode 100644 index 0000000..a5f540e --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/PutHostAction.php @@ -0,0 +1,8 @@ +