22
33namespace LKDev \HetznerCloud \Models \Zones ;
44
5+ use LKDev \HetznerCloud \APIException ;
56use LKDev \HetznerCloud \APIResponse ;
67use LKDev \HetznerCloud \Clients \GuzzleClient ;
78use LKDev \HetznerCloud \HetznerAPIClient ;
89use LKDev \HetznerCloud \Models \Actions \Action ;
910use LKDev \HetznerCloud \Models \Contracts \Resource ;
11+ use LKDev \HetznerCloud \Models \Meta ;
1012use LKDev \HetznerCloud \Models \Model ;
1113use LKDev \HetznerCloud \Models \Protection ;
14+ use LKDev \HetznerCloud \RequestOpts ;
1215
1316class Zone extends Model implements Resource
1417{
@@ -72,8 +75,8 @@ class Zone extends Model implements Resource
7275 public AuthoritativeNameservers $ authoritative_nameservers ;
7376
7477 /**
75- * @param int $zoneId
76- * @param GuzzleClient|null $httpClient
78+ * @param int $zoneId
79+ * @param GuzzleClient|null $httpClient
7780 */
7881 public function __construct (int $ zoneId , ?GuzzleClient $ httpClient = null )
7982 {
@@ -131,9 +134,9 @@ public function reload()
131134 public function delete (): ?APIResponse
132135 {
133136 $ response = $ this ->httpClient ->delete ($ this ->replaceZoneIdInUri ('zones/{id} ' ));
134- if (! HetznerAPIClient::hasError ($ response )) {
137+ if (!HetznerAPIClient::hasError ($ response )) {
135138 return APIResponse::create ([
136- 'action ' => Action::parse (json_decode ((string ) $ response ->getBody ())->action ),
139+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
137140 ], $ response ->getHeaders ());
138141 }
139142
@@ -145,7 +148,7 @@ public function delete(): ?APIResponse
145148 *
146149 * @see https://docs.hetzner.cloud/reference/cloud#zones-update-a-zone
147150 *
148- * @param array $data
151+ * @param array $data
149152 * @return APIResponse|null
150153 *
151154 * @throws \LKDev\HetznerCloud\APIException
@@ -155,9 +158,9 @@ public function update(array $data)
155158 $ response = $ this ->httpClient ->put ($ this ->replaceZoneIdInUri ('zones/{id} ' ), [
156159 'json ' => $ data ,
157160 ]);
158- if (! HetznerAPIClient::hasError ($ response )) {
161+ if (!HetznerAPIClient::hasError ($ response )) {
159162 return APIResponse::create ([
160- 'zone ' => self ::parse (json_decode ((string ) $ response ->getBody ())->zone ),
163+ 'zone ' => self ::parse (json_decode ((string )$ response ->getBody ())->zone ),
161164 ], $ response ->getHeaders ());
162165 }
163166
@@ -169,21 +172,21 @@ public function update(array $data)
169172 *
170173 * @see https://docs.hetzner.cloud/#zone-actions-change-zone-protection
171174 *
172- * @param bool $delete
175+ * @param bool $delete
173176 * @return APIResponse|null
174177 *
175178 * @throws \LKDev\HetznerCloud\APIException
176179 */
177180 public function changeProtection (bool $ delete = true ): ?APIResponse
178181 {
179- $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_protection ' , [
182+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_protection ' , [
180183 'json ' => [
181184 'delete ' => $ delete ,
182185 ],
183186 ]);
184- if (! HetznerAPIClient::hasError ($ response )) {
187+ if (!HetznerAPIClient::hasError ($ response )) {
185188 return APIResponse::create ([
186- 'action ' => Action::parse (json_decode ((string ) $ response ->getBody ())->action ),
189+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
187190 ], $ response ->getHeaders ());
188191 }
189192
@@ -192,10 +195,10 @@ public function changeProtection(bool $delete = true): ?APIResponse
192195
193196 public function exportZonefile (): ?APIResponse
194197 {
195- $ response = $ this ->httpClient ->get ('zones/ ' . $ this ->id . '/zonefile ' );
196- if (! HetznerAPIClient::hasError ($ response )) {
198+ $ response = $ this ->httpClient ->get ('zones/ ' . $ this ->id . '/zonefile ' );
199+ if (!HetznerAPIClient::hasError ($ response )) {
197200 return APIResponse::create ([
198- 'zonefile ' => json_decode ((string ) $ response ->getBody ())->zonefile ,
201+ 'zonefile ' => json_decode ((string )$ response ->getBody ())->zonefile ,
199202 ], $ response ->getHeaders ());
200203 }
201204
@@ -204,14 +207,14 @@ public function exportZonefile(): ?APIResponse
204207
205208 public function changeTTL (int $ ttl ): ?APIResponse
206209 {
207- $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_ttl ' , [
210+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_ttl ' , [
208211 'json ' => [
209212 'ttl ' => $ ttl ,
210213 ],
211214 ]);
212- if (! HetznerAPIClient::hasError ($ response )) {
215+ if (!HetznerAPIClient::hasError ($ response )) {
213216 return APIResponse::create ([
214- 'action ' => Action::parse (json_decode ((string ) $ response ->getBody ())->action ),
217+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
215218 ], $ response ->getHeaders ());
216219 }
217220
@@ -220,22 +223,22 @@ public function changeTTL(int $ttl): ?APIResponse
220223
221224 public function importZonefile (string $ zonefile ): ?APIResponse
222225 {
223- $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/import_zonefile ' , [
226+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/import_zonefile ' , [
224227 'json ' => [
225228 'zonefile ' => $ zonefile ,
226229 ],
227230 ]);
228- if (! HetznerAPIClient::hasError ($ response )) {
231+ if (!HetznerAPIClient::hasError ($ response )) {
229232 return APIResponse::create ([
230- 'action ' => Action::parse (json_decode ((string ) $ response ->getBody ())->action ),
233+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
231234 ], $ response ->getHeaders ());
232235 }
233236
234237 return null ;
235238 }
236239
237240 /**
238- * @param string $uri
241+ * @param string $uri
239242 * @return string
240243 */
241244 protected function replaceZoneIdInUri (string $ uri ): string
@@ -257,19 +260,110 @@ public static function parse($input)
257260 }
258261
259262 /**
260- * @param array<PrimaryNameserver> $primary_nameservers
263+ * @param array<PrimaryNameserver> $primary_nameservers
261264 * @return void#
265+ * @throws APIException
262266 */
263267 public function changePrimaryNameservers (array $ primary_nameservers )
264268 {
265- $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_primary_nameservers ' , [
269+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/actions/change_primary_nameservers ' , [
266270 'json ' => [
267271 'primary_nameservers ' => $ primary_nameservers ,
268272 ],
269273 ]);
270- if (! HetznerAPIClient::hasError ($ response )) {
274+ if (!HetznerAPIClient::hasError ($ response )) {
271275 return APIResponse::create ([
272- 'action ' => Action::parse (json_decode ((string ) $ response ->getBody ())->action ),
276+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
277+ ], $ response ->getHeaders ());
278+ }
279+
280+ return null ;
281+ }
282+
283+ /**
284+ * @param RRSetRequestOpts|null $requestOpts
285+ * @return array<RRSet>
286+ * @throws APIException
287+ */
288+ public function allRRSets (?RRSetRequestOpts $ requestOpts = null ): array
289+ {
290+ if ($ requestOpts == null ) {
291+ $ requestOpts = new RRSetRequestOpts ();
292+ }
293+ $ entities = [];
294+ $ requestOpts ->per_page = HetznerAPIClient::MAX_ENTITIES_PER_PAGE ;
295+ $ max_pages = PHP_INT_MAX ;
296+ for ($ i = 1 ; $ i < $ max_pages ; $ i ++) {
297+ $ requestOpts ->page = $ i ;
298+ $ _f = $ this ->listRRSets ($ requestOpts );
299+ $ entities = array_merge ($ entities , $ _f ->rrsets );
300+ if ($ _f ->meta ->pagination ->page === $ _f ->meta ->pagination ->last_page || $ _f ->meta ->pagination ->last_page === null ) {
301+ $ max_pages = 0 ;
302+ }
303+ }
304+
305+ return $ entities ;
306+ }
307+
308+ /**
309+ * @param RRSetRequestOpts|null $requestOpts
310+ * @return APIResponse|null
311+ * @throws \LKDev\HetznerCloud\APIException
312+ */
313+ public function listRRSets (?RRSetRequestOpts $ requestOpts = null ): ?APIResponse
314+ {
315+ if ($ requestOpts == null ) {
316+ $ requestOpts = new RRSetRequestOpts ();
317+ }
318+ $ response = $ this ->httpClient ->get ('zones/ ' . $ this ->id . "/rrsets " . $ requestOpts ->buildQuery ());
319+ if (!HetznerAPIClient::hasError ($ response )) {
320+ $ resp = json_decode ((string )$ response ->getBody ());
321+ $ rrsets = [];
322+ foreach ($ resp ->rrsets as $ rrset ) {
323+ $ rrsets [] = RRSet::fromResponse (get_object_vars ($ rrset ));
324+ }
325+ return APIResponse::create ([
326+ 'meta ' => Meta::parse ($ resp ->meta ),
327+ 'rrsets ' => $ rrsets ,
328+ ], $ response ->getHeaders ());
329+ }
330+
331+ return null ;
332+ }
333+
334+ /**
335+ * @param string $name
336+ * @param string $type
337+ * @param array<Record> $records
338+ * @param int|null $ttl
339+ * @param array|null $labels
340+ * @return void
341+ * @throws APIException
342+ */
343+ public function createRRSet (string $ name , string $ type , array $ records , ?int $ ttl = null , ?array $ labels = [])
344+ {
345+ $ parameters = [
346+ 'name ' => $ name ,
347+ 'type ' => $ type ,
348+ 'records ' => $ records ,
349+ ];
350+ if ($ ttl !== null ) {
351+ $ parameters ['ttl ' ] = $ ttl ;
352+ }
353+ if (!empty ($ labels )) {
354+ $ parameters ['labels ' ] = $ labels ;
355+ }
356+
357+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->id . '/rrsets ' , [
358+ 'json ' => $ parameters ,
359+ ]);
360+
361+ if (!HetznerAPIClient::hasError ($ response )) {
362+ $ payload = json_decode ((string )$ response ->getBody ());
363+
364+ return APIResponse::create ([
365+ 'action ' => Action::parse ($ payload ->action ),
366+ 'rrset ' => RRSet::fromResponse (get_object_vars ($ payload ->rrset )),
273367 ], $ response ->getHeaders ());
274368 }
275369
0 commit comments