Skip to content

Commit

Permalink
dns zone beheer mogelijk gemaakt
Browse files Browse the repository at this point in the history
  • Loading branch information
websmurf committed Feb 8, 2024
1 parent 44ed13a commit 1286ad3
Show file tree
Hide file tree
Showing 14 changed files with 812 additions and 0 deletions.
225 changes: 225 additions & 0 deletions src/Api/DnsZonesApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Api;

use InvalidArgumentException;
use SandwaveIo\RealtimeRegister\Domain\DnsZone;
use SandwaveIo\RealtimeRegister\Domain\DnsZoneCollection;
use SandwaveIo\RealtimeRegister\Domain\DomainZoneRecordCollection;
use SandwaveIo\RealtimeRegister\Domain\Enum\ZoneServiceEnum;
use Webmozart\Assert\Assert;

final class DnsZonesApi extends AbstractApi
{
/**
* @see https://dm.realtimeregister.com/docs/api/dns/zones/list
*
* @param int|null $limit
* @param int|null $offset
* @param string|null $search
* @param array|null $parameters
*
* @throws InvalidArgumentException
*
* @return DnsZoneCollection
*/
public function list(
?int $limit = null,
?int $offset = null,
?string $search = null,
?array $parameters = null
): DnsZoneCollection {
$query = [];
if ($limit !== null) {
$query['limit'] = $limit;
}
if ($offset !== null) {
$query['offset'] = $offset;
}
if ($search !== null) {
$query['q'] = $search;
}
if ($parameters !== null) {
$query = array_merge($parameters, $query);
}

$response = $this->client->get('v2/dns/zones', $query);
return DnsZoneCollection::fromArray($response->json());
}

/**
* @see https://dm.realtimeregister.com/docs/api/dns/zones/get
*
* @param int $id
*
* @throws InvalidArgumentException
*
* @return DnsZone
*/
public function get(int $id): DnsZone
{
$response = $this->client->get(
sprintf('v2/dns/zones/%s', $id)
);
return DnsZone::fromArray($response->json());
}

/**
* @see https://dm.realtimeregister.com/docs/api/dns/zones/create
*
* @throws InvalidArgumentException
*/
public function create(
string $name,
?ZoneServiceEnum $service = null,
?string $template = null,
?bool $link = null,
?string $master = null,
?array $ns = null,
?bool $dnssec = null,
?string $hostMaster = null,
?int $refresh = null,
?int $retry = null,
?int $expire = null,
?int $ttl = null,
?DomainZoneRecordCollection $records = null,
): void {
$this->validateZoneName($name);

$payload = [
'name' => $name,
];
if ($service !== null) {
$payload['service'] = $service->value;
}
if ($template !== null) {
$payload['template'] = $template;
}
if ($link !== null) {
$payload['link'] = $link;
}
if ($master !== null) {
$payload['master'] = $master;
}
if($ns !== null) {
$payload['ns'] = $ns;
}
if($dnssec !== null) {
$payload['dnssec'] = $dnssec;
}
if($hostMaster !== null) {
$payload['hostMaster'] = $hostMaster;
}
if($refresh !== null) {
$payload['refresh'] = $refresh;
}
if($retry !== null) {
$payload['retry'] = $retry;
}
if($expire !== null) {
$payload['expire'] = $expire;
}
if($ttl !== null) {
$payload['ttl'] = $ttl;
}
if($records !== null) {
$payload['records'] = $records->toArray();
}

$this->client->post('v2/dns/zones', $payload);
}

/**
* @see https://dm.realtimeregister.com/docs/api/dns/zones/update
*
* @throws InvalidArgumentException
*/
public function update(
int $id,
?string $name,
?ZoneServiceEnum $service = null,
?string $template = null,
?bool $link = null,
?string $master = null,
?array $ns = null,
?bool $dnssec = null,
?string $hostMaster = null,
?int $refresh = null,
?int $retry = null,
?int $expire = null,
?int $ttl = null,
?DomainZoneRecordCollection $records = null,
): void {
$payload = [];
if ($name !== null) {
$this->validateZoneName($name);
$payload['name'] = $name;
}
if ($service !== null) {
$payload['service'] = $service->value;
}
if ($template !== null) {
$payload['template'] = $template;
}
if ($link !== null) {
$payload['link'] = $link;
}
if ($master !== null) {
$payload['master'] = $master;
}
if($ns !== null) {
$payload['ns'] = $ns;
}
if($dnssec !== null) {
$payload['dnssec'] = $dnssec;
}
if($hostMaster !== null) {
$payload['hostMaster'] = $hostMaster;
}
if($refresh !== null) {
$payload['refresh'] = $refresh;
}
if($retry !== null) {
$payload['retry'] = $retry;
}
if($expire !== null) {
$payload['expire'] = $expire;
}
if($ttl !== null) {
$payload['ttl'] = $ttl;
}
if($records !== null) {
$payload['records'] = $records->toArray();
}

$this->client->post(
sprintf('v2/dns/zones/%s/update', $id),
$payload,
);
}

/**
* @see https://dm.realtimeregister.com/docs/api/dns/zones/delete
*
* @param int $id The id of the zone to delete
*/
public function delete(int $id): void
{
$this->client->delete(
sprintf('v2/dns/zones/%s', $id)
);
}

/**
* Validate zone name input.
*
* @param string $name Zone name
*
* @throws InvalidArgumentException
*/
private function validateZoneName(string $name): void
{
Assert::lengthBetween($name, 3, 40, 'Zone name should be between 3 and 40 characters');
Assert::regex($name, '/^[a-zA-Z0-9\-_@.]+$/', 'Invalid zone name, allowed characters: a-z A-Z 0-9 - _ @ .');
}
}
87 changes: 87 additions & 0 deletions src/Domain/DnsZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

