Skip to content

Commit 6753f35

Browse files
StyleCIBotLKaemmerling
authored andcommitted
Add all currently available endpoints
1 parent 7913e1d commit 6753f35

File tree

3 files changed

+90
-61
lines changed

3 files changed

+90
-61
lines changed

src/Models/Zones/RRSet.php

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,31 @@ class RRSet extends Model implements Resource
2222
public int $zone;
2323

2424
/**
25-
* @param string $id
26-
* @param GuzzleClient|null $client
25+
* Creates a new RRSet. This is useful if you want to create a new RRSet to pass to the createRRSet method of a Zone.
26+
*
27+
* @param string $name
28+
* @param string $type
29+
* @param array $records
30+
* @param int|null $ttl
31+
* @param array|null $labels
32+
* @return RRSet|null
33+
*/
34+
public static function create(string $name, string $type, array $records, ?int $ttl = null, ?array $labels = []): ?RRSet
35+
{
36+
return (new RRset(""))->setAdditionalData((object)[
37+
'name' => $name,
38+
'type' => $type,
39+
'ttl' => $ttl,
40+
'records' => $records,
41+
'labels' => (object)$labels,
42+
'protection' => (object)[],
43+
'zone' => 0,
44+
]);
45+
}
46+
47+
/**
48+
* @param string $id
49+
* @param GuzzleClient|null $client
2750
*/
2851
public function __construct(string $id, ?GuzzleClient $client = null)
2952
{
@@ -45,6 +68,7 @@ public function setAdditionalData($data)
4568
$this->labels = get_object_vars($data->labels);
4669
$this->protection = RRSetProtection::parse($data->protection);
4770
$this->zone = $data->zone;
71+
4872
return $this;
4973
}
5074

