Skip to content

Commit 939cbe0

Browse files
committed
Implement first RRSet methods
1 parent f6f3aa5 commit 939cbe0

File tree

8 files changed

+289
-45
lines changed

8 files changed

+289
-45
lines changed

src/Models/Zones/RRSet.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RRSet
1010
public int $ttl;
1111
public array $records;
1212
public array $labels;
13-
public RRSetProtection $protection;
13+
public ?RRSetProtection $protection;
1414

1515
/**
1616
* @param string $id
@@ -34,7 +34,7 @@ public function __construct(string $id, string $name, string $type, int $ttl, ar
3434

3535
public static function fromResponse(array $data): RRSet
3636
{
37-
return new self($data['id'], $data['name'], $data['type'], $data['ttl'], $data['records'], $data['labels'], RRSetProtection::parse($data['protection']));
37+
return new self($data['id'], $data['name'], $data['type'], $data['ttl'], $data['records'], get_object_vars($data['labels']), RRSetProtection::parse($data['protection']));
3838
}
3939

4040
public function __toRequest(): array
@@ -52,15 +52,3 @@ public function __toRequest(): array
5252
return $r;
5353
}
5454
}
55-
56-
class Record
57-
{
58-
public string $value;
59-
public string $comment;
60-
61-
public function __construct(string $value, string $comment)
62-
{
63-
$this->value = $value;
64-
$this->comment = $comment;
65-
}
66-
}

src/Models/Zones/RRSetProtection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function parse($input)
4141
return null;
4242
}
4343
if (! is_array($input)) {
44-
return null;
44+
$input = get_object_vars($input);
4545
}
4646

4747
return new self($input['change'] ?? false);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Zones;
4+
5+
use LKDev\HetznerCloud\RequestOpts;
6+
7+
/**
8+
* Class ServerRequestOpts.
9+
*/
10+
class RRSetRequestOpts extends RequestOpts
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public $name;
16+
17+
/**
18+
* @var string
19+
*/
20+
public $type;
21+
22+
/**
23+
* RequestOpts constructor.
24+
*
25+
* @param string|null $name
26+
* @param string|null $type
27+
* @param int|null $perPage
28+
* @param int|null $page
29+
* @param string|null $labelSelector
30+
*/
31+
public function __construct(?string $name = null, ?string $type = null, ?int $perPage = null, ?int $page = null, ?string $labelSelector = null)
32+
{
33+
$this->name = $name;
34+
$this->type = $type;
35+
parent::__construct($perPage, $page, $labelSelector);
36+
}
37+
}

src/Models/Zones/Record.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Zones;
4+
5+
class Record
6+
{
7+
public string $value;
8+
public string $comment;
9+
10+
public function __construct(string $value, string $comment)
11+
{
12+
$this->value = $value;
13+
$this->comment = $comment;
14+
}
15+
}

src/Models/Zones/Zone.php

Lines changed: 119 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace LKDev\HetznerCloud\Models\Zones;
44

5+
use LKDev\HetznerCloud\APIException;
56
use LKDev\HetznerCloud\APIResponse;
67
use LKDev\HetznerCloud\Clients\GuzzleClient;
78
use LKDev\HetznerCloud\HetznerAPIClient;
89
use LKDev\HetznerCloud\Models\Actions\Action;
910
use LKDev\HetznerCloud\Models\Contracts\Resource;
11+
use LKDev\HetznerCloud\Models\Meta;
1012
use LKDev\HetznerCloud\Models\Model;
1113
use LKDev\HetznerCloud\Models\Protection;
14+
use LKDev\HetznerCloud\RequestOpts;
1215

