refs #614. Itnegration ogDhcp. Change some params

feature/integration-dhcp
Manuel Aranda Rosales 2024-08-27 16:03:22 +02:00
parent 6d1a2a89ea
commit a232d5cbd7
9 changed files with 140 additions and 40 deletions

View File

@ -23,7 +23,7 @@ resources:
ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~ ApiPlatform\Metadata\Delete: ~
get_collection: get_collection_templates:
shortName: PxeTemplate Server shortName: PxeTemplate Server
description: Get collection of PxeTemplate description: Get collection of PxeTemplate
class: ApiPlatform\Metadata\GetCollection class: ApiPlatform\Metadata\GetCollection
@ -32,7 +32,7 @@ resources:
uriTemplate: /pxe-templates/server/get-collection uriTemplate: /pxe-templates/server/get-collection
controller: App\Controller\OgBoot\PxeTemplate\GetCollectionAction controller: App\Controller\OgBoot\PxeTemplate\GetCollectionAction
get: get_template:
shortName: PxeTemplate Server shortName: PxeTemplate Server
description: Get PxeTemplate description: Get PxeTemplate
class: ApiPlatform\Metadata\Get class: ApiPlatform\Metadata\Get
@ -41,7 +41,7 @@ resources:
uriTemplate: /pxe-templates/server/{uuid}/get uriTemplate: /pxe-templates/server/{uuid}/get
controller: App\Controller\OgBoot\PxeTemplate\GetAction controller: App\Controller\OgBoot\PxeTemplate\GetAction
post: post_template:
shortName: PxeTemplate Server shortName: PxeTemplate Server
description: Create PxeTemplate description: Create PxeTemplate
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
@ -50,7 +50,7 @@ resources:
uriTemplate: /pxe-templates/server/{uuid}/post uriTemplate: /pxe-templates/server/{uuid}/post
controller: App\Controller\OgBoot\PxeTemplate\PostAction controller: App\Controller\OgBoot\PxeTemplate\PostAction
delete: delete_template:
shortName: PxeTemplate Server shortName: PxeTemplate Server
description: Delete PxeTemplate description: Delete PxeTemplate
class: ApiPlatform\Metadata\Get class: ApiPlatform\Metadata\Get

View File

@ -23,7 +23,7 @@ resources:
ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~ ApiPlatform\Metadata\Delete: ~
get_collection: get_collection_subnets:
shortName: Subnet Server shortName: Subnet Server
description: Get collection of Subnet description: Get collection of Subnet
class: ApiPlatform\Metadata\GetCollection class: ApiPlatform\Metadata\GetCollection
@ -32,7 +32,7 @@ resources:
uriTemplate: /og-dhcp/server/get-collection uriTemplate: /og-dhcp/server/get-collection
controller: App\Controller\OgDhcp\Subnet\GetCollectionAction controller: App\Controller\OgDhcp\Subnet\GetCollectionAction
get: get_subnet:
shortName: Subnet Server shortName: Subnet Server
description: Get Subnet description: Get Subnet
class: ApiPlatform\Metadata\Get class: ApiPlatform\Metadata\Get
@ -41,7 +41,7 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/get uriTemplate: /og-dhcp/server/{uuid}/get
controller: App\Controller\OgDhcp\Subnet\GetAction controller: App\Controller\OgDhcp\Subnet\GetAction
post: post_subnet:
shortName: Subnet Server shortName: Subnet Server
description: Create Subnet description: Create Subnet
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
@ -50,7 +50,7 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/post uriTemplate: /og-dhcp/server/{uuid}/post
controller: App\Controller\OgDhcp\Subnet\PostAction controller: App\Controller\OgDhcp\Subnet\PostAction
put: put_subnet:
shortName: Subnet Server shortName: Subnet Server
description: Create Subnet description: Create Subnet
class: ApiPlatform\Metadata\Put class: ApiPlatform\Metadata\Put
@ -59,7 +59,7 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/put uriTemplate: /og-dhcp/server/{uuid}/put
controller: App\Controller\OgDhcp\Subnet\PutAction controller: App\Controller\OgDhcp\Subnet\PutAction
delete: delete_subnet:
shortName: Subnet Server shortName: Subnet Server
description: Delete Subnet description: Delete Subnet
class: ApiPlatform\Metadata\Delete class: ApiPlatform\Metadata\Delete
@ -68,14 +68,14 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/delete uriTemplate: /og-dhcp/server/{uuid}/delete
controller: App\Controller\OgDhcp\Subnet\DeleteAction controller: App\Controller\OgDhcp\Subnet\DeleteAction
add_host: post_host:
shortName: Subnet Server Hosts shortName: Subnet Server Hosts
description: Add Host to Subnet description: Post Host to Subnet
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
method: POST method: POST
input: false input: App\Dto\Input\SubnetAddHostInput
uriTemplate: /og-dhcp/server/{uuid}/add-host uriTemplate: /og-dhcp/server/{uuid}/post-host
controller: App\Controller\OgDhcp\Subnet\AddHostAction controller: App\Controller\OgDhcp\Subnet\PostHostAction
get_hosts: get_hosts:
shortName: Subnet Server Hosts shortName: Subnet Server Hosts

View File

@ -25,11 +25,11 @@ class DeleteAction extends AbstractOgDhcpController
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{ {
if (!$data->getChecksum()) { if (!$data->getId()) {
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Data Id is required');
} }
$content = $this->createRequest($httpClient, 'DELETE', $this->ogDhcpApiUrl.'/opengnsys3/rest/dhcp/subnets/'.$data->getChecksum()); $content = $this->createRequest($httpClient, 'DELETE', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getId());
$this->entityManager->remove($data); $this->entityManager->remove($data);
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -3,9 +3,43 @@
namespace App\Controller\OgDhcp\Subnet; namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController; 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\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] #[AsController]
class DeleteHostAction extends AbstractOgDhcpController{ class DeleteHostAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse
{
if (!$data->getId()) {
throw new ValidatorException('Data URL is required');
}
$params = [
'json' => [
'host' => '',
]
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getId().'/hosts', $params);
$this->entityManager->persist($data);
$this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
} }

View File

@ -29,7 +29,7 @@ class GetHostsAction extends AbstractOgDhcpController
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Checksum is required');
} }
$content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getId()); $content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getId().'/hosts');
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -35,7 +35,7 @@ class PostAction extends AbstractOgDhcpController
] ]
]; ];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets', $params); $content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets' , $params);
$this->entityManager->persist($data); $this->entityManager->persist($data);
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -3,6 +3,8 @@
namespace App\Controller\OgDhcp\Subnet; namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController; use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\SubnetAddHostInput;
use App\Entity\Client;
use App\Entity\Subnet; use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -15,7 +17,7 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController] #[AsController]
class AddHostAction extends AbstractOgDhcpController class PostHostAction extends AbstractOgDhcpController
{ {
/** /**
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
@ -23,26 +25,26 @@ class AddHostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse public function __invoke(SubnetAddHostInput $input, HttpClientInterface $httpClient): JsonResponse
{ {
if (!$data) { $subnet = $input->subnet->getEntity();
throw new ValidatorException('Data URL is required'); $clients = $input->clients;
/** @var Client $client */
foreach ($clients as $client) {
$data = [
'host' => $client->getName(),
'macAddress' => $client->getMac(),
'address' => $client->getIp(),
];
$params = [
'json' => $data
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
} }
$params = [ return new JsonResponse(status: Response::HTTP_OK);
'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,9 +3,47 @@
namespace App\Controller\OgDhcp\Subnet; namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController; use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\SubnetAddHostInput;
use App\Entity\Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; 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] #[AsController]
class PutHostAction extends AbstractOgDhcpController{ class PutHostAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(SubnetAddHostInput $input, HttpClientInterface $httpClient): JsonResponse
{
$subnet = $input->subnet->getEntity();
$clients = $input->clients;
/** @var Client $client */
foreach ($clients as $client) {
$data = [
'host' => $client->getName(),
'oldMacAddress' => '',
'macAddress' => '',
'address' => '',
];
$params = [
'json' => $data
];
$content = $this->createRequest($httpClient, 'PUT', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
}
return new JsonResponse(status: Response::HTTP_OK);
}
} }

View File

@ -0,0 +1,26 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\PxeTemplateOutput;
use App\Dto\Output\SubnetOutput;
use App\Entity\PxeBootFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class SubnetAddHostInput
{
#[Assert\NotNull(message: 'validators.subnet_add_host.subnet.not_null')]
#[Groups(['subnet:write'])]
#[ApiProperty(description: 'The subnet of the pxeBootFile', example: "Subnet 1")]
public ?SubnetOutput $subnet = null;
/**
* @var ClientOutput[]
*/
#[Groups(['subnet:write'])]
#[ApiProperty(description: 'The clients of the subnet', readableLink: false, writableLink: false, example: "Client 1")]
public array $clients = [];
}