22
33namespace LKDev \HetznerCloud \Models \Zones ;
44
5+ use LKDev \HetznerCloud \APIResponse ;
56use LKDev \HetznerCloud \Clients \GuzzleClient ;
67use LKDev \HetznerCloud \HetznerAPIClient ;
8+ use LKDev \HetznerCloud \Models \Actions \Action ;
79use LKDev \HetznerCloud \Models \Contracts \Resource ;
810use LKDev \HetznerCloud \Models \Model ;
911
@@ -20,8 +22,8 @@ class RRSet extends Model implements Resource
2022 public int $ zone ;
2123
2224 /**
23- * @param string $id
24- * @param GuzzleClient|null $client
25+ * @param string $id
26+ * @param GuzzleClient|null $client
2527 */
2628 public function __construct (string $ id , ?GuzzleClient $ client = null )
2729 {
@@ -43,7 +45,6 @@ public function setAdditionalData($data)
4345 $ this ->labels = get_object_vars ($ data ->labels );
4446 $ this ->protection = RRSetProtection::parse ($ data ->protection );
4547 $ this ->zone = $ data ->zone ;
46-
4748 return $ this ;
4849 }
4950
@@ -60,50 +61,160 @@ public function __toRequest(): array
6061 'ttl ' => $ this ->ttl ,
6162 'records ' => $ this ->records ,
6263 ];
63- if (! empty ($ this ->labels )) {
64+ if (!empty ($ this ->labels )) {
6465 $ r ['labels ' ] = $ this ->labels ;
6566 }
6667
6768 return $ r ;
6869 }
6970
71+ /**
72+ * @return RRSet|null
73+ * @throws \LKDev\HetznerCloud\APIException
74+ */
7075 public function reload ()
7176 {
72- return HetznerAPIClient:: $ instance -> zones ()-> getById ($ this ->zone )-> getRRSet ($ this ->id );
77+ return ( new Zone ($ this ->zone ))-> getRRSetById ($ this ->id );
7378 }
7479
75- public function delete ()
80+ /**
81+ * @return APIResponse|null
82+ * @throws \LKDev\HetznerCloud\APIException
83+ */
84+ public function delete (): ?APIResponse
7685 {
77- // TODO: Implement delete() method.
86+ $ response = $ this ->httpClient ->delete ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id );
87+ if (!HetznerAPIClient::hasError ($ response )) {
88+ return APIResponse::create ([
89+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
90+ ], $ response ->getHeaders ());
91+ }
92+
93+ return null ;
7894 }
7995
80- public function update (array $ data )
96+ /**
97+ * @param array $data
98+ * @return APIResponse|null
99+ * @throws \LKDev\HetznerCloud\APIException
100+ */
101+ public function update (array $ data ): ?APIResponse
81102 {
82- // TODO: Implement update() method.
103+ $ response = $ this ->httpClient ->put ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id , [
104+ 'json ' => $ data ,
105+ ]);
106+ if (!HetznerAPIClient::hasError ($ response )) {
107+ return APIResponse::create ([
108+ 'rrset ' => self ::parse (json_decode ((string )$ response ->getBody ())->rrset ),
109+ ], $ response ->getHeaders ());
110+ }
111+
112+ return null ;
83113 }
84114
85- public function changeProtection (RRSetProtection $ protection )
115+ /**
116+ * @param bool $change
117+ * @return APIResponse|null
118+ * @throws \LKDev\HetznerCloud\APIException
119+ */
120+ public function changeProtection (bool $ change ): ?APIResponse
86121 {
87- // TODO: Implement changeProtection() method.
122+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id . '/actions/change_protection ' , [
123+ 'json ' => [
124+ 'change ' => $ change ,
125+ ],
126+ ]);
127+ if (!HetznerAPIClient::hasError ($ response )) {
128+ return APIResponse::create ([
129+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
130+ ], $ response ->getHeaders ());
131+ }
132+ return null ;
88133 }
89134
90- public function changeTTL (int $ ttl )
135+ /**
136+ * @param int $ttl
137+ * @return APIResponse|null
138+ * @throws \LKDev\HetznerCloud\APIException
139+ */
140+ public function changeTTL (int $ ttl ): ?APIResponse
91141 {
92- // TODO: Implement changeTTL() method.
142+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id . '/actions/change_ttl ' , [
143+ 'json ' => [
144+ 'ttl ' => $ ttl ,
145+ ],
146+ ]);
147+ if (!HetznerAPIClient::hasError ($ response )) {
148+ return APIResponse::create ([
149+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
150+ ], $ response ->getHeaders ());
151+ }
152+
153+ return null ;
93154 }
94155
95- public function setRecords (array $ records )
156+ /**
157+ * @param array<Record> $records
158+ * @return APIResponse|null
159+ * @throws \LKDev\HetznerCloud\APIException
160+ */
161+ public function setRecords (array $ records ): ?APIResponse
96162 {
97- // TODO: Implement setRecords() method.
163+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id . '/actions/set_records ' , [
164+ 'json ' => [
165+ 'records ' => $ records ,
166+ ],
167+ ]);
168+ if (!HetznerAPIClient::hasError ($ response )) {
169+ return APIResponse::create ([
170+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
171+ ], $ response ->getHeaders ());
172+ }
173+
174+ return null ;
98175 }
99176
100- public function addRecords (array $ records )
177+ /**
178+ * @param array<Record> $records
179+ * @param int|null $ttl
180+ * @return APIResponse|null
181+ * @throws \LKDev\HetznerCloud\APIException
182+ */
183+ public function addRecords (array $ records , ?int $ ttl = null ): ?APIResponse
101184 {
102- // TODO: Implement addRecords() method.
185+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id . '/actions/add_records ' , [
186+ 'json ' => [
187+ 'records ' => $ records ,
188+ 'ttl ' => $ ttl ,
189+ ],
190+ ]);
191+ if (!HetznerAPIClient::hasError ($ response )) {
192+ return APIResponse::create ([
193+ 'action ' => Action::parse (json_decode ((string )$ response ->getBody ())->action ),
194+ ], $ response ->getHeaders ());
195+ }
196+
197+ return null ;
103198 }
104199
200+ /**
201+ * @param array<Record> $records
202+ * @return APIResponse|null
203+ * @throws \LKDev\HetznerCloud\APIException
204+ */
105205 public function removeRecords (array $ records )
106206 {
107- // TODO: Implement removeRecords() method.
207+ $ response = $ this ->httpClient ->post ('zones/ ' . $ this ->zone . '/rrsets/ ' . $ this ->id . '/actions/remove_records ' , [
208+ 'json ' => [
209+ 'records ' => $ records ,
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 ;
108219 }
109220}
0 commit comments