use DateTimeImmutable;
use SandwaveIo\RealtimeRegister\Domain\Enum\ZoneServiceEnum;

final class DnsZone implements DomainObjectInterface
{
private function __construct(
public int $id,
public string $name,
public string $customer,
public DateTimeImmutable $createdDate,
public ?DateTimeImmutable $updatedDate,
public ?DateTimeImmutable $deletionDate,
public bool $managed,
public ZoneServiceEnum $service,
public ?string $template,
public ?string $master,
public ?array $ns,
public DomainZoneRecordCollection $defaultRecords,
public string $hostMaster = 'hostmaster@realtimeregister.com',
public int $refresh = 3600,
public int $retry = 3600,
public int $expire = 14 * 24 * 60 * 60,
public int $ttl = 3600,
public bool $dnssec = false,
public ?DomainZoneRecordCollection $records = null,
public ?KeyDataCollection $keyData = null,
) {
}

public static function fromArray(array $json): self
{
return new DnsZone(
$json['id'],
$json['name'] ?? null,
$json['customer'],
new DateTimeImmutable($json['createdDate']),
isset($json['updatedDate']) ? new DateTimeImmutable($json['updatedDate']) : null,
isset($json['deletionDate']) ? new DateTimeImmutable($json['deletionDate']) : null,
$json['managed'],
ZoneServiceEnum::from($json['service']),
$json['template'] ?? null,
$json['master'] ?? null,
$json['ns'] ?? null,
DomainZoneRecordCollection::fromArray($json['defaultRecords']),
$json['hostMaster'],
$json['refresh'],
$json['retry'],
$json['expire'],
$json['ttl'],
$json['dnssec'],
isset($json['records']) ? DomainZoneRecordCollection::fromArray($json['records']) : null,
isset($json['keyData']) ? KeyDataCollection::fromArray($json['keyData']) : null,
);
}

public function toArray(): array
{
return array_filter([
'id' => $this->id,
'customer' => $this->customer,
'name' => $this->name,
'createdDate' => $this->createdDate->format('Y-m-d\TH:i:s\Z'),
'updatedDate' => $this->updatedDate?->format('Y-m-d\TH:i:s\Z'),
'deletionDate' => $this->deletionDate?->format('Y-m-d\TH:i:s\Z'),
'service' => $this->service->value,
'dnssec' => $this->dnssec,
'managed' => $this->managed,
'template' => $this->template,
'master' => $this->master,
'ns' => $this->ns,
'hostMaster' => $this->hostMaster,
'refresh' => $this->refresh,
'retry' => $this->retry,
'expire' => $this->expire,
'ttl' => $this->ttl,
'defaultRecords' => $this->defaultRecords->toArray(),
'records' => $this->records?->toArray(),
'keyData' => $this->keyData?->toArray(),
], function ($x) {
return $x !== null;
});
}
}
24 changes: 24 additions & 0 deletions src/Domain/DnsZoneCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

final class DnsZoneCollection extends AbstractCollection
{
/** @var DnsZone[] */
public array $entities;

public static function fromArray(array $json): self
{
return parent::fromArray($json);
}

public function offsetGet($offset): ?DnsZone
{
return $this->entities[$offset] ?? null;
}

public static function parseChild(array $json): DnsZone
{
return DnsZone::fromArray($json);
}
}
9 changes: 9 additions & 0 deletions src/Domain/Enum/ZoneServiceEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain\Enum;

enum ZoneServiceEnum: string
{
case BASIC = 'BASIC';
case PREMIUM = 'PREMIUM';
}
4 changes: 4 additions & 0 deletions src/RealtimeRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SandwaveIo\RealtimeRegister\Api\ContactsApi;
use SandwaveIo\RealtimeRegister\Api\CustomersApi;
use SandwaveIo\RealtimeRegister\Api\DnsTemplatesApi;
use SandwaveIo\RealtimeRegister\Api\DnsZonesApi;
use SandwaveIo\RealtimeRegister\Api\DomainsApi;
use SandwaveIo\RealtimeRegister\Api\FinancialApi;
use SandwaveIo\RealtimeRegister\Api\NotificationsApi;
Expand Down Expand Up @@ -38,6 +39,8 @@ final class RealtimeRegister

public DnsTemplatesApi $dnstemplates;

public DnsZonesApi $dnszones;

public TLDsApi $tlds;

public FinancialApi $financial;
Expand All @@ -62,6 +65,7 @@ public function setClient(AuthorizedClient $client): void
$this->processes = new ProcessesApi($client);
$this->providers = new ProvidersApi($client);
$this->dnstemplates = new DnsTemplatesApi($client);
$this->dnszones = new DnsZonesApi($client);
$this->tlds = new TLDsApi($client);
$this->financial = new FinancialApi($client);
}
Expand Down
Loading

0 comments on commit 1286ad3

Please sign in to comment.