refs #617. Added single host integration
parent
2cf6730fbb
commit
07b419016e
|
@ -68,6 +68,15 @@ 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
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20240904134540 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE client CHANGE og_live_id subnet_id INT DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455C9CF9478 FOREIGN KEY (subnet_id) REFERENCES subnet (id)');
|
||||
$this->addSql('CREATE INDEX IDX_C7440455C9CF9478 ON client (subnet_id)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE client DROP FOREIGN KEY FK_C7440455C9CF9478');
|
||||
$this->addSql('DROP INDEX IDX_C7440455C9CF9478 ON client');
|
||||
$this->addSql('ALTER TABLE client CHANGE subnet_id og_live_id INT DEFAULT NULL');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|||
#[AsController]
|
||||
class PostHostAction extends AbstractOgDhcpController
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
|
@ -30,9 +31,14 @@ class PostHostAction extends AbstractOgDhcpController
|
|||
{
|
||||
$clients = $input->clients;
|
||||
|
||||
$subnet->setClients($clients);
|
||||
$this->entityManager->persist($subnet);
|
||||
$this->entityManager->flush();
|
||||
|
||||
foreach ($clients as $client) {
|
||||
/** @var Client $clientEntity */
|
||||
$clientEntity = $client->getEntity();
|
||||
|
||||
$data = [
|
||||
'host' => $clientEntity->getName(),
|
||||
'macAddress' => $clientEntity->getMac(),
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?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;
|
||||
}
|
|
@ -63,6 +63,9 @@ class Client extends AbstractEntity
|
|||
#[ORM\ManyToOne(inversedBy: 'clients')]
|
||||
private ?OgRepository $repository = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'clients')]
|
||||
private ?Subnet $subnet = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
@ -254,4 +257,16 @@ class Client extends AbstractEntity
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSubnet(): ?Subnet
|
||||
{
|
||||
return $this->subnet;
|
||||
}
|
||||
|
||||
public function setSubnet(?Subnet $subnet): static
|
||||
{
|
||||
$this->subnet = $subnet;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,10 +30,17 @@ class Subnet extends AbstractEntity
|
|||
#[ORM\OneToMany(mappedBy: 'subnet', targetEntity: OrganizationalUnit::class)]
|
||||
private Collection $organizationalUnits;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Client>
|
||||
*/
|
||||
#[ORM\OneToMany(mappedBy: 'subnet', targetEntity: Client::class)]
|
||||
private Collection $clients;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->organizationalUnits = new ArrayCollection();
|
||||
$this->clients = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getNetmask(): ?string
|
||||
|
@ -124,4 +131,45 @@ class Subnet extends AbstractEntity
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Client>
|
||||
*/
|
||||
public function getClients(): Collection
|
||||
{
|
||||
return $this->clients;
|
||||
}
|
||||
|
||||
public function setClients(array $clients): static
|
||||
{
|
||||
$this->clients->clear();
|
||||
|
||||
foreach ($clients as $client){
|
||||
$this->addClient($client);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addClient(Client $client): static
|
||||
{
|
||||
if (!$this->clients->contains($client)) {
|
||||
$this->clients->add($client);
|
||||
$client->setSubnet($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeClient(Client $client): static
|
||||
{
|
||||
if ($this->clients->removeElement($client)) {
|
||||
// set the owning side to null (unless already changed)
|
||||
if ($client->getSubnet() === $this) {
|
||||
$client->setSubnet(null);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue