diff --git a/config/api_platform/Subnet.yaml b/config/api_platform/Subnet.yaml index 3f19ae8..6f384de 100644 --- a/config/api_platform/Subnet.yaml +++ b/config/api_platform/Subnet.yaml @@ -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: diff --git a/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php b/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php deleted file mode 100644 index dcaf665..0000000 --- a/src/Controller/OgDhcp/Subnet/AddSingleHostAction.php +++ /dev/null @@ -1,52 +0,0 @@ -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/AddSingleOrganizationalUnitAction.php b/src/Controller/OgDhcp/Subnet/AddSingleOrganizationalUnitAction.php deleted file mode 100644 index ced5db2..0000000 --- a/src/Controller/OgDhcp/Subnet/AddSingleOrganizationalUnitAction.php +++ /dev/null @@ -1,54 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/src/Controller/OgDhcp/Subnet/GetAction.php b/src/Controller/OgDhcp/Subnet/GetAction.php index c9b5133..b17698c 100644 --- a/src/Controller/OgDhcp/Subnet/GetAction.php +++ b/src/Controller/OgDhcp/Subnet/GetAction.php @@ -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); } diff --git a/src/Controller/OgDhcp/Subnet/PostHostAction.php b/src/Controller/OgDhcp/Subnet/PostHostAction.php index 319025e..1b900e8 100644 --- a/src/Controller/OgDhcp/Subnet/PostHostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostHostAction.php @@ -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); } } \ No newline at end of file diff --git a/src/Dto/Input/SubnetAddHostInput.php b/src/Dto/Input/SubnetAddHostInput.php index bc5015e..0129a86 100644 --- a/src/Dto/Input/SubnetAddHostInput.php +++ b/src/Dto/Input/SubnetAddHostInput.php @@ -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; } \ No newline at end of file diff --git a/src/Dto/Input/SubnetAddSingleHostInput.php b/src/Dto/Input/SubnetAddSingleHostInput.php deleted file mode 100644 index 5075161..0000000 --- a/src/Dto/Input/SubnetAddSingleHostInput.php +++ /dev/null @@ -1,15 +0,0 @@ -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();