Skip to content

Commit 057e3da

Browse files
committed
Implement Basic Server Requests without any knowledge about the response
1 parent a395fac commit 057e3da

6 files changed

Lines changed: 341 additions & 16 deletions

File tree

src/Models/ISOs/ISO.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\ISOs;
4+
5+
class ISO
6+
{
7+
}

src/Models/Servers/Server.php

Lines changed: 231 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,239 @@
77
*/
88

99
namespace LKDev\HetznerCloud\Models\Servers;
10-
10+
use LKDev\HetznerCloud\HetznerAPIClient;
11+
use LKDev\HetznerCloud\Models\Images\Image;
12+
use LKDev\HetznerCloud\Models\ISOs\ISO;
1113
use LKDev\HetznerCloud\Models\Model;
14+
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
1215

16+
/**
17+
*
18+
*/
1319
class 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
}

src/Models/Servers/ServerType.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Models/Servers/Servers.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,65 @@
88

99
namespace LKDev\HetznerCloud\Models\Servers;
1010

11+
use LKDev\HetznerCloud\Models\Datacenters\Datacenter;
12+
use LKDev\HetznerCloud\Models\Images\Image;
13+
use LKDev\HetznerCloud\Models\Locations\Location;
1114
use LKDev\HetznerCloud\Models\Model;
15+
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
1216

17+
/**
18+
*
19+
*/
1320
class Servers extends Model
1421
{
15-
16-
public function all(){
22+
/**
23+
* @return array
24+
*/
25+
public function all(): array
26+
{
1727
$this->httpClient->get('servers');
28+
}
1829

30+
/**
31+
* @param int $serverId
32+
* @return \LKDev\HetznerCloud\Models\Servers\Server
33+
*/
34+
public function get(int $serverId): Server
35+
{
36+
$this->httpClient->get('servers/'.$serverId);
37+
}
38+
/**
39+
* @param string $name
40+
* @param \LKDev\HetznerCloud\Models\Servers\Types\ServerType $serverType
41+
* @param \LKDev\HetznerCloud\Models\Datacenters\Datacenter $datacenter
42+
* @param \LKDev\HetznerCloud\Models\Locations\Location $location
43+
* @param \LKDev\HetznerCloud\Models\Images\Image $image
44+
* @param bool $startAfterCreate
45+
* @param string $user_data
46+
* @param array $ssh_keys
47+
* @return \LKDev\HetznerCloud\Models\Servers\Server
48+
*/
49+
public function create(
50+
string $name,
51+
ServerType $serverType,
52+
Datacenter $datacenter,
53+
Location $location,
54+
Image $image,
55+
$startAfterCreate = true,
56+
$user_data = '',
57+
$ssh_keys = []
58+
): Server {
59+
$this->httpClient->post('servers', [
60+
'json' => [
61+
'name' => $name,
62+
'server_type' => $serverType->id,
63+
'datacenter' => $datacenter->id,
64+
'location' => $location->id,
65+
'image' => $image->id,
66+
'start_after_create' => $startAfterCreate,
67+
'user_data' => $user_data,
68+
'ssh_keys' => $ssh_keys,
69+
],
70+
]);
1971
}
2072
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\Servers\Types;
4+
5+
use LKDev\HetznerCloud\HetznerAPIClient;
6+
use LKDev\HetznerCloud\Models\Model;
7+
8+
/**
9+
*
10+
*/
11+
class ServerType extends Model
12+
{
13+
/**
14+
* @var int
15+
*/
16+
public $id;
17+
18+
/**
19+
* ServerType constructor.
20+
*
21+
* @param int $serverTypeId
22+
* @param \LKDev\HetznerCloud\HetznerAPIClient $hetznerAPIClient
23+
* @param null $httpClient
24+
*/
25+
public function __construct(int $serverTypeId, \LKDev\HetznerCloud\HetznerAPIClient $hetznerAPIClient, $httpClient = null)
26+
{
27+
$this->id = $serverTypeId;
28+
parent::__construct($hetznerAPIClient, $httpClient);
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 28.01.18
6+
* Time: 20:58
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models\Servers\Types;
10+
11+
use LKDev\HetznerCloud\Models\Model;
12+
13+
class ServerTypes extends Model
14+
{
15+
public function all(): array
16+
{
17+
$this->httpClient->get('server_types');
18+
}
19+
}

0 commit comments

Comments
 (0)