Skip to content

Commit 7913e1d

Browse files
committed
Add all currently available endpoints
1 parent e447989 commit 7913e1d

File tree

3 files changed

+258
-20
lines changed

3 files changed

+258
-20
lines changed

src/Models/Zones/RRSet.php

Lines changed: 129 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace LKDev\HetznerCloud\Models\Zones;
44

5+
use LKDev\HetznerCloud\APIResponse;
56
use LKDev\HetznerCloud\Clients\GuzzleClient;
67
use LKDev\HetznerCloud\HetznerAPIClient;
8+
use LKDev\HetznerCloud\Models\Actions\Action;
79
use LKDev\HetznerCloud\Models\Contracts\Resource;
810
use LKDev\HetznerCloud\Models\Model;
911

@@ -20,8 +22,8 @@ class RRSet extends Model implements Resource
2022
public int $zone;
2123

2224
/**
23-
* @param string $id
24-
* @param GuzzleClient|null $client
25+
* @param string $id
26+
* @param GuzzleClient|null $client
2527
*/
2628
public function __construct(string $id, ?GuzzleClient $client = null)
2729
{
@@ -43,7 +45,6 @@ public function setAdditionalData($data)
4345
$this->labels = get_object_vars($data->labels);
4446
$this->protection = RRSetProtection::parse($data->protection);
4547
$this->zone = $data->zone;
46-
4748
return $this;
4849
}
4950

@@ -60,50 +61,160 @@ public function __toRequest(): array
6061
'ttl' => $this->ttl,
6162
'records' => $this->records,
6263
];
63-
if (! empty($this->labels)) {
64+
if (!empty($this->labels)) {
6465
$r['labels'] = $this->labels;
6566
}
6667

6768
return $r;
6869
}
6970

71+
/**
72+
* @return RRSet|null
73+
* @throws \LKDev\HetznerCloud\APIException
74+
*/
7075
public function reload()
7176
{
72-
return HetznerAPIClient::$instance->zones()->getById($this->zone)->getRRSet($this->id);
77+
return (new Zone($this->zone))->getRRSetById($this->id);
7378
}
7479

75-
public function delete()
80+
/**
81+
* @return APIResponse|null
82+
* @throws \LKDev\HetznerCloud\APIException
83+
*/
84+
public function delete(): ?APIResponse
7685
{
77-
// TODO: Implement delete() method.
86+
$response = $this->httpClient->delete('zones/' . $this->zone . '/rrsets/' . $this->id);
87+
if (!HetznerAPIClient::hasError($response)) {
88+
return APIResponse::create([
89+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
90+
], $response->getHeaders());
91+
}
92+
93+
return null;
7894
}
7995

80-
public function update(array $data)
96+
/**
97+
* @param array $data
98+
* @return APIResponse|null
99+
* @throws \LKDev\HetznerCloud\APIException
100+
*/
101+
public function update(array $data): ?APIResponse
81102
{
82-
// TODO: Implement update() method.
103+
$response = $this->httpClient->put('zones/' . $this->zone . '/rrsets/' . $this->id, [
104+
'json' => $data,
105+
]);
106+
if (!HetznerAPIClient::hasError($response)) {
107+
return APIResponse::create([
108+
'rrset' => self::parse(json_decode((string)$response->getBody())->rrset),
109+
], $response->getHeaders());
110+
}
111+
112+
return null;
83113
}
84114

85-
public function changeProtection(RRSetProtection $protection)
115+
/**
116+
* @param bool $change
117+
* @return APIResponse|null
118+
* @throws \LKDev\HetznerCloud\APIException
119+
*/
120+
public function changeProtection(bool $change): ?APIResponse
86121
{
87-
// TODO: Implement changeProtection() method.
122+
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/change_protection', [
123+
'json' => [
124+
'change' => $change,
125+
],
126+
]);
127+
if (!HetznerAPIClient::hasError($response)) {
128+
return APIResponse::create([
129+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
130+
], $response->getHeaders());
131+
}
132+
return null;
88133
}
89134

