Skip to content

Commit 88dd153

Browse files
committed
WIP
1 parent 3e6d535 commit 88dd153

File tree

5 files changed

+124
-20
lines changed

5 files changed

+124
-20
lines changed

src/HetznerAPIClient.php

Lines changed: 1 addition & 0 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\RRSet;
2425
use LKDev\HetznerCloud\Models\Zones\Zones;
2526
use Psr\Http\Message\ResponseInterface;
2627

src/Models/Zones/RRSet.php

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace LKDev\HetznerCloud\Models\Zones;
44

5-
class RRSet
5+
use LKDev\HetznerCloud\Clients\GuzzleClient;
6+
use LKDev\HetznerCloud\HetznerAPIClient;
7+
use LKDev\HetznerCloud\Models\Contracts\Resource;
8+
use LKDev\HetznerCloud\Models\Model;
9+
10+
class RRSet extends Model implements Resource
611
{
712
public string $id;
813
public string $name;
@@ -12,29 +17,38 @@ class RRSet
1217
public array $labels;
1318
public ?RRSetProtection $protection;
1419

20+
public int $zone;
21+
1522
/**
16-
* @param string $id
17-
* @param string $name
18-
* @param string $type
19-
* @param int $ttl
20-
* @param array $records
21-
* @param array|null $labels
22-
* @param RRSetProtection|null $protection
23+
* @param string $id
24+
* @param GuzzleClient|null $client
2325
*/
24-
public function __construct(string $id, string $name, string $type, int $ttl, array $records, ?array $labels, ?RRSetProtection $protection)
26+
public function __construct(string $id, ?GuzzleClient $client = null)
2527
{
2628
$this->id = $id;
27-
$this->name = $name;
28-
$this->type = $type;
29-
$this->ttl = $ttl;
30-
$this->records = $records;
31-
$this->labels = $labels;
32-
$this->protection = $protection;
29+
30+
parent::__construct($client);
31+
}
32+
33+
/**
34+
* @param $data
35+
* @return \LKDev\HetznerCloud\Models\Zones\RRSet
36+
*/
37+
public function setAdditionalData($data)
38+
{
39+
$this->name = $data->name;
40+
$this->type = $data->type;
41+
$this->ttl = $data->ttl;
42+
$this->records = $data->records;
43+
$this->labels = get_object_vars($data->labels);
44+
$this->protection = RRSetProtection::parse($data->protection);
45+
$this->zone = $data->zone;
46+
return $this;
3347
}
3448

35-
public static function fromResponse(array $data): RRSet
49+
public static function parse($input): RRSet
3650
{
37-
return new self($data['id'], $data['name'], $data['type'], $data['ttl'], $data['records'], get_object_vars($data['labels']), RRSetProtection::parse($data['protection']));
51+
return (new self($input->id))->setAdditionalData($input);
3852
}
3953

4054
public function __toRequest(): array
@@ -45,10 +59,50 @@ public function __toRequest(): array
4559
'ttl' => $this->ttl,
4660
'records' => $this->records,
4761
];
48-
if (! empty($this->labels)) {
62+
if (!empty($this->labels)) {
4963
$r['labels'] = $this->labels;
5064
}
5165

5266
return $r;
5367
}
68+
69+
public function reload()
70+
{
71+
return HetznerAPIClient::$instance->zones()->getById($this->zone)->getRRSet($this->id);
72+
}
73+
74+
public function delete()
75+
{
76+
// TODO: Implement delete() method.
77+
}
78+
79+
public function update(array $data)
80+
{
81+
// TODO: Implement update() method.
82+
}
83+
84+
public function changeProtection(RRSetProtection $protection)
85+
{
86+
// TODO: Implement changeProtection() method.
87+
}
88+
89+
public function changeTTL(int $ttl)
90+
{
91+
// TODO: Implement changeTTL() method.
92+
}
93+
94+
public function setRecords(array $records)
95+
{
96+
// TODO: Implement setRecords() method.
97+
}
98+
99+
public function addRecords(array $records)
100+
{
101+
// TODO: Implement addRecords() method.
102+
}
103+
104+
public function removeRecords(array $records)
105+
{
106+
// TODO: Implement removeRecords() method.
107+
}
54108
}

src/Models/Zones/Zone.php

Lines changed: 17 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::fromResponse(get_object_vars($rrset));
325+
$rrsets[] = RRSet::parse(get_object_vars($rrset));
326326
}
327327

328328
return APIResponse::create([
@@ -367,10 +367,25 @@ 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::fromResponse(get_object_vars($payload->rrset)),
370+
'rrset' => RRSet::parse(get_object_vars($payload->rrset)),
371371
], $response->getHeaders());
372372
}
373373

374374
return null;
375375
}
376+
377+
/**
378+
* @param string $id
379+
* @return RRSet|null
380+
* @throws APIException
381+
*/
382+
public function getRRSetById(string $id): ?RRSet
383+
{
384+
$response = $this->httpClient->get('zones/' . $this->id . "/rrsets/" . $id);;
385+
if (!HetznerAPIClient::hasError($response)) {
386+
return RRSet::parse(json_decode((string)$response->getBody())->rrset);
387+
}
388+
389+
return null;
390+
}
376391
}

tests/Unit/Models/Zones/ZoneTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ public function testCreateRRSet()
138138
'records' => [['value' => '198.51.100.1', 'comment' => 'my webserver at Hetzner Cloud']]]);
139139
}
140140

141+
public function testGetById()
142+
{
143+
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__.'/fixtures/zone_rrset.json')));
144+
$rrset = $this->zone->getRRSetById("www/A");
145+
$this->assertEquals($rrset->id, "www/A");
146+
$this->assertEquals($rrset->name, 'www');
147+
148+
$this->assertLastRequestEquals('GET', '/zones/4711/rrsets/www/A');
149+
}
150+
151+
141152
protected function getGenericActionResponse(string $command)
142153
{
143154
return str_replace('$command', $command, file_get_contents(__DIR__.'/fixtures/zone_action_generic.json'));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"rrset": {
3+
"id": "www/A",
4+
"name": "www",
5+
"type": "A",
6+
"ttl": 3600,
7+
"labels": {
8+
"environment": "prod",
9+
"example.com/my": "label",
10+
"just-a-key": ""
11+
},
12+
"protection": {
13+
"change": false
14+
},
15+
"records": [
16+
{
17+
"value": "198.51.100.1",
18+
"comment": "My web server at Hetzner Cloud."
19+
}
20+
],
21+
"zone": 4711
22+
}
23+
}

0 commit comments

Comments
 (0)