From 07b419016e96c7414a848a443452716cc6fc4cb1 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Wed, 4 Sep 2024 16:11:58 +0200 Subject: [PATCH] refs #617. Added single host integration --- config/api_platform/Subnet.yaml | 9 ++++ migrations/Version20240904134540.php | 35 +++++++++++++ .../OgDhcp/Subnet/AddSingleHostAction.php | 52 +++++++++++++++++++ .../OgDhcp/Subnet/PostHostAction.php | 6 +++ src/Dto/Input/SubnetAddSingleHostInput.php | 15 ++++++ src/Entity/Client.php | 15 ++++++ src/Entity/Subnet.php | 48 +++++++++++++++++ 7 files changed, 180 insertions(+) create mode 100644 migrations/Version20240904134540.php create mode 100644 src/Controller/OgDhcp/Subnet/AddSingleHostAction.php create mode 100644 src/Dto/Input/SubnetAddSingleHostInput.php diff --git a/config/api_platform/Subnet.yaml b/config/api_platform/Subnet.yaml index 1755518..2222cda 100644 --- a/config/api_platform/Subnet.yaml +++ b/config/api_platform/Subnet.yaml @@ -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 diff --git a/migrations/Version20240904134540.php b/migrations/Version20240904134540.php new file mode 100644 index 0000000..df9dd35 --- /dev/null +++ b/migrations/Version20240904134540.php @@ -0,0 +1,35 @@ +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'); + } +} diff --git a/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php b/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php new file mode 100644 index 0000000..dcaf665 --- /dev/null +++ b/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php @@ -0,0 +1,52 @@ +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); + } +} \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/PostHostAction.php b/src/Controller/OgDhcp/Subnet/PostHostAction.php index d1c15c9..319025e 100644 --- a/src/Controller/OgDhcp/Subnet/PostHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostHostAction.php @@ -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(), diff --git a/src/Dto/Input/SubnetAddSingleHostInput.php b/src/Dto/Input/SubnetAddSingleHostInput.php new file mode 100644 index 0000000..855261b --- /dev/null +++ b/src/Dto/Input/SubnetAddSingleHostInput.php @@ -0,0 +1,15 @@ +subnet; + } + + public function setSubnet(?Subnet $subnet): static + { + $this->subnet = $subnet; + + return $this; + } } diff --git a/src/Entity/Subnet.php b/src/Entity/Subnet.php index 6fc7c15..719de65 100644 --- a/src/Entity/Subnet.php +++ b/src/Entity/Subnet.php @@ -30,10 +30,17 @@ class Subnet extends AbstractEntity #[ORM\OneToMany(mappedBy: 'subnet', targetEntity: OrganizationalUnit::class)] private Collection $organizationalUnits; + /** + * @var Collection + */ + #[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 + */ + 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; + } }