Skip to content

Commit 99ed071

Browse files
committed
Implement basic DNS Zones client
1 parent e8e35e5 commit 99ed071

16 files changed

+1050
-10
lines changed

src/HetznerAPIClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use LKDev\HetznerCloud\Models\Servers\Types\ServerTypes;
2222
use LKDev\HetznerCloud\Models\SSHKeys\SSHKeys;
2323
use LKDev\HetznerCloud\Models\Volumes\Volumes;
24+
use LKDev\HetznerCloud\Models\Zones\Zones;
2425
use Psr\Http\Message\ResponseInterface;
2526

2627
/**
@@ -63,9 +64,9 @@ class HetznerAPIClient
6364
protected GuzzleClient $httpClient;
6465

6566
/**
66-
* @param string $apiToken
67-
* @param string $baseUrl
68-
* @param string $userAgent
67+
* @param string $apiToken
68+
* @param string $baseUrl
69+
* @param string $userAgent
6970
*/
7071
public function __construct(string $apiToken, $baseUrl = 'https://api.hetzner.cloud/v1/', $userAgent = '')
7172
{
@@ -101,7 +102,7 @@ public function getBaseUrl(): string
101102
}
102103

103104
/**
104-
* @param string $userAgent
105+
* @param string $userAgent
105106
* @return HetznerAPIClient
106107
*/
107108
public function setUserAgent(string $userAgent): self
@@ -112,7 +113,7 @@ public function setUserAgent(string $userAgent): self
112113
}
113114

114115
/**
115-
* @param string $baseUrl
116+
* @param string $baseUrl
116117
* @return HetznerAPIClient
117118
*/
118119
public function setBaseUrl(string $baseUrl): self
@@ -141,13 +142,13 @@ public function setHttpClient(GuzzleClient $client): self
141142
}
142143