@@ -61,7 +85,7 @@ public function __toRequest(): array
6185
'ttl' => $this->ttl,
6286
'records' => $this->records,
6387
];
64-
if (!empty($this->labels)) {
88+
if (! empty($this->labels)) {
6589
$r['labels'] = $this->labels;
6690
}
6791

@@ -70,6 +94,7 @@ public function __toRequest(): array
7094

7195
/**
7296
* @return RRSet|null
97+
*
7398
* @throws \LKDev\HetznerCloud\APIException
7499
*/
75100
public function reload()
@@ -79,139 +104,147 @@ public function reload()
79104

80105
/**
81106
* @return APIResponse|null
107+
*
82108
* @throws \LKDev\HetznerCloud\APIException
83109
*/
84110
public function delete(): ?APIResponse
85111
{
86-
$response = $this->httpClient->delete('zones/' . $this->zone . '/rrsets/' . $this->id);
87-
if (!HetznerAPIClient::hasError($response)) {
112+
$response = $this->httpClient->delete('zones/'.$this->zone.'/rrsets/'.$this->id);
113+
if (! HetznerAPIClient::hasError($response)) {
88114
return APIResponse::create([
89-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
115+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
90116
], $response->getHeaders());
91117
}
92118

93119
return null;
94120
}
95121

96122
/**
97-
* @param array $data
123+
* @param array $data
98124
* @return APIResponse|null
125+
*
99126
* @throws \LKDev\HetznerCloud\APIException
100127
*/
101128
public function update(array $data): ?APIResponse
102129
{
103-
$response = $this->httpClient->put('zones/' . $this->zone . '/rrsets/' . $this->id, [
130+
$response = $this->httpClient->put('zones/'.$this->zone.'/rrsets/'.$this->id, [
104131
'json' => $data,
105132
]);
106-
if (!HetznerAPIClient::hasError($response)) {
133+
if (! HetznerAPIClient::hasError($response)) {
107134
return APIResponse::create([
108-
'rrset' => self::parse(json_decode((string)$response->getBody())->rrset),
135+
'rrset' => self::parse(json_decode((string) $response->getBody())->rrset),
109136
], $response->getHeaders());
110137
}
111138

112139
return null;
113140
}
114141

115142
/**
116-
* @param bool $change
143+
* @param bool $change
117144
* @return APIResponse|null
145+
*
118146
* @throws \LKDev\HetznerCloud\APIException
119147
*/
120148
public function changeProtection(bool $change): ?APIResponse
121149
{
122-
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/change_protection', [
150+
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_protection', [
123151
'json' => [
124152
'change' => $change,
125153
],
126154
]);
127-
if (!HetznerAPIClient::hasError($response)) {
155+
if (! HetznerAPIClient::hasError($response)) {
128156
return APIResponse::create([
129-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
157+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
130158
], $response->getHeaders());
131159
}
160+
132161
return null;
133162
}
134163

135164
/**
136-
* @param int $ttl
165+
* @param int $ttl
137166
* @return APIResponse|null
167+
*
138168
* @throws \LKDev\HetznerCloud\APIException
139169
*/
140170
public function changeTTL(int $ttl): ?APIResponse
141171
{
142-
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/change_ttl', [
172+
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_ttl', [
143173
'json' => [
144174
'ttl' => $ttl,
145175
],
146176
]);
147-
if (!HetznerAPIClient::hasError($response)) {
177+
if (! HetznerAPIClient::hasError($response)) {
148178
return APIResponse::create([
149-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
179+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
150180
], $response->getHeaders());
151181
}
152182

153183
return null;
154184
}
155185

156186
/**
157-
* @param array<Record> $records
187+
* @param array<Record> $records
158188
* @return APIResponse|null
189+
*
159190
* @throws \LKDev\HetznerCloud\APIException
160191
*/
161192
public function setRecords(array $records): ?APIResponse
162193
{
163-
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/set_records', [
194+
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/set_records', [
164195
'json' => [
165196
'records' => $records,
166197
],
167198
]);
168-
if (!HetznerAPIClient::hasError($response)) {
199+
if (! HetznerAPIClient::hasError($response)) {
169200
return APIResponse::create([
170-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
201+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
171202
], $response->getHeaders());
172203
}
173204

174205
return null;
175206
}
176207

177208
/**
178-
* @param array<Record> $records
179-
* @param int|null $ttl
209+
* @param array<Record> $records
210+
* @param int|null $ttl
180211
* @return APIResponse|null
212+
*
181213
* @throws \LKDev\HetznerCloud\APIException
182214
*/
183215
public function addRecords(array $records, ?int $ttl = null): ?APIResponse
184216
{
185-
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/add_records', [
217+
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/add_records', [
186218
'json' => [
187219
'records' => $records,
188220
'ttl' => $ttl,
189221
],
190222
]);
191-
if (!HetznerAPIClient::hasError($response)) {
223+
if (! HetznerAPIClient::hasError($response)) {
192224
return APIResponse::create([
193-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
225+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
194226
], $response->getHeaders());
195227
}
196228

197229
return null;
198230
}
199231

200232
/**
201-
* @param array<Record> $records
233+
* @param array<Record> $records
202234
* @return APIResponse|null
235+
*
203236
* @throws \LKDev\HetznerCloud\APIException
204237
*/
205238
public function removeRecords(array $records)
206239
{
207-
$response = $this->httpClient->post('zones/' . $this->zone . '/rrsets/' . $this->id . '/actions/remove_records', [
240+
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/remove_records', [
208241
'json' => [
209242
'records' => $records,
210243
],
211244
]);
212-
if (!HetznerAPIClient::hasError($response)) {
245+
if (! HetznerAPIClient::hasError($response)) {
213246
return APIResponse::create([
214-
'action' => Action::parse(json_decode((string)$response->getBody())->action),
247+
'action' => Action::parse(json_decode((string) $response->getBody())->action),
215248
], $response->getHeaders());
216249
}
217250

tests/Unit/Models/Zones/RRSetTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace LKDev\Tests\Unit\Models\Zones;
44

55
use GuzzleHttp\Psr7\Response;
6-
use LKDev\HetznerCloud\Models\Zones\PrimaryNameserver;
76
use LKDev\HetznerCloud\Models\Zones\Record;
87
use LKDev\HetznerCloud\Models\Zones\RRSet;
98
use LKDev\HetznerCloud\Models\Zones\Zone;
10-
use LKDev\HetznerCloud\Models\Zones\Zones;
119
use LKDev\Tests\TestCase;
1210

1311
class RRSetTest extends TestCase
@@ -20,8 +18,8 @@ class RRSetTest extends TestCase
2018
public function setUp(): void
2119
{
2220
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');;
21+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone_rrset.json')));
22+
$this->rrset = (new Zone(4711))->getRRSetById('www/A');
2523
}
2624

2725
public function testDelete()
@@ -37,15 +35,15 @@ public function testDelete()
3735

3836
public function testUpdate()
3937
{
40-
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone_rrset.json')));
41-
$this->rrset->update(['labels' => ['environment' => 'prod']]);;
38+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone_rrset.json')));
39+
$this->rrset->update(['labels' => ['environment' => 'prod']]);
4240
$this->assertLastRequestEquals('PUT', '/zones/4711/rrsets/www/A');
4341
$this->assertLastRequestBodyParametersEqual(['labels' => ['environment' => 'prod']]);
4442
}
4543

4644
public function testChangeProtection()
4745
{
48-
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('change_rrset_protection')));;
46+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('change_rrset_protection')));
4947
$apiResponse = $this->rrset->changeProtection(true);
5048
$this->assertEquals('change_rrset_protection', $apiResponse->action->command);
5149
$this->assertEquals($this->rrset->zone, $apiResponse->action->resources[0]->id);
@@ -77,9 +75,9 @@ public function testSetRecords()
7775
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/set_records');
7876
$this->assertLastRequestBodyParametersEqual(['records' => [
7977
[
80-
"value" => "198.51.100.1",
81-
"comment" => "my webserver at Hetzner Cloud"
82-
]
78+
'value' => '198.51.100.1',
79+
'comment' => 'my webserver at Hetzner Cloud',
80+
],
8381
]]);
8482
}
8583

@@ -95,9 +93,9 @@ public function testAddRecords()
9593
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/add_records');
9694
$this->assertLastRequestBodyParametersEqual(['ttl' => 3600, 'records' => [
9795
[
98-
"value" => "198.51.100.1",
99-
"comment" => "my webserver at Hetzner Cloud"
100-
]
96+
'value' => '198.51.100.1',
97+
'comment' => 'my webserver at Hetzner Cloud',
98+
],
10199
]]);
102100
}
103101

@@ -113,15 +111,14 @@ public function testRemoveRecords()
113111
$this->assertLastRequestEquals('POST', '/zones/4711/rrsets/www/A/actions/remove_records');
114112
$this->assertLastRequestBodyParametersEqual(['records' => [
115113
[
116-
"value" => "198.51.100.1",
117-
"comment" => "my webserver at Hetzner Cloud"
118-
]
114+
'value' => '198.51.100.1',
115+
'comment' => 'my webserver at Hetzner Cloud',
116+
],
119117
]]);
120118
}
121119

122-
123120
protected function getGenericActionResponse(string $command)
124121
{
125-
return str_replace('$command', $command, file_get_contents(__DIR__ . '/fixtures/zone_action_generic.json'));
122+
return str_replace('$command', $command, file_get_contents(__DIR__.'/fixtures/zone_action_generic.json'));
126123
}
127124
}

0 commit comments

Comments
 (0)