From 2cf6730fbb06a5c89cda4716c3ea5d7f11b84e10 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Tue, 3 Sep 2024 10:37:41 +0200 Subject: [PATCH] refs #617. DHCP new changes in API --- migrations/Version20240902124157.php | 33 ++++++++++++++ migrations/Version20240903081001.php | 35 +++++++++++++++ src/Dto/Input/NetworkSettingsInput.php | 10 +++++ src/Dto/Input/SubnetInput.php | 20 +++++++++ src/Dto/Output/NetworkSettingsOutput.php | 8 ++++ src/Dto/Output/SubnetOutput.php | 10 +++++ src/Entity/NetworkSettings.php | 27 +++++++++++- src/Entity/OrganizationalUnit.php | 15 +++++++ src/Entity/Subnet.php | 55 ++++++++++++++++++++++++ 9 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 migrations/Version20240902124157.php create mode 100644 migrations/Version20240903081001.php diff --git a/migrations/Version20240902124157.php b/migrations/Version20240902124157.php new file mode 100644 index 0000000..5c320b9 --- /dev/null +++ b/migrations/Version20240902124157.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE network_settings ADD next_server VARCHAR(255) DEFAULT NULL, ADD boot_file_name VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE network_settings ADD og_live_id INT DEFAULT NULL, DROP next_server, DROP boot_file_name'); + $this->addSql('ALTER TABLE network_settings ADD CONSTRAINT FK_48869B54F7E54CF3 FOREIGN KEY (og_live_id) REFERENCES og_live (id)'); + + } +} diff --git a/migrations/Version20240903081001.php b/migrations/Version20240903081001.php new file mode 100644 index 0000000..9202867 --- /dev/null +++ b/migrations/Version20240903081001.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE organizational_unit ADD subnet_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE organizational_unit ADD CONSTRAINT FK_749AEB2DC9CF9478 FOREIGN KEY (subnet_id) REFERENCES subnet (id)'); + $this->addSql('CREATE INDEX IDX_749AEB2DC9CF9478 ON organizational_unit (subnet_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE organizational_unit DROP FOREIGN KEY FK_749AEB2DC9CF9478'); + $this->addSql('DROP INDEX IDX_749AEB2DC9CF9478 ON organizational_unit'); + $this->addSql('ALTER TABLE organizational_unit DROP subnet_id'); + } +} diff --git a/src/Dto/Input/NetworkSettingsInput.php b/src/Dto/Input/NetworkSettingsInput.php index e32c732..039e6f9 100644 --- a/src/Dto/Input/NetworkSettingsInput.php +++ b/src/Dto/Input/NetworkSettingsInput.php @@ -15,6 +15,12 @@ use Symfony\Component\Validator\Constraints as Assert; class NetworkSettingsInput { + #[Groups(['organizational-unit:write'])] + public ?string $nextServer = null; + + #[Groups(['organizational-unit:write'])] + public ?string $bootFileName = null; + #[Groups(['organizational-unit:write'])] public ?string $proxy = null; @@ -71,6 +77,8 @@ class NetworkSettingsInput return; } + $this->nextServer = $networkSettings->getNextServer(); + $this->bootFileName = $networkSettings->getBootFileName(); $this->proxy = $networkSettings->getProxy(); $this->dns = $networkSettings->getDns(); $this->netmask = $networkSettings->getNetmask(); @@ -100,6 +108,8 @@ class NetworkSettingsInput $networkSettings = new NetworkSettings(); } + $networkSettings->setNextServer($this->nextServer); + $networkSettings->setBootFileName($this->bootFileName); $networkSettings->setProxy($this->proxy); $networkSettings->setDns($this->dns); $networkSettings->setNetmask($this->netmask); diff --git a/src/Dto/Input/SubnetInput.php b/src/Dto/Input/SubnetInput.php index d536042..98fed68 100644 --- a/src/Dto/Input/SubnetInput.php +++ b/src/Dto/Input/SubnetInput.php @@ -3,6 +3,8 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; +use App\Dto\Output\OrganizationalUnitOutput; +use App\Dto\Output\SubnetOutput; use App\Entity\Subnet; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; @@ -34,6 +36,13 @@ final class SubnetInput #[ApiProperty(description: 'The boot file name of the subnet', example: "")] public ?string $bootFileName = null; + /** + * @var OrganizationalUnitOutput[] + */ + #[Groups('user:write')] + #[ApiProperty(readableLink: false, writableLink: false)] + public array $organizationalUnits = []; + public function __construct(?Subnet $subnet = null) { if (!$subnet) { @@ -45,6 +54,12 @@ final class SubnetInput $this->ipAddress = $subnet->getIpAddress(); $this->nextServer = $subnet->getNextServer(); $this->bootFileName = $subnet->getBootFileName(); + + if ($subnet->getOrganizationalUnits()) { + foreach ($subnet->getOrganizationalUnits() as $organizationalUnit) { + $this->organizationalUnits[] = new OrganizationalUnitOutput($organizationalUnit); + } + } } public function createOrUpdateEntity(?Subnet $subnet = null): Subnet @@ -59,6 +74,11 @@ final class SubnetInput $subnet->setNextServer($this->nextServer); $subnet->setBootFileName($this->bootFileName); + foreach ($this->organizationalUnits as $organizationalUnit) { + $organizationalUnitToAdd[] = $organizationalUnit->getEntity(); + } + $subnet->setOrganizationalUnits($organizationalUnitToAdd ?? [] ); + return $subnet; } } \ No newline at end of file diff --git a/src/Dto/Output/NetworkSettingsOutput.php b/src/Dto/Output/NetworkSettingsOutput.php index 9c0b66d..d258489 100644 --- a/src/Dto/Output/NetworkSettingsOutput.php +++ b/src/Dto/Output/NetworkSettingsOutput.php @@ -9,6 +9,12 @@ use Symfony\Component\Serializer\Annotation\Groups; #[Get(shortName: 'NetworkSettings')] final class NetworkSettingsOutput extends AbstractOutput { + #[Groups(['network-settings:read', "organizational-unit:read", "client:read"])] + public ?string $nextServer = null; + + #[Groups(['network-settings:read', "organizational-unit:read", "client:read"])] + public ?string $bootFileName = null; + #[Groups(['network-settings:read', "organizational-unit:read", "client:read"])] public ?string $proxy = null; @@ -61,6 +67,8 @@ final class NetworkSettingsOutput extends AbstractOutput { parent::__construct($networkSettings); + $this->nextServer = $networkSettings->getNextServer(); + $this->bootFileName = $networkSettings->getBootFileName(); $this->proxy = $networkSettings->getProxy(); $this->dns = $networkSettings->getDns(); $this->netmask = $networkSettings->getNetmask(); diff --git a/src/Dto/Output/SubnetOutput.php b/src/Dto/Output/SubnetOutput.php index 92cf65d..9366f42 100644 --- a/src/Dto/Output/SubnetOutput.php +++ b/src/Dto/Output/SubnetOutput.php @@ -4,6 +4,7 @@ namespace App\Dto\Output; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\Get; +use App\Entity\OrganizationalUnit; use App\Entity\Subnet; use Symfony\Component\Serializer\Annotation\Groups; @@ -25,6 +26,9 @@ final class SubnetOutput extends AbstractOutput #[Groups(['subnet:read'])] public string $bootFileName; + #[Groups(['subnet:read'])] + public array $organizationalUnits; + #[Groups(['subnet:read'])] public \DateTime $createdAt; @@ -40,6 +44,12 @@ final class SubnetOutput extends AbstractOutput $this->ipAddress = $subnet->getIpAddress(); $this->nextServer = $subnet->getNextServer(); $this->bootFileName = $subnet->getBootFileName(); + + + $this->organizationalUnits = $subnet->getOrganizationalUnits()->map( + fn(OrganizationalUnit $organizationalUnit) => new OrganizationalUnitOutput($organizationalUnit) + )->toArray(); + $this->createdAt = $subnet->getCreatedAt(); $this->createdBy = $subnet->getCreatedBy(); } diff --git a/src/Entity/NetworkSettings.php b/src/Entity/NetworkSettings.php index de3b9c9..77df412 100644 --- a/src/Entity/NetworkSettings.php +++ b/src/Entity/NetworkSettings.php @@ -10,6 +10,12 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: NetworkSettingsRepository::class)] class NetworkSettings extends AbstractEntity { + #[ORM\Column(length: 255, nullable: true)] + private ?string $nextServer = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $bootFileName = null; + #[ORM\Column(length: 255, nullable: true)] private ?string $proxy = null; @@ -69,9 +75,26 @@ class NetworkSettings extends AbstractEntity $this->organizationalUnits = new ArrayCollection(); } - public function getId(): ?int + public function getNextServer(): ?string { - return $this->id; + return $this->nextServer; + } + + public function setNextServer(?string $nextServer): void + { + $this->nextServer = $nextServer; + } + + public function getBootFileName(): ?string + { + return $this->bootFileName; + } + + public function setBootFileName(?string $bootFileName): static + { + $this->bootFileName = $bootFileName; + + return $this; } public function getProxy(): ?string diff --git a/src/Entity/OrganizationalUnit.php b/src/Entity/OrganizationalUnit.php index 469a9f5..29bdd20 100644 --- a/src/Entity/OrganizationalUnit.php +++ b/src/Entity/OrganizationalUnit.php @@ -84,6 +84,9 @@ class OrganizationalUnit extends AbstractEntity #[ORM\OneToMany(mappedBy: 'organizationalUnit', targetEntity: SoftwareProfile::class)] private Collection $softwareProfiles; + #[ORM\ManyToOne(inversedBy: 'organizationalUnits')] + private ?Subnet $subnet = null; + public function __construct() { parent::__construct(); @@ -368,4 +371,16 @@ class OrganizationalUnit extends AbstractEntity return $this; } + + public function getSubnet(): ?Subnet + { + return $this->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 5693555..6fc7c15 100644 --- a/src/Entity/Subnet.php +++ b/src/Entity/Subnet.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\SubnetRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: SubnetRepository::class)] @@ -22,6 +24,18 @@ class Subnet extends AbstractEntity #[ORM\Column(length: 255)] private ?string $bootFileName = null; + /** + * @var Collection + */ + #[ORM\OneToMany(mappedBy: 'subnet', targetEntity: OrganizationalUnit::class)] + private Collection $organizationalUnits; + + public function __construct() + { + parent::__construct(); + $this->organizationalUnits = new ArrayCollection(); + } + public function getNetmask(): ?string { return $this->netmask; @@ -69,4 +83,45 @@ class Subnet extends AbstractEntity return $this; } + + /** + * @return Collection + */ + public function getOrganizationalUnits(): Collection + { + return $this->organizationalUnits; + } + + public function setOrganizationalUnits(array $organizationalUnits): static + { + $this->organizationalUnits->clear(); + + foreach ($organizationalUnits as $organizationalUnit){ + $this->addOrganizationalUnit($organizationalUnit); + } + + return $this; + } + + public function addOrganizationalUnit(OrganizationalUnit $organizationalUnit): static + { + if (!$this->organizationalUnits->contains($organizationalUnit)) { + $this->organizationalUnits->add($organizationalUnit); + $organizationalUnit->setSubnet($this); + } + + return $this; + } + + public function removeOrganizationalUnit(OrganizationalUnit $organizationalUnit): static + { + if ($this->organizationalUnits->removeElement($organizationalUnit)) { + // set the owning side to null (unless already changed) + if ($organizationalUnit->getSubnet() === $this) { + $organizationalUnit->setSubnet(null); + } + } + + return $this; + } }