-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from websmurf/master
Added DNS zone management options
- Loading branch information
Showing
15 changed files
with
813 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 - _ @ .'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '[email protected]', | ||
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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.