Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/HetznerAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use LKDev\HetznerCloud\Models\Servers\Types\ServerTypes;
use LKDev\HetznerCloud\Models\SSHKeys\SSHKeys;
use LKDev\HetznerCloud\Models\Volumes\Volumes;
use LKDev\HetznerCloud\Models\Zones\Zones;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -327,6 +328,11 @@ public function loadBalancerTypes()
return new LoadBalancerTypes($this->httpClient);
}

public function zones()
{
return new Zones($this->httpClient);
}

/**
* @return GuzzleClient
*/
Expand Down
30 changes: 30 additions & 0 deletions src/Models/Zones/AuthoritativeNameservers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace LKDev\HetznerCloud\Models\Zones;

class AuthoritativeNameservers
{
public array $assigned;
public array $delegated;
public string $delegation_last_check;
public string $delegation_status;

/**
* @param array $assigned
* @param array $delegated
* @param string $delegation_last_check
* @param string $delegation_status
*/
public function __construct(array $assigned, array $delegated, string $delegation_last_check, string $delegation_status)
{
$this->assigned = $assigned;
$this->delegated = $delegated;
$this->delegation_last_check = $delegation_last_check;
$this->delegation_status = $delegation_status;
}

public static function fromResponse(array $response): AuthoritativeNameservers
{
return new self($response['assigned'], $response['delegated'], $response['delegation_last_check'], $response['delegation_status']);
}
}
24 changes: 24 additions & 0 deletions src/Models/Zones/PrimaryNameserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LKDev\HetznerCloud\Models\Zones;

class PrimaryNameserver
{
public string $address;
public int $port;

/**
* @param string $address
* @param int $port
*/
public function __construct(string $address, int $port)
{
$this->port = $port;
$this->address = $address;
}

public static function fromResponse(array $response): PrimaryNameserver
{
return new self($response['address'], $response['port']);
}
}
253 changes: 253 additions & 0 deletions src/Models/Zones/RRSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
<?php

namespace LKDev\HetznerCloud\Models\Zones;

use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\Clients\GuzzleClient;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Actions\Action;
use LKDev\HetznerCloud\Models\Contracts\Resource;
use LKDev\HetznerCloud\Models\Model;

class RRSet extends Model implements Resource
{
public string $id;
public string $name;
public string $type;
public int $ttl;
public array $records;
public array $labels;
public ?RRSetProtection $protection;

public int $zone;

/**
* Creates a new RRSet. This is useful if you want to create a new RRSet to pass to the createRRSet method of a Zone.
*
* @param string $name
* @param string $type
* @param array $records
* @param int|null $ttl
* @param array|null $labels
* @return RRSet|null
*/
public static function create(string $name, string $type, array $records, ?int $ttl = null, ?array $labels = []): ?RRSet
{
return (new RRSet(''))->setAdditionalData((object) [
'name' => $name,
'type' => $type,
'ttl' => $ttl,
'records' => $records,
'labels' => (object) $labels,
'protection' => (object) [],
'zone' => 0,
]);
}

/**
* @param string $id
* @param GuzzleClient|null $client
*/
public function __construct(string $id, ?GuzzleClient $client = null)
{
$this->id = $id;

parent::__construct($client);
}

/**
* @param $data
* @return \LKDev\HetznerCloud\Models\Zones\RRSet
*/
public function setAdditionalData($data)
{
$this->name = $data->name;
$this->type = $data->type;
$this->ttl = $data->ttl;
$this->records = $data->records;
$this->labels = get_object_vars($data->labels);
$this->protection = RRSetProtection::parse($data->protection);
$this->zone = $data->zone;

return $this;
}

public static function parse($input): RRSet
{
return (new self($input->id))->setAdditionalData($input);
}

public function __toRequest(): array
{
$r = [
'name' => $this->name,
'type' => $this->type,
'ttl' => $this->ttl,
'records' => $this->records,
];
if (! empty($this->labels)) {
$r['labels'] = $this->labels;
}

return $r;
}

/**
* @return RRSet|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function reload()
{
return (new Zone($this->zone))->getRRSetById($this->id);
}

/**
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function delete(): ?APIResponse
{
$response = $this->httpClient->delete('zones/'.$this->zone.'/rrsets/'.$this->id);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}

/**
* @param array $data
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function update(array $data): ?APIResponse
{
$response = $this->httpClient->put('zones/'.$this->zone.'/rrsets/'.$this->id, [
'json' => $data,
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'rrset' => self::parse(json_decode((string) $response->getBody())->rrset),
], $response->getHeaders());
}

return null;
}

/**
* @param bool $change
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function changeProtection(bool $change): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_protection', [
'json' => [
'change' => $change,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}

/**
* @param int $ttl
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function changeTTL(int $ttl): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_ttl', [
'json' => [
'ttl' => $ttl,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}

/**
* @param array<Record> $records
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function setRecords(array $records): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/set_records', [
'json' => [
'records' => $records,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}

/**
* @param array<Record> $records
* @param int|null $ttl
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function addRecords(array $records, ?int $ttl = null): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/add_records', [
'json' => [
'records' => $records,
'ttl' => $ttl,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}

/**
* @param array<Record> $records
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function removeRecords(array $records)
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/remove_records', [
'json' => [
'records' => $records,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}

return null;
}
}
49 changes: 49 additions & 0 deletions src/Models/Zones/RRSetProtection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Created by PhpStorm.
* User: lukaskammerling
* Date: 01.04.18
* Time: 19:02.
*/

namespace LKDev\HetznerCloud\Models\Zones;

// This is a read only model, that does not have any logic. Just a stupid dataholder.
use LKDev\HetznerCloud\Models\Model;

class RRSetProtection extends Model
{
/**
* @var bool
*/
public $change;

/**
* Protection constructor.
*
* @param bool $change
*/
public function __construct(bool $delete)
{
$this->change = $delete;
// Force getting the default http client
parent::__construct(null);
}

/**
* @param array $input
* @return ?RRSetProtection
*/
public static function parse($input)
{
if ($input == null) {
return null;
}
if (! is_array($input)) {
$input = get_object_vars($input);
}

return new self($input['change'] ?? false);
}
}
Loading