Skip to content

Commit 2143ca2

Browse files
committed
Implement remaining zone actions
1 parent 7b7b8c7 commit 2143ca2

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

src/Models/Zones/Zone.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,50 @@ public function changeProtection(bool $delete = true): ?APIResponse
190190
return null;
191191
}
192192

193+
public function exportZonefile(): ?APIResponse
194+
{
195+
$response = $this->httpClient->get('zones/' . $this->id . '/zonefile');
196+
if (!HetznerAPIClient::hasError($response)) {
197+
return APIResponse::create([
198+
'zonefile' => json_decode((string)$response->getBody())->zonefile,
199+
], $response->getHeaders());
200+
}
201+
202+
return null;
203+
}
204+
205+
public function changeTTL(int $ttl): ?APIResponse
206+
{
207+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/change_ttl', [
208+
'json' => [
209+
'ttl' => $ttl,
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;
219+
}
220+
221+
public function importZonefile(string $zonefile): ?APIResponse
222+
{
223+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/import_zonefile', [
224+
'json' => [
225+
'zonefile' => $zonefile,
226+
],
227+
]);
228+
if (!HetznerAPIClient::hasError($response)) {
229+
return APIResponse::create([
230+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
231+
], $response->getHeaders());
232+
}
233+
234+
return null;
235+
}
236+
193237
/**
194238
* @param string $uri
195239
* @return string
@@ -199,6 +243,7 @@ protected function replaceZoneIdInUri(string $uri): string
199243
return str_replace('{id}', $this->id, $uri);
200244
}
201245

246+
202247
/**
203248
* @param $input
204249
* @return \LKDev\HetznerCloud\Models\Zones\Zone|static |null
@@ -211,4 +256,24 @@ public static function parse($input)
211256

212257
return (new self($input->id))->setAdditionalData($input);
213258
}
259+
260+
/**
261+
* @param array<PrimaryNameserver> $primary_nameservers
262+
* @return void#
263+
*/
264+
public function changePrimaryNameservers(array $primary_nameservers)
265+
{
266+
$response = $this->httpClient->post('zones/' . $this->id . '/actions/change_primary_nameservers', [
267+
'json' => [
268+
'primary_nameservers' => $primary_nameservers,
269+
],
270+
]);
271+
if (!HetznerAPIClient::hasError($response)) {
272+
return APIResponse::create([
273+
'action' => Action::parse(json_decode((string)$response->getBody())->action),
274+
], $response->getHeaders());
275+
}
276+
277+
return null;
278+
}
214279
}

tests/Unit/Models/Zones/ZoneTest.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LKDev\Tests\Unit\Models\Zones;
44

55
use GuzzleHttp\Psr7\Response;
6+
use LKDev\HetznerCloud\Models\Zones\PrimaryNameserver;
67
use LKDev\HetznerCloud\Models\Zones\Zone;
78
use LKDev\HetznerCloud\Models\Zones\Zones;
89
use LKDev\Tests\TestCase;
@@ -19,7 +20,7 @@ public function setUp(): void
1920
parent::setUp();
2021
$tmp = new Zones($this->hetznerApi->getHttpClient());
2122

22-
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone.json')));
23+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone.json')));
2324
$this->zone = $tmp->getById(4711);
2425
}
2526

@@ -36,15 +37,15 @@ public function testDelete()
3637

3738
public function testUpdate()
3839
{
39-
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone.json')));
40+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone.json')));
4041
$this->zone->update(['name' => 'new-name']);
4142
$this->assertLastRequestEquals('PUT', '/zones/4711');
4243
$this->assertLastRequestBodyParametersEqual(['name' => 'new-name']);
4344
}
4445

4546
public function testChangeProtection()
4647
{
47-
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone_action_change_protection.json')));
48+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone_action_change_protection.json')));
4849
$apiResponse = $this->zone->changeProtection(true);
4950
$this->assertEquals('change_protection', $apiResponse->action->command);
5051
$this->assertEquals($this->zone->id, $apiResponse->action->resources[0]->id);
@@ -53,8 +54,52 @@ public function testChangeProtection()
5354
$this->assertLastRequestBodyParametersEqual(['delete' => true]);
5455
}
5556

57+
public function testChangeTTL()
58+
{
59+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('change_ttl')));
60+
$apiResponse = $this->zone->changeTTL(50);
61+
$this->assertEquals('change_ttl', $apiResponse->action->command);
62+
$this->assertEquals($this->zone->id, $apiResponse->action->resources[0]->id);
63+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
64+
$this->assertLastRequestEquals('POST', '/zones/4711/actions/change_ttl');
65+
$this->assertLastRequestBodyParametersEqual(['ttl' => 50]);
66+
}
67+
68+
public function testImportZonefile()
69+
{
70+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('import_zonefile')));
71+
$apiResponse = $this->zone->importZonefile("zonefile_content");
72+
$this->assertEquals('import_zonefile', $apiResponse->action->command);
73+
$this->assertEquals($this->zone->id, $apiResponse->action->resources[0]->id);
74+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
75+
$this->assertLastRequestEquals('POST', '/zones/4711/actions/import_zonefile');
76+
$this->assertLastRequestBodyParametersEqual(['zonefile' => "zonefile_content"]);
77+
}
78+
79+
public function testTestChangePrimaryNameservers()
80+
{
81+
$this->mockHandler->append(new Response(200, [], $this->getGenericActionResponse('import_zonefile')));
82+
$apiResponse = $this->zone->changePrimaryNameservers([
83+
new PrimaryNameserver("192.168.178.1", 53)
84+
]);
85+
$this->assertEquals('import_zonefile', $apiResponse->action->command);
86+
$this->assertEquals($this->zone->id, $apiResponse->action->resources[0]->id);
87+
$this->assertEquals('zone', $apiResponse->action->resources[0]->type);
88+
$this->assertLastRequestEquals('POST', '/zones/4711/actions/change_primary_nameservers');
89+
$this->assertLastRequestBodyParametersEqual(['primary_nameservers' => [["address" => "192.168.178.1", "port" => 53]]]);
90+
}
91+
92+
93+
public function testExportZonefile()
94+
{
95+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/fixtures/zone_zonefile.json')));
96+
$apiResponse = $this->zone->exportZonefile();
97+
$this->assertNotEmpty($apiResponse->zonefile);
98+
$this->assertLastRequestEquals('GET', '/zones/4711/zonefile');
99+
}
100+
56101
protected function getGenericActionResponse(string $command)
57102
{
58-
return str_replace('$command', $command, file_get_contents(__DIR__.'/fixtures/zone_action_generic.json'));
103+
return str_replace('$command', $command, file_get_contents(__DIR__ . '/fixtures/zone_action_generic.json'));
59104
}
60105
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"zonefile": "$ORIGIN\texample.com.\n$TTL\t3600\n\n@\tIN\tSOA\thydrogen.ns.hetzner.com. dns.hetzner.com. 2024010100 86400 10800 3600000 3600\n\n@\tIN\t10800\tNS\thydrogen.ns.hetzner.com. ; Some comment.\n@\tIN\t10800\tNS\toxygen.ns.hetzner.com.\n@\tIN\t10800\tNS\thelium.ns.hetzner.de.\n"
3+
}

0 commit comments

Comments
 (0)