90-
public function changeTTL(int $ttl)
135+
/**
136+
* @param int $ttl
137+
* @return APIResponse|null
138+
* @throws \LKDev\HetznerCloud\APIException
139+
*/
140+
public function changeTTL(int $ttl): ?APIResponse
91141
{
92-
// TODO: Implement changeTTL() method.
142+
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/change_ttl', [
143+
'json' => [
144+
'ttl' => $ttl,
145+
],
146+
]);
147+
if (!HetznerAPIClient::hasError($response)) {
148+
return APIResponse::create([
149+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
150+
], $response->getHeaders());
151+
}
152+
153+
return null;
93154
}
94155

95-
public function setRecords(array $records)
156+
/**
157+
* @param array<Record> $records
158+
* @return APIResponse|null
159+
* @throws \LKDev\HetznerCloud\APIException
160+
*/
161+
public function setRecords(array $records): ?APIResponse
96162
{
97-
// TODO: Implement setRecords() method.
163+
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/set_records', [
164+
'json' => [
165+
'records' => $records,
166+
],
167+
]);
168+
if (!HetznerAPIClient::hasError($response)) {
169+
return APIResponse::create([
170+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
171+
], $response->getHeaders());
172+
}
173+
174+
return null;
98175
}
99176

100-
public function addRecords(array $records)
177+
/**
178+
* @param array<Record> $records
179+
* @param int|null $ttl
180+
* @return APIResponse|null
181+
* @throws \LKDev\HetznerCloud\APIException
182+
*/
183+
public function addRecords(array $records, ?int $ttl = null): ?APIResponse
101184
{
102-
// TODO: Implement addRecords() method.
185+
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/add_records', [
186+
'json' => [
187+
'records' => $records,
188+
'ttl' => $ttl,
189+
],
190+
]);
191+
if (!HetznerAPIClient::hasError($response)) {
192+
return APIResponse::create([
193+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
194+
], $response->getHeaders());
195+
}
196+
197+
return null;
103198
}
104199

200+
/**
201+
* @param array<Record> $records
202+
* @return APIResponse|null
203+
* @throws \LKDev\HetznerCloud\APIException
204+
*/
105205
public function removeRecords(array $records)
106206
{
107-
// TODO: Implement removeRecords() method.
207+
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/remove_records', [
208+
'json' => [
209+
'records' => $records,
210+
],
211+
]);
212+
if (!HetznerAPIClient::hasError($response)) {
213+
return APIResponse::create([
214+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
215+
], $response->getHeaders());
216+
}
217+
218+
return null;
108219
}
109220
}

src/Models/Zones/Zone.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function listRRSets(?RRSetRequestOpts $requestOpts = null): ?APIResponse
322322
$resp = json_decode((string) $response->getBody());
323323
$rrsets = [];
324324
foreach ($resp->rrsets as $rrset) {
325-
$rrsets[] = RRSet::parse(get_object_vars($rrset));
325+
$rrsets[] = RRSet::parse($rrset);
326326
}
327327