143144
/**
144-
* @param \Psr\Http\Message\ResponseInterface $response
145+
* @param \Psr\Http\Message\ResponseInterface $response
145146
*
146147
* @throws \LKDev\HetznerCloud\APIException
147148
*/
148149
public static function throwError(ResponseInterface $response)
149150
{
150-
$body = (string) $response->getBody();
151+
$body = (string)$response->getBody();
151152
if (strlen($body) > 0) {
152153
$error = \GuzzleHttp\json_decode($body);
153154
throw new APIException(APIResponse::create([
@@ -160,15 +161,15 @@ public static function throwError(ResponseInterface $response)
160161
}
161162

162163
/**
163-
* @param \Psr\Http\Message\ResponseInterface $response
164+
* @param \Psr\Http\Message\ResponseInterface $response
164165
* @return bool
165166
*
166167
* @throws \LKDev\HetznerCloud\APIException
167168
*/
168169
public static function hasError(ResponseInterface $response)
169170
{
170-
$responseDecoded = json_decode((string) $response->getBody());
171-
if (strlen((string) $response->getBody()) > 0) {
171+
$responseDecoded = json_decode((string)$response->getBody());
172+
if (strlen((string)$response->getBody()) > 0) {
172173
if (property_exists($responseDecoded, 'error')) {
173174
self::throwError($response);
174175

@@ -327,6 +328,12 @@ public function loadBalancerTypes()
327328
return new LoadBalancerTypes($this->httpClient);
328329
}
329330

331+
332+
public function zones()
333+
{
334+
return new Zones($this->httpClient);
335+
}
336+
330337
/**
331338
* @return GuzzleClient
332339
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Zones;
4+
5+
class AuthoritativeNameservers
6+
{
7+
public array $assigned;
8+
public array $delegated;
9+
public string $delegation_last_check;
10+
public string $delegation_status;
11+
12+
/**
13+
* @param array $assigned
14+
* @param array $delegated
15+
* @param string $delegation_last_check
16+
* @param string $delegation_status
17+
*/
18+
public function __construct(array $assigned, array $delegated, string $delegation_last_check, string $delegation_status)
19+
{
20+
$this->assigned = $assigned;
21+
$this->delegated = $delegated;
22+
$this->delegation_last_check = $delegation_last_check;
23+
$this->delegation_status = $delegation_status;
24+
}
25+
26+
public static function fromResponse(array $response): AuthoritativeNameservers
27+
{
28+
return new self($response['assigned'], $response['delegated'], $response['delegation_last_check'], $response['delegation_status']);
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Zones;
4+
5+
class PrimaryNameserver
6+
{
7+
public string $address;
8+
public int $port;
9+
10+
/**
11+
* @param string $address
12+
* @param int $port
13+
*/
14+
public function __construct(string $address, int $port)
15+
{
16+
$this->port = $port;
17+
$this->address = $address;
18+
}
19+
20+
21+
public static function fromResponse(array $response): PrimaryNameserver
22+
{
23+
return new self($response['address'], $response['port']);
24+
}
25+
}

src/Models/Zones/RRSet.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Zones;
4+
5+
use LKDev\HetznerCloud\Models\Protection;
6+
7+
class RRSet
8+
{
9+
public string $id;
10+
public string $name;
11+
public string $type;
12+
public int $ttl;
13+
public array $records;
14+
public array $labels;
15+
public RRSetProtection $protection;
16+
17+
/**
18+
* @param string $id
19+
* @param string $name
20+
* @param string $type
21+
* @param int $ttl
22+
* @param array $records
23+
* @param array|null $labels
24+
* @param RRSetProtection|null $protection
25+
*/
26+
public function __construct(string $id, string $name, string $type, int $ttl, array $records, ?array $labels, ?RRSetProtection $protection)
27+
{
28+
$this->id = $id;
29+
$this->name = $name;
30+
$this->type = $type;
31+
$this->ttl = $ttl;
32+
$this->records = $records;
33+
$this->labels = $labels;
34+
$this->protection = $protection;
35+
}
36+
37+
public static function fromResponse(array $data): RRSet
38+
{
39+
return new self($data['id'], $data['name'], $data['type'], $data['ttl'], $data['records'], $data['labels'], RRSetProtection::parse($data['protection']));
40+
}
41+
42+
public function __toRequest(): array
43+
{
44+
$r = [
45+
"name" => $this->name,
46+
'type' => $this->type,
47+
'ttl' => $this->ttl,
48+
'records' => $this->records,
49+
];
50+
if (!empty($this->labels)) {
51+
$r['labels'] = $this->labels;
52+
}
53+
return $r;
54+
}
55+
}
56+
57+
58+
class Record
59+
{
60+
public string $value;
61+
public string $comment;
62+
63+
64+
public function __construct(string $value, string $comment)
65+
{
66+
$this->value = $value;
67+
$this->comment = $comment;
68+
}
69+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: lukaskammerling
6+
* Date: 01.04.18
7+
* Time: 19:02.
8+
*/
9+
10+
namespace LKDev\HetznerCloud\Models\Zones;
11+
12+
// This is a read only model, that does not have any logic. Just a stupid dataholder.
13+
use LKDev\HetznerCloud\Models\Model;
14+
15+
class RRSetProtection extends Model
16+
{
17+
/**
18+
* @var bool
19+
*/
20+
public $change;
21+
22+
23+
/**
24+
* Protection constructor.
25+
*
26+
* @param bool $delete
27+
*/
28+
public function __construct(bool $delete)
29+
{
30+
$this->change = $delete;
31+
// Force getting the default http client
32+
parent::__construct(null);
33+
}
34+
35+
/**
36+
* @param array $input
37+
* @return ?RRSetProtection
38+
*/
39+
public static function parse($input)
40+
{
41+
if ($input == null) {
42+
return null;
43+
}
44+
if (!is_array($input)) {
45+
return null;
46+
}
47+
return new self($input['change'] ?? false);
48+
}
49+
}

0 commit comments

Comments
 (0)