-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathRRSet.php
More file actions
253 lines (226 loc) · 7.09 KB
/
RRSet.php
File metadata and controls
253 lines (226 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace LKDev\HetznerCloud\Models\Zones;
use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\Clients\GuzzleClient;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Actions\Action;
use LKDev\HetznerCloud\Models\Contracts\Resource;
use LKDev\HetznerCloud\Models\Model;
class RRSet extends Model implements Resource
{
public string $id;
public string $name;
public string $type;
public int $ttl;
public array $records;
public array $labels;
public ?RRSetProtection $protection;
public int $zone;
/**
* Creates a new RRSet. This is useful if you want to create a new RRSet to pass to the createRRSet method of a Zone.
*
* @param string $name
* @param string $type
* @param array $records
* @param int|null $ttl
* @param array|null $labels
* @return RRSet|null
*/
public static function create(string $name, string $type, array $records, ?int $ttl = null, ?array $labels = []): ?RRSet
{
return (new RRSet(''))->setAdditionalData((object) [
'name' => $name,
'type' => $type,
'ttl' => $ttl,
'records' => $records,
'labels' => (object) $labels,
'protection' => (object) [],
'zone' => 0,
]);
}
/**
* @param string $id
* @param GuzzleClient|null $client
*/
public function __construct(string $id, ?GuzzleClient $client = null)
{
$this->id = $id;
parent::__construct($client);
}
/**
* @param $data
* @return \LKDev\HetznerCloud\Models\Zones\RRSet
*/
public function setAdditionalData($data)
{
$this->name = $data->name;
$this->type = $data->type;
$this->ttl = $data->ttl;
$this->records = $data->records;
$this->labels = get_object_vars($data->labels);
$this->protection = RRSetProtection::parse($data->protection);
$this->zone = $data->zone;
return $this;
}
public static function parse($input): RRSet
{
return (new self($input->id))->setAdditionalData($input);
}
public function __toRequest(): array
{
$r = [
'name' => $this->name,
'type' => $this->type,
'ttl' => $this->ttl,
'records' => $this->records,
];
if (! empty($this->labels)) {
$r['labels'] = $this->labels;
}
return $r;
}
/**
* @return RRSet|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function reload()
{
return (new Zone($this->zone))->getRRSetById($this->id);
}
/**
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function delete(): ?APIResponse
{
$response = $this->httpClient->delete('zones/'.$this->zone.'/rrsets/'.$this->id);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
/**
* @param array $data
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function update(array $data): ?APIResponse
{
$response = $this->httpClient->put('zones/'.$this->zone.'/rrsets/'.$this->id, [
'json' => $data,
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'rrset' => self::parse(json_decode((string) $response->getBody())->rrset),
], $response->getHeaders());
}
return null;
}
/**
* @param bool $change
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function changeProtection(bool $change): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_protection', [
'json' => [
'change' => $change,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
/**
* @param int $ttl
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function changeTTL(int $ttl): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/change_ttl', [
'json' => [
'ttl' => $ttl,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
/**
* @param array<Record> $records
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function setRecords(array $records): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/set_records', [
'json' => [
'records' => $records,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
/**
* @param array<Record> $records
* @param int|null $ttl
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function addRecords(array $records, ?int $ttl = null): ?APIResponse
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/add_records', [
'json' => [
'records' => $records,
'ttl' => $ttl,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
/**
* @param array<Record> $records
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function removeRecords(array $records)
{
$response = $this->httpClient->post('zones/'.$this->zone.'/rrsets/'.$this->id.'/actions/remove_records', [
'json' => [
'records' => $records,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return APIResponse::create([
'action' => Action::parse(json_decode((string) $response->getBody())->action),
], $response->getHeaders());
}
return null;
}
}