From 6d1a2a89ea7526d547c92a6fe9f050cf96cc2f9c Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Tue, 27 Aug 2024 15:13:56 +0200 Subject: [PATCH] refs #614. Itnegration ogDhcp. New endpoints --- config/api_platform/Subnet.yaml | 29 +++++++----- migrations/Version20240827102833.php | 31 +++++++++++++ .../OgDhcp/Subnet/GetCollectionAction.php | 5 ++- src/Controller/OgDhcp/Subnet/PutAction.php | 44 ++++++++++++++++++- src/Dto/Input/SubnetInput.php | 7 +++ src/Dto/Output/SubnetOutput.php | 4 ++ src/Entity/Subnet.php | 2 + 7 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 migrations/Version20240827102833.php diff --git a/config/api_platform/Subnet.yaml b/config/api_platform/Subnet.yaml index b240585..5dba0ef 100644 --- a/config/api_platform/Subnet.yaml +++ b/config/api_platform/Subnet.yaml @@ -29,7 +29,7 @@ resources: class: ApiPlatform\Metadata\GetCollection method: GET input: false - uriTemplate: /subnets/server/get-collection + uriTemplate: /og-dhcp/server/get-collection controller: App\Controller\OgDhcp\Subnet\GetCollectionAction get: @@ -38,7 +38,7 @@ resources: class: ApiPlatform\Metadata\Get method: GET input: false - uriTemplate: /subnets/server/{uuid}/get + uriTemplate: /og-dhcp/server/{uuid}/get controller: App\Controller\OgDhcp\Subnet\GetAction post: @@ -47,16 +47,25 @@ resources: class: ApiPlatform\Metadata\Post method: POST input: false - uriTemplate: /subnets/server/{uuid}/post + uriTemplate: /og-dhcp/server/{uuid}/post controller: App\Controller\OgDhcp\Subnet\PostAction + put: + shortName: Subnet Server + description: Create Subnet + class: ApiPlatform\Metadata\Put + method: PUT + input: false + uriTemplate: /og-dhcp/server/{uuid}/put + controller: App\Controller\OgDhcp\Subnet\PutAction + delete: shortName: Subnet Server description: Delete Subnet - class: ApiPlatform\Metadata\Get - method: GET + class: ApiPlatform\Metadata\Delete + method: DELETE input: false - uriTemplate: /subnets/server/{uuid}/delete + uriTemplate: /og-dhcp/server/{uuid}/delete controller: App\Controller\OgDhcp\Subnet\DeleteAction add_host: @@ -65,7 +74,7 @@ resources: class: ApiPlatform\Metadata\Post method: POST input: false - uriTemplate: /subnets/server/{uuid}/add-host + uriTemplate: /og-dhcp/server/{uuid}/add-host controller: App\Controller\OgDhcp\Subnet\AddHostAction get_hosts: @@ -74,7 +83,7 @@ resources: class: ApiPlatform\Metadata\GetCollection method: GET input: false - uriTemplate: /subnets/server/{uuid}/get-hosts + uriTemplate: /og-dhcp/server/{uuid}/get-hosts controller: App\Controller\OgDhcp\Subnet\GetHostsAction put_host: @@ -83,7 +92,7 @@ resources: class: ApiPlatform\Metadata\Put method: PUT input: false - uriTemplate: /subnets/server/{uuid}/put-host + uriTemplate: /og-dhcp/server/{uuid}/put-host controller: App\Controller\OgDhcp\Subnet\PutHostAction delete_host: @@ -92,7 +101,7 @@ resources: class: ApiPlatform\Metadata\Delete method: DELETE input: false - uriTemplate: /subnets/server/{uuid}/delete-host + uriTemplate: /og-dhcp/server/{uuid}/delete-host controller: App\Controller\OgDhcp\Subnet\DeleteHostAction properties: diff --git a/migrations/Version20240827102833.php b/migrations/Version20240827102833.php new file mode 100644 index 0000000..00ab457 --- /dev/null +++ b/migrations/Version20240827102833.php @@ -0,0 +1,31 @@ +addSql('CREATE TABLE subnet (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, netmask VARCHAR(255) NOT NULL, ip_address VARCHAR(255) NOT NULL, next_server VARCHAR(255) NOT NULL, boot_file_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_91C24216D17F50A6 (uuid), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE subnet'); + } +} diff --git a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php index 2b2ee50..9f9324c 100644 --- a/src/Controller/OgDhcp/Subnet/GetCollectionAction.php +++ b/src/Controller/OgDhcp/Subnet/GetCollectionAction.php @@ -23,8 +23,9 @@ class GetCollectionAction extends AbstractOgDhcpController */ public function __invoke(HttpClientInterface $httpClient): JsonResponse { - $content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl . '/opengnsys3/rest/subnets'); + $content = $this->createRequest($httpClient, 'GET', $this->ogDhcpApiUrl . '/ogdhcp/v1/subnets'); return new JsonResponse(data: $content, status: Response::HTTP_OK); } -} \ No newline at end of file +} + diff --git a/src/Controller/OgDhcp/Subnet/PutAction.php b/src/Controller/OgDhcp/Subnet/PutAction.php index 8e187a0..252343d 100644 --- a/src/Controller/OgDhcp/Subnet/PutAction.php +++ b/src/Controller/OgDhcp/Subnet/PutAction.php @@ -2,7 +2,47 @@ namespace App\Controller\OgDhcp\Subnet; -class PutAction -{ +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 PutAction extends AbstractOgDhcpController +{ + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ + public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse + { + if (null === $data->getId()) { + throw new ValidatorException('Id is required'); + } + + $params = [ + 'json' => [ + 'mask' => $data->getNetmask(), + 'address' => $data->getIpAddress(), + 'nextServer' => $data->getNextServer(), + 'bootFileName' => $data->getBootFileName(), + ] + ]; + + $content = $this->createRequest($httpClient, 'POST', $this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getId(), $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/Dto/Input/SubnetInput.php b/src/Dto/Input/SubnetInput.php index b7c233d..d536042 100644 --- a/src/Dto/Input/SubnetInput.php +++ b/src/Dto/Input/SubnetInput.php @@ -9,6 +9,11 @@ use Symfony\Component\Validator\Constraints as Assert; final class SubnetInput { + #[Assert\NotBlank(message: 'validators.subnet.name.not_blank')] + #[Groups(['subnet:write'])] + #[ApiProperty(description: 'The name of the subnet', example: "Subnet 1")] + public ?string $name = null; + #[Assert\NotBlank(message: 'validators.subnet.netmask.not_blank')] #[Groups(['subnet:write'])] #[ApiProperty(description: 'The netmask of the subnet', example: "")] @@ -35,6 +40,7 @@ final class SubnetInput return; } + $this->name = $subnet->getName(); $this->netmask = $subnet->getNetmask(); $this->ipAddress = $subnet->getIpAddress(); $this->nextServer = $subnet->getNextServer(); @@ -47,6 +53,7 @@ final class SubnetInput $subnet = new Subnet(); } + $subnet->setName($this->name); $subnet->setNetmask($this->netmask); $subnet->setIpAddress($this->ipAddress); $subnet->setNextServer($this->nextServer); diff --git a/src/Dto/Output/SubnetOutput.php b/src/Dto/Output/SubnetOutput.php index 567ff2f..92cf65d 100644 --- a/src/Dto/Output/SubnetOutput.php +++ b/src/Dto/Output/SubnetOutput.php @@ -10,6 +10,9 @@ use Symfony\Component\Serializer\Annotation\Groups; #[Get(shortName: 'Subnet')] final class SubnetOutput extends AbstractOutput { + #[Groups(['subnet:read'])] + public string $name; + #[Groups(['subnet:read'])] public string $netmask; @@ -32,6 +35,7 @@ final class SubnetOutput extends AbstractOutput { parent::__construct($subnet); + $this->name = $subnet->getName(); $this->netmask = $subnet->getNetmask(); $this->ipAddress = $subnet->getIpAddress(); $this->nextServer = $subnet->getNextServer(); diff --git a/src/Entity/Subnet.php b/src/Entity/Subnet.php index 29201dd..5693555 100644 --- a/src/Entity/Subnet.php +++ b/src/Entity/Subnet.php @@ -8,6 +8,8 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: SubnetRepository::class)] class Subnet extends AbstractEntity { + use NameableTrait; + #[ORM\Column(length: 255)] private ?string $netmask = null;