328328
return APIResponse::create([
@@ -367,7 +367,7 @@ public function createRRSet(string $name, string $type, array $records, ?int $tt
367367

368368
return APIResponse::create([
369369
'action' => Action::parse($payload->action),
370-
'rrset' => RRSet::parse(get_object_vars($payload->rrset)),
370+
'rrset' => RRSet::parse($payload->rrset),
371371
], $response->getHeaders());
372372
}
373373

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace LKDev\Tests\Unit\Models\Zones;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use LKDev\HetznerCloud\Models\Zones\PrimaryNameserver;
7+
use LKDev\HetznerCloud\Models\Zones\Record;
8+
use LKDev\HetznerCloud\Models\Zones\RRSet;
9+
use LKDev\HetznerCloud\Models\Zones\Zone;
10+
use LKDev\HetznerCloud\Models\Zones\Zones;
11+
use LKDev\Tests\TestCase;
12+
13+
class RRSetTest extends TestCase
14+
{
15+
/**
16+
* @var RRSet
17+
*/
18+
protected $rrset;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone_rrset.json')));
24+
$this->rrset = (new Zone(4711))->getRRSetById('www/A');;
25+
}
26+
27+
public function testDelete()
28+
{
29+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('delete_rrset')));
30+
$resp = $this->rrset->delete();
31+
32+
$this->assertEquals('delete_rrset', $resp->action->command);
33+
$this->assertEquals($this->rrset->zone, $resp->action->resources[0]->id);
34+
$this->assertEquals('zone', $resp->action->resources[0]->type);
35+
$this->assertLastRequestEquals('DELETE', '/zones/4711/rrsets/www/A');
36+
}
37+
38+
public function testUpdate()
39+
{
40+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone_rrset.json')));
41+
$this->rrset->update(['labels' => ['environment' => 'prod']]);;
42+
$this->assertLastRequestEquals('PUT', '/zones/4711/rrsets/www/A');
43+
$this->assertLastRequestBodyParametersEqual(['labels' => ['environment' => 'prod']]);
44+
}
45+
46+
public function testChangeProtection()
47+
{
48+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('change_rrset_protection')));;
49+
$apiResponse = $this->rrset->changeProtection(true);
50+
$this->assertEquals('change_rrset_protection', $apiResponse->action->command);
51+
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
52+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
53+
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/change_protection');
54+
$this->assertLastRequestBodyParametersEqual(['change' => true]);
55+
}
56+
57+
public function testChangeTTL()
58+
{
59+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('change_rrset_ttl')));
60+
$apiResponse = $this->rrset->changeTTL(50);
61+
$this->assertEquals('change_rrset_ttl', $apiResponse->action->command);
62+
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
63+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
64+
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/change_ttl');
65+
$this->assertLastRequestBodyParametersEqual(['ttl' => 50]);
66+
}
67+
68+
public function testSetRecords()
69+
{
70+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('set_rrset_records')));
71+
$apiResponse = $this->rrset->setRecords([
72+
new Record('198.51.100.1', 'my webserver at Hetzner Cloud'),
73+
]);
74+
$this->assertEquals('set_rrset_records', $apiResponse->action->command);
75+
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
76+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
77+
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/set_records');
78+
$this->assertLastRequestBodyParametersEqual(['records' => [
79+
[
80+
"value" => "198.51.100.1",
81+
"comment" => "my webserver at Hetzner Cloud"
82+
]
83+
]]);
84+
}
85+
86+
public function testAddRecords()
87+
{
88+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('add_rrset_records')));
89+
$apiResponse = $this->rrset->addRecords([
90+
new Record('198.51.100.1', 'my webserver at Hetzner Cloud'),
91+
], 3600);
92+
$this->assertEquals('add_rrset_records', $apiResponse->action->command);
93+
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
94+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
95+
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/add_records');
96+
$this->assertLastRequestBodyParametersEqual(['ttl' => 3600, 'records' => [
97+
[
98+
"value" => "198.51.100.1",
99+
"comment" => "my webserver at Hetzner Cloud"
100+
]
101+
]]);
102+
}
103+
104+
public function testRemoveRecords()
105+
{
106+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('remove_rrset_records')));
107+
$apiResponse = $this->rrset->removeRecords([
108+
new Record('198.51.100.1', 'my webserver at Hetzner Cloud'),
109+
]);
110+
$this->assertEquals('remove_rrset_records', $apiResponse->action->command);
111+
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
112+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
113+
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/remove_records');
114+
$this->assertLastRequestBodyParametersEqual(['records' => [
115+
[
116+
"value" => "198.51.100.1",
117+
"comment" => "my webserver at Hetzner Cloud"
118+
]
119+
]]);
120+
}
121+
122+
123+
protected function getGenericActionResponse(string $command)
124+
{
125+
return str_replace('$command', $command, file_get_contents(__DIR__ . '/fixtures/zone_action_generic.json'));
126+
}
127+
}

0 commit comments

Comments
 (0)