1316
class Zone extends Model implements Resource
1417
{
@@ -72,8 +75,8 @@ class Zone extends Model implements Resource
7275
public AuthoritativeNameservers $authoritative_nameservers;
7376

7477
/**
75-
* @param int $zoneId
76-
* @param GuzzleClient|null $httpClient
78+
* @param int $zoneId
79+
* @param GuzzleClient|null $httpClient
7780
*/
7881
public function __construct(int $zoneId, ?GuzzleClient $httpClient = null)
7982
{
@@ -131,9 +134,9 @@ public function reload()
131134
public function delete(): ?APIResponse
132135
{
133136
$response = $this->httpClient->delete($this->replaceZoneIdInUri('zones/{id}'));
134-
if (! HetznerAPIClient::hasError($response)) {
137+
if (!HetznerAPIClient::hasError($response)) {
135138
return APIResponse::create([
136-
'action' => Action::parse(json_decode((string) $response->getBody())->action),
139+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
137140
], $response->getHeaders());
138141
}
139142

@@ -145,7 +148,7 @@ public function delete(): ?APIResponse
145148
*
146149
* @see https://docs.hetzner.cloud/reference/cloud#zones-update-a-zone
147150
*
148-
* @param array $data
151+
* @param array $data
149152
* @return APIResponse|null
150153
*
151154
* @throws \LKDev\HetznerCloud\APIException
@@ -155,9 +158,9 @@ public function update(array $data)
155158
$response = $this->httpClient->put($this->replaceZoneIdInUri('zones/{id}'), [
156159
'json' => $data,
157160
]);
158-
if (! HetznerAPIClient::hasError($response)) {
161+
if (!HetznerAPIClient::hasError($response)) {
159162
return APIResponse::create([
160-
'zone' => self::parse(json_decode((string) $response->getBody())->zone),
163+
'zone' => self::parse(json_decode((string)$response->getBody())->zone),
161164
], $response->getHeaders());
162165
}
163166

@@ -169,21 +172,21 @@ public function update(array $data)
169172
*
170173
* @see https://docs.hetzner.cloud/#zone-actions-change-zone-protection
171174
*
172-
* @param bool $delete
175+
* @param bool $delete
173176
* @return APIResponse|null
174177
*
175178
* @throws \LKDev\HetznerCloud\APIException
176179
*/
177180
public function changeProtection(bool $delete = true): ?APIResponse
178181
{
179-
$response = $this->httpClient->post('zones/'.$this->id.'/actions/change_protection', [
182+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/change_protection', [
180183
'json' => [
181184
'delete' => $delete,
182185
],
183186
]);
184-
if (! HetznerAPIClient::hasError($response)) {
187+
if (!HetznerAPIClient::hasError($response)) {
185188
return APIResponse::create([
186-
'action' => Action::parse(json_decode((string) $response->getBody())->action),
189+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
187190
], $response->getHeaders());
188191
}
189192

@@ -192,10 +195,10 @@ public function changeProtection(bool $delete = true): ?APIResponse
192195

193196
public function exportZonefile(): ?APIResponse
194197
{
195-
$response = $this->httpClient->get('zones/'.$this->id.'/zonefile');
196-
if (! HetznerAPIClient::hasError($response)) {
198+
$response = $this->httpClient->get('zones/' . $this->id . '/zonefile');
199+
if (!HetznerAPIClient::hasError($response)) {
197200
return APIResponse::create([
198-
'zonefile' => json_decode((string) $response->getBody())->zonefile,
201+
'zonefile' => json_decode((string)$response->getBody())->zonefile,
199202
], $response->getHeaders());
200203
}
201204

@@ -204,14 +207,14 @@ public function exportZonefile(): ?APIResponse
204207

205208
public function changeTTL(int $ttl): ?APIResponse
206209
{
207-
$response = $this->httpClient->post('zones/'.$this->id.'/actions/change_ttl', [
210+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/change_ttl', [
208211
'json' => [
209212
'ttl' => $ttl,
210213
],
211214
]);
212-
if (! HetznerAPIClient::hasError($response)) {
215+
if (!HetznerAPIClient::hasError($response)) {
213216
return APIResponse::create([
214-
'action' => Action::parse(json_decode((string) $response->getBody())->action),
217+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
215218
], $response->getHeaders());
216219
}
217220

@@ -220,22 +223,22 @@ public function changeTTL(int $ttl): ?APIResponse
220223

221224
public function importZonefile(string $zonefile): ?APIResponse
222225
{
223-
$response = $this->httpClient->post('zones/'.$this->id.'/actions/import_zonefile', [
226+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/import_zonefile', [
224227
'json' => [
225228
'zonefile' => $zonefile,
226229
],
227230
]);
228-
if (! HetznerAPIClient::hasError($response)) {
231+
if (!HetznerAPIClient::hasError($response)) {
229232
return APIResponse::create([
230-
'action' => Action::parse(json_decode((string) $response->getBody())->action),
233+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
231234
], $response->getHeaders());
232235
}
233236

234237
return null;
235238
}
236239

237240
/**
238-
* @param string $uri
241+
* @param string $uri
239242
* @return string
240243
*/
241244
protected function replaceZoneIdInUri(string $uri): string
@@ -257,19 +260,110 @@ public static function parse($input)
257260
}
258261

259262
/**
260-
* @param array<PrimaryNameserver> $primary_nameservers
263+
* @param array<PrimaryNameserver> $primary_nameservers
261264
* @return void#
265+
* @throws APIException
262266
*/
263267
public function changePrimaryNameservers(array $primary_nameservers)
264268
{
265-
$response = $this->httpClient->post('zones/'.$this->id.'/actions/change_primary_nameservers', [
269+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/change_primary_nameservers', [
266270
'json' => [
267271
'primary_nameservers' => $primary_nameservers,
268272
],
269273
]);
270-
if (! HetznerAPIClient::hasError($response)) {
274+
if (!HetznerAPIClient::hasError($response)) {
271275
return APIResponse::create([
272-
'action' => Action::parse(json_decode((string) $response->getBody())->action),
276+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
277+
], $response->getHeaders());
278+
}
279+
280+
return null;
281+
}
282+
283+
/**
284+
* @param RRSetRequestOpts|null $requestOpts
285+
* @return array<RRSet>
286+
* @throws APIException
287+
*/
288+
public function allRRSets(?RRSetRequestOpts $requestOpts = null): array
289+
{
290+
if ($requestOpts == null) {
291+
$requestOpts = new RRSetRequestOpts();
292+
}
293+
$entities = [];
294+
$requestOpts->per_page = HetznerAPIClient::MAX_ENTITIES_PER_PAGE;
295+
$max_pages = PHP_INT_MAX;
296+
for ($i = 1; $i < $max_pages; $i++) {
297+
$requestOpts->page = $i;
298+
$_f = $this->listRRSets($requestOpts);
299+
$entities = array_merge($entities, $_f->rrsets);
300+
if ($_f->meta->pagination->page === $_f->meta->pagination->last_page || $_f->meta->pagination->last_page === null) {
301+
$max_pages = 0;
302+
}
303+
}
304+
305+
return $entities;
306+
}
307+
308+
/**
309+
* @param RRSetRequestOpts|null $requestOpts
310+
* @return APIResponse|null
311+
* @throws \LKDev\HetznerCloud\APIException
312+
*/
313+
public function listRRSets(?RRSetRequestOpts $requestOpts = null): ?APIResponse
314+
{
315+
if ($requestOpts == null) {
316+
$requestOpts = new RRSetRequestOpts();
317+
}
318+
$response = $this->httpClient->get('zones/' . $this->id . "/rrsets" . $requestOpts->buildQuery());
319+
if (!HetznerAPIClient::hasError($response)) {
320+
$resp = json_decode((string)$response->getBody());
321+
$rrsets = [];
322+
foreach ($resp->rrsets as $rrset) {
323+
$rrsets[] = RRSet::fromResponse(get_object_vars($rrset));
324+
}
325+
return APIResponse::create([
326+
'meta' => Meta::parse($resp->meta),
327+
'rrsets' => $rrsets,
328+
], $response->getHeaders());
329+
}
330+
331+
return null;
332+
}
333+
334+
/**
335+
* @param string $name
336+
* @param string $type
337+
* @param array<Record> $records
338+
* @param int|null $ttl
339+
* @param array|null $labels
340+
* @return void
341+
* @throws APIException
342+
*/
343+
public function createRRSet(string $name, string $type, array $records, ?int $ttl = null, ?array $labels = [])
344+
{
345+
$parameters = [
346+
'name' => $name,
347+
'type' => $type,
348+
'records' => $records,
349+
];
350+
if ($ttl !== null) {
351+
$parameters['ttl'] = $ttl;
352+
}
353+
if (!empty($labels)) {
354+
$parameters['labels'] = $labels;
355+
}
356+
357+
$response = $this->httpClient->post('zones/' . $this->id . '/rrsets', [
358+
'json' => $parameters,
359+
]);
360+
361+
if (!HetznerAPIClient::hasError($response)) {
362+
$payload = json_decode((string)$response->getBody());
363+
364+
return APIResponse::create([
365+
'action' => Action::parse($payload->action),
366+
'rrset' => RRSet::fromResponse(get_object_vars($payload->rrset)),
273367
], $response->getHeaders());
274368
}
275369

0 commit comments

Comments
 (0)