Skip to content

Commit e9ab838

Browse files
committed
Implement Resources Images & Image & the Server Method createImage
1 parent 41cfe0d commit e9ab838

File tree

3 files changed

+249
-8
lines changed

3 files changed

+249
-8
lines changed

src/Models/Images/Image.php

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,171 @@
88

99
namespace LKDev\HetznerCloud\Models\Images;
1010

11-
class Image
11+
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Model;
13+
use LKDev\HetznerCloud\Models\Servers\Server;
14+
15+
/**
16+
*
17+
*/
18+
class Image extends Model
1219
{
20+
/**
21+
* @var int
22+
*/
23+
public $id;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $type;
29+
30+
/**
31+
* @var string
32+
*/
33+
public $status;
34+
35+
/**
36+
* @var string
37+
*/
38+
public $name;
39+
40+
/**
41+
* @var string
42+
*/
43+
public $description;
44+
45+
/**
46+
* @var float
47+
*/
48+
public $imageSize;
49+
50+
/**
51+
* @var integer
52+
*/
53+
public $diskSize;
54+
55+
/**
56+
* @var string
57+
*/
58+
public $created;
59+
60+
/**
61+
* @var \LKDev\HetznerCloud\Models\Servers\Server
62+
*/
63+
public $createdFrom;
64+
65+
/**
66+
* @var int
67+
*/
68+
public $boundTo;
69+
70+
/**
71+
* @var string
72+
*/
73+
public $osFlavor;
74+
75+
/**
76+
* @var string
77+
*/
78+
public $osVersion;
79+
80+
/**
81+
* @var bool
82+
*/
83+
public $rapidDeploy;
84+
85+
/**
86+
* Image constructor.
87+
*
88+
* @param int $id
89+
* @param string $type
90+
* @param string $status
91+
* @param string $name
92+
* @param string $description
93+
* @param float $imageSize
94+
* @param int $diskSize
95+
* @param string $created
96+
* @param \LKDev\HetznerCloud\Models\Servers\Server $createdFrom
97+
* @param int $boundTo
98+
* @param string $osFlavor
99+
* @param string $osVersion
100+
* @param bool $rapidDeploy
101+
*/
102+
public function __construct(
103+
int $id,
104+
string $type,
105+
string $status,
106+
string $name,
107+
string $description,
108+
float $imageSize,
109+
int $diskSize,
110+
string $created,
111+
Server $createdFrom,
112+
int $boundTo,
113+
string $osFlavor,
114+
string $osVersion,
115+
bool $rapidDeploy
116+
) {
117+
$this->id = $id;
118+
$this->type = $type;
119+
$this->status = $status;
120+
$this->name = $name;
121+
$this->description = $description;
122+
$this->imageSize = $imageSize;
123+
$this->diskSize = $diskSize;
124+
$this->created = $created;
125+
$this->createdFrom = $createdFrom;
126+
$this->boundTo = $boundTo;
127+
$this->osFlavor = $osFlavor;
128+
$this->osVersion = $osVersion;
129+
$this->rapidDeploy = $rapidDeploy;
130+
parent::__construct();
131+
}
132+
133+
/**
134+
* Updates the Image. You may change the description or convert a Backup image to a Snapshot Image. Only images of type snapshot and backup can be updated.
135+
*
136+
* @see https://docs.hetzner.cloud/#resources-images-put
137+
* @param string $description
138+
* @param string $type
139+
* @return \LKDev\HetznerCloud\Models\Images\Image
140+
* @throws \LKDev\HetznerCloud\APIException
141+
*/
142+
public function update(string $description, string $type): Image
143+
{
144+
$response = $this->httpClient->put('images/'.$this->id, [
145+
'json' => [
146+
'description' => $description,
147+
'type' => $type,
148+
],
149+
]);
150+
if (! HetznerAPIClient::hasError($response)) {
151+
return self::parse(json_decode((string) $response->getBody())->image);
152+
}
153+
}
154+
155+
/**
156+
* Deletes an Image. Only images of type snapshot and backup can be deleted.
157+
*
158+
* @see https://docs.hetzner.cloud/#resources-images-delete
159+
* @return bool
160+
* @throws \LKDev\HetznerCloud\APIException
161+
*/
162+
public function delete(): bool
163+
{
164+
$response = $this->httpClient->delete('images/'.$this->id);
165+
if (! HetznerAPIClient::hasError($response)) {
166+
return true;
167+
}
168+
}
169+
170+
/**
171+
* @param object $input
172+
* @return \LKDev\HetznerCloud\Models\Images\Image|static
173+
*/
174+
public static function parse(object $input)
175+
{
176+
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, Server::parse($input->created_from), $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy);
177+
}
13178
}

src/Models/Images/Images.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 28.01.18
6+
* Time: 21:01
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models\Images;
10+
11+
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Model;
13+
14+
class Images extends Model
15+
{ /**
16+
* @var array
17+
*/
18+
public $images;
19+
20+
/**
21+
* Returns all image objects.
22+
*
23+
* @see https://docs.hetzner.cloud/#resources-images-get
24+
* @return array
25+
* @throws \LKDev\HetznerCloud\APIException
26+
*/
27+
public function all(): array
28+
{
29+
$response = $this->httpClient->get('images');
30+
if (! HetznerAPIClient::hasError($response)) {
31+
return self::parse(json_decode((string) $response->getBody()))->images;
32+
}
33+
}
34+
35+
/**
36+
* Returns a specific image object.
37+
*
38+
* @see https://docs.hetzner.cloud/#resources-images-get-1
39+
* @param int $imageId
40+
* @return \LKDev\HetznerCloud\Models\Images\Image
41+
* @throws \LKDev\HetznerCloud\APIException
42+
*/
43+
public function get(int $imageId): Image
44+
{
45+
$response = $this->httpClient->get('images/'.$imageId);
46+
if (! HetznerAPIClient::hasError($response)) {
47+
return Image::parse(json_decode((string) $response->getBody())->image);
48+
}
49+
}
50+
51+
/**
52+
* @param object $input
53+
* @return $this
54+
*/
55+
public function setAdditionalData(object $input)
56+
{
57+
$this->images = collect($input->images)->map(function ($image, $key) {
58+
return Image::parse($image);
59+
})->toArray();
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* @param object $input
66+
* @return $this|static
67+
*/
68+
public static function parse(object $input)
69+
{
70+
return (new self())->setAdditionalData($input);
71+
}
72+
}

src/Models/Servers/Server.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function powerOn(): Action
153153
*/
154154
public function softReboot(): Action
155155
{
156-
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/reboot'));
156+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/reboot'));
157157
if (! HetznerAPIClient::hasError($response)) {
158158
return Action::parse(json_decode((string) $response->getBody()->action));
159159
}
@@ -250,7 +250,7 @@ public function enableRescue($type = 'linux64', $ssh_keys = []): Action
250250
*/
251251
public function disableRescue(): Action
252252
{
253-
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/disable_rescue'));
253+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/disable_rescue'));
254254
if (! HetznerAPIClient::hasError($response)) {
255255
return Action::parse(json_decode((string) $response->getBody()->action));
256256
}
@@ -263,16 +263,20 @@ public function disableRescue(): Action
263263
* @param string $description
264264
* @param string $type
265265
* @return \LKDev\HetznerCloud\Models\Images\Image
266+
* @throws \LKDev\HetznerCloud\APIException
266267
*/
267268
public function createImage(string $description = '', string $type = 'snapshot'): Image
268269
{
269-
// ToDo
270-
$this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/create_image'), [
270+
271+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/create_image'), [
271272
'form_params' => [
272273
'description' => $description,
273274
'type' => $type,
274275
],
275276
]);
277+
if (! HetznerAPIClient::hasError($response)) {
278+
return Image::parse(json_decode((string) $response->getBody()->image));
279+
}
276280
}
277281

278282
/**
@@ -360,9 +364,9 @@ public function disableBackups(): Action
360364
* @return \LKDev\HetznerCloud\Models\Actions\Action
361365
* @throws \LKDev\HetznerCloud\APIException
362366
*/
363-
public function attachISO(ISO $iso):Action
367+
public function attachISO(ISO $iso): Action
364368
{
365-
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/attach_iso'), [
369+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/attach_iso'), [
366370
'form_params' => [
367371
'iso' => $iso->id,
368372
],
@@ -381,7 +385,7 @@ public function attachISO(ISO $iso):Action
381385
*/
382386
public function detachISO(): Action
383387
{
384-
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/detach_iso'));
388+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/detach_iso'));
385389
if (! HetznerAPIClient::hasError($response)) {
386390
return Action::parse(json_decode((string) $response->getBody()->action));
387391
}

0 commit comments

Comments
 (0)