77 */
88
99namespace LKDev \HetznerCloud \Models \Servers ;
10-
10+ use LKDev \HetznerCloud \HetznerAPIClient ;
11+ use LKDev \HetznerCloud \Models \Images \Image ;
12+ use LKDev \HetznerCloud \Models \ISOs \ISO ;
1113use LKDev \HetznerCloud \Models \Model ;
14+ use LKDev \HetznerCloud \Models \Servers \Types \ServerType ;
1215
16+ /**
17+ *
18+ */
1319class Server extends Model
1420{
21+ /**
22+ * @var int
23+ */
24+ public $ id ;
25+
26+ /**
27+ *
28+ *
29+ * @param int $serverId
30+ * @param HetznerAPIClient $hetznerAPIClient
31+ * @param \LKDev\HetznerCloud\Clients\GuzzleClient $httpClient
32+ */
33+ public function __construct (
34+ int $ serverId ,
35+ HetznerAPIClient $ hetznerAPIClient ,
36+ $ httpClient = null
37+ ) {
38+ $ this ->id = $ serverId ;
39+ parent ::__construct ($ hetznerAPIClient , $ httpClient );
40+ }
41+
42+ /**
43+ * @return bool
44+ */
45+ public function powerOn (): bool
46+ {
47+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/poweron ' ));
48+ }
49+
50+ /**
51+ * @return bool
52+ */
53+ public function softReboot (): bool
54+ {
55+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/reboot ' ));
56+ }
57+
58+ /**
59+ * @return bool
60+ */
61+ public function reset (): bool
62+ {
63+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/reset ' ));
64+ }
65+
66+ /**
67+ * @return bool
68+ */
69+ public function shutdown (): bool
70+ {
71+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/shutdown ' ));
72+ }
73+
74+ /**
75+ * @return bool
76+ */
77+ public function powerOff (): bool
78+ {
79+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/powerOff ' ));
80+ }
81+
82+ /**
83+ * @return string
84+ */
85+ public function resetRootPassword (): string
86+ {
87+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/reset_password ' ));
88+ }
89+
90+ /**
91+ * @param string $type
92+ * @param array $ssh_keys
93+ * @return bool
94+ */
95+ public function enableRescue ($ type = 'linux64 ' , $ ssh_keys = []): bool
96+ {
97+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/enable_rescue ' ), [
98+ 'form_params ' => [
99+ 'type ' => $ type ,
100+ 'ssh_keys ' => $ ssh_keys ,
101+ ],
102+ ]);
103+ }
104+
105+ /**
106+ * @return bool
107+ */
108+ public function disableRescue (): bool
109+ {
110+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/disable_rescue ' ));
111+ }
112+
113+ /**
114+ * @param string $description
115+ * @param string $type
116+ * @return \LKDev\HetznerCloud\Models\Images\Image
117+ */
118+ public function createImage (string $ description = '' , string $ type = 'snapshot ' ): Image
119+ {
120+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/create_image ' ), [
121+ 'form_params ' => [
122+ 'description ' => $ description ,
123+ 'type ' => $ type ,
124+ ],
125+ ]);
126+ }
127+
128+ /**
129+ * @param \LKDev\HetznerCloud\Models\Images\Image $image
130+ * @return bool
131+ */
132+ public function rebuildFromImage (Image $ image ): bool
133+ {
134+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/rebuild ' ), [
135+ 'form_params ' => [
136+ 'image ' => $ image ->id ,
137+ ],
138+ ]);
139+ }
140+
141+ /**
142+ * @param \LKDev\HetznerCloud\Models\Servers\Types\ServerType $serverType
143+ * @param bool $upgradeDisk
144+ * @return bool
145+ */
146+ public function changeType (ServerType $ serverType , bool $ upgradeDisk = false ): bool
147+ {
148+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/change_type ' ), [
149+ 'form_params ' => [
150+ 'server_type ' => $ serverType ->id ,
151+ 'upgrade_disk ' => $ upgradeDisk ,
152+ ],
153+ ]);
154+ }
155+
156+ /**
157+ * @param string|null $backupWindow
158+ * @return bool
159+ */
160+ public function enableBackups (string $ backupWindow = null ): bool
161+ {
162+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/enable_backup ' ), [
163+ 'form_params ' => [
164+ 'backup_window ' => $ backupWindow ,
165+ ],
166+ ]);
167+ }
168+
169+ /**
170+ * @return bool
171+ */
172+ public function disableBackups (): bool
173+ {
174+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/disable_backup ' ));
175+ }
176+
177+ /**
178+ * @param \LKDev\HetznerCloud\Models\ISOs\ISO $iso
179+ */
180+ public function attachISO (ISO $ iso )
181+ {
182+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/attach_iso ' ), [
183+ 'form_params ' => [
184+ 'iso ' => $ iso ->id ,
185+ ],
186+ ]);
187+ }
188+
189+ /**
190+ * @return bool
191+ */
192+ public function dettachISO (): bool
193+ {
194+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/detach_iso ' ));
195+ }
196+
197+ /**
198+ * @param string $ip
199+ * @param string $dnsPtr
200+ * @return bool
201+ */
202+ public function changeReverseDNS (string $ ip , string $ dnsPtr ): bool
203+ {
204+ $ this ->httpClient ->post ($ this ->replaceServerIdInUri ('servers/{id}/actions/change_dns_ptr ' ), [
205+ 'form_params ' => [
206+ 'ip ' => $ ip ,
207+ 'dns_ptr ' => $ dnsPtr ,
208+ ],
209+ ]);
210+ }
211+
212+ public function metrics (string $ type , string $ start , string $ end , int $ step = null )
213+ {
214+ $ this ->httpClient ->get ($ this ->replaceServerIdInUri ('servers/{id}/metrics? ' ).http_build_query (compact ('type ' , 'start ' , 'end ' , 'step ' )));
215+ }
216+
217+ /**
218+ * @return bool
219+ */
220+ public function delete (): bool
221+ {
222+ $ this ->httpClient ->delete ($ this ->replaceServerIdInUri ('servers/{id} ' ));
223+ }
224+
225+ /**
226+ * @param string $name
227+ */
228+ public function changeName (string $ name )
229+ {
230+ $ this ->httpClient ->put ($ this ->replaceServerIdInUri ('servers/{id} ' ), [
231+ 'form_params ' => [
232+ 'name ' => $ name ,
233+ ],
234+ ]);
235+ }
236+
237+ /**
238+ * @param string $uri
239+ * @return string
240+ */
241+ protected function replaceServerIdInUri (string $ uri ): string
242+ {
243+ return str_replace ('{id} ' , $ this ->id , $ uri );
244+ }
15245}
0 commit comments