refs #614. Integration DHCP
testing/ogcore-api/pipeline/head This commit looks good Details

develop-jenkins
Manuel Aranda Rosales 2024-10-14 06:43:34 +02:00
parent 401898b262
commit 8c57e61a57
9 changed files with 28 additions and 170 deletions

View File

@ -68,15 +68,6 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/delete
controller: App\Controller\OgDhcp\Subnet\DeleteAction
add_single_host:
shortName: Subnet Server Hosts
description: Add Single Host to Subnet
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\SubnetAddSingleHostInput
uriTemplate: /og-dhcp/server/{uuid}/add-single-host
controller: App\Controller\OgDhcp\Subnet\AddSingleHostAction
post_host:
shortName: Subnet Server Hosts
description: Post Host to Subnet
@ -113,14 +104,6 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/delete-host
controller: App\Controller\OgDhcp\Subnet\DeleteHostAction
add_single_organizational_unit:
shortName: Subnet Server Organizational Units
description: Add Single Organizational Unit to Subnet
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\SubnetAddSingleOrganizationalUnitInput
uriTemplate: /og-dhcp/server/{uuid}/add-single-organizational-unit
controller: App\Controller\OgDhcp\Subnet\AddSingleOrganizationalUnitAction
properties:
App\Entity\Subnet:

View File

@ -1,52 +0,0 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\SubnetAddSingleHostInput;
use App\Entity\Client;
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 AddSingleHostAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(SubnetAddSingleHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse
{
$client = $input->client;
/** @var Client $clientEntity */
$clientEntity = $client->getEntity();
$params = [
'json' => [
'host' => $clientEntity->getName(),
'macAddress' => $clientEntity->getMac(),
'address' => $clientEntity->getIp(),
]
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
if ($content->getStatusCode() === 200) {
$subnet->addClient($clientEntity);
$this->entityManager->persist($subnet);
$this->entityManager->flush();
}
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -1,54 +0,0 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\SubnetAddSingleHostInput;
use App\Dto\Input\SubnetAddSingleOrganizationalUnitInput;
use App\Entity\Client;
use App\Entity\OrganizationalUnit;
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 AddSingleOrganizationalUnitAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(SubnetAddSingleOrganizationalUnitInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse
{
$ou = $input->organizationalUnitOutput;
/** @var OrganizationalUnit $organizationalUnitEntity */
$organizationalUnitEntity = $ou->getEntity();
$params = [
'json' => [
'name' => $organizationalUnitEntity->getId(),
'nextServer' => $organizationalUnitEntity->getNetworkSettings()?->getNextServer(),
'bootFileName' => $organizationalUnitEntity->getNetworkSettings()?->getBootFileName(),
]
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/classroom', $params);
if ($content->getStatusCode() === 200) {
$subnet->addOrganizationalUnit($organizationalUnitEntity);
$this->entityManager->persist($subnet);
$this->entityManager->flush();
}
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

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

View File

@ -29,29 +29,27 @@ class PostHostAction extends AbstractOgDhcpController
*/
public function __invoke(SubnetAddHostInput $input, Subnet $subnet, HttpClientInterface $httpClient): JsonResponse
{
$clients = $input->clients;
$client = $input->client;
$subnet->setClients($clients);
/** @var Client $clientEntity */
$clientEntity = $client->getEntity();
$data = [
'host' => $clientEntity->getName(),
'macAddress' => strtolower($clientEntity->getMac()),
'address' => $clientEntity->getIp(),
];
$params = [
'json' => $data
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
$subnet->addClient($clientEntity);
$this->entityManager->persist($subnet);
$this->entityManager->flush();
foreach ($clients as $client) {
/** @var Client $clientEntity */
$clientEntity = $client->getEntity();
$data = [
'host' => $clientEntity->getName(),
'macAddress' => $clientEntity->getMac(),
'address' => $clientEntity->getIp(),
];
$params = [
'json' => $data
];
$content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params);
}
return new JsonResponse(status: Response::HTTP_OK);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -12,10 +12,7 @@ use Symfony\Component\Validator\Constraints as Assert;
final class SubnetAddHostInput
{
/**
* @var ClientOutput[]
*/
#[Assert\NotNull]
#[Groups(['subnet:write'])]
#[ApiProperty(description: 'The clients of the subnet', readableLink: false, writableLink: false, example: "Client 1")]
public array $clients = [];
public ?ClientOutput $client = null;
}

View File

@ -1,15 +0,0 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class SubnetAddSingleHostInput
{
#[Assert\NotNull]
#[Groups(['subnet:write'])]
public ?ClientOutput $client = null;
}

View File

@ -13,16 +13,16 @@ final class ClientOutput extends AbstractOutput
{
CONST string TYPE = 'client';
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read', 'trace:read'])]
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read', 'trace:read', 'subnet:read'])]
public string $name;
#[Groups(['client:read', 'organizational-unit:read'])]
public string $type = self::TYPE;
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read', 'trace:read'])]
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read', 'trace:read', 'subnet:read'])]
public ?string $ip = '';
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read'])]
#[Groups(['client:read', 'organizational-unit:read', 'pxe-boot-file:read', 'subnet:read'])]
public ?string $mac = '';
#[Groups(['client:read', 'organizational-unit:read', 'trace:read'])]

View File

@ -4,6 +4,7 @@ namespace App\Dto\Output;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Client;
use App\Entity\OrganizationalUnit;
use App\Entity\Subnet;
use Symfony\Component\Serializer\Annotation\Groups;
@ -27,7 +28,7 @@ final class SubnetOutput extends AbstractOutput
public string $bootFileName;
#[Groups(['subnet:read'])]
public array $organizationalUnits;
public array $clients;
#[Groups(['subnet:read'])]
public \DateTime $createdAt;
@ -46,8 +47,8 @@ final class SubnetOutput extends AbstractOutput
$this->bootFileName = $subnet->getBootFileName();
$this->organizationalUnits = $subnet->getOrganizationalUnits()->map(
fn(OrganizationalUnit $organizationalUnit) => new OrganizationalUnitOutput($organizationalUnit)
$this->clients = $subnet->getClients()->map(
fn(Client $client) => new ClientOutput($client)
)->toArray();
$this->createdAt = $subnet->getCreatedAt();