Skip to content

Commit b9d4adb

Browse files
committed
Remove $recommendation on Datacenters
Implement the new change protection methods from the api.
1 parent f823fc9 commit b9d4adb

File tree

5 files changed

+155
-12
lines changed

5 files changed

+155
-12
lines changed

src/Models/Datacenters/Datacenter.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ class Datacenter extends Model
4141
*/
4242
public $serverTypes;
4343

44-
/**
45-
* @var bool
46-
*/
47-
public $recommendation;
48-
4944
/**
5045
* Datacenter constructor.
5146
*
@@ -54,7 +49,6 @@ class Datacenter extends Model
5449
* @param string $description
5550
* @param \LKDev\HetznerCloud\Models\Locations\Location $location
5651
* @param array $server_types
57-
* @param bool $recommendation
5852
*/
5953
public function __construct(
6054
int $id,
@@ -69,7 +63,6 @@ public function __construct(
6963
$this->description = $description;
7064
$this->location = $location;
7165
$this->serverTypes = $server_types;
72-
$this->recommendation = $recommendation;
7366
parent::__construct();
7467
}
7568

@@ -82,6 +75,6 @@ public static function parse($input)
8275
if ($input == null) {
8376
return null;
8477
}
85-
return new self($input->id,$input->name,$input->description,Location::parse($input->location),$input->server_types,$input->recommendation);
78+
return new self($input->id,$input->name,$input->description,Location::parse($input->location),$input->server_types);
8679
}
8780
}

src/Models/FloatingIps/FloatingIp.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
namespace LKDev\HetznerCloud\Models\FloatingIps;
1010

1111
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Actions\Action;
1213
use LKDev\HetznerCloud\Models\Locations\Location;
1314
use LKDev\HetznerCloud\Models\Model;
15+
use LKDev\HetznerCloud\Models\Protection;
1416

1517
/**
1618
*
@@ -52,6 +54,11 @@ class FloatingIp extends Model
5254
*/
5355
public $blocked;
5456

57+
/**
58+
* @var array|\LKDev\HetznerCloud\Models\Protection
59+
*/
60+
public $protection;
61+
5562
/**
5663
* FloatingIp constructor.
5764
*
@@ -62,6 +69,7 @@ class FloatingIp extends Model
6269
* @param array $dnsPtr
6370
* @param \LKDev\HetznerCloud\Models\Locations\Location $homeLocation
6471
* @param bool $blocked
72+
* @param array $protection
6573
*/
6674
public function __construct(
6775
int $id,
@@ -70,7 +78,8 @@ public function __construct(
7078
int $server,
7179
array $dnsPtr,
7280
Location $homeLocation,
73-
bool $blocked
81+
bool $blocked,
82+
Protection $protection
7483
) {
7584
$this->id = $id;
7685
$this->description = $description;
@@ -79,6 +88,7 @@ public function __construct(
7988
$this->dnsPtr = $dnsPtr;
8089
$this->homeLocation = $homeLocation;
8190
$this->blocked = $blocked;
91+
$this->protection = $protection;
8292
parent::__construct();
8393
}
8494

@@ -116,4 +126,37 @@ public function delete()
116126
return true;
117127
}
118128
}
129+
130+
/**
131+
* Changes the protection configuration of the Floating IP.
132+
*
133+
* @see https://docs.hetzner.cloud/#resources-floating-ip-actions-post-3
134+
* @param bool $delete
135+
* @return \LKDev\HetznerCloud\Models\Actions\Action
136+
* @throws \LKDev\HetznerCloud\APIException
137+
*/
138+
public function changeProtection(bool $delete = true): Action
139+
{
140+
$response = $this->httpClient->post('floating_ips/'.$this->id.'/change_protection', [
141+
'json' => [
142+
'delete' => $delete,
143+
],
144+
]);
145+
if (! HetznerAPIClient::hasError($response)) {
146+
return Action::parse(json_decode((string) $response->getBody())->action);
147+
}
148+
}
149+
150+
/**
151+
* @param $input
152+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp|static
153+
*/
154+
public static function parse($input)
155+
{
156+
if ($input == null) {
157+
return null;
158+
}
159+
160+
return new self($input->id, $input->description, $input->ip, $input->type, $input->server, $input->dns_ptr, $input->home_location, $input->blocked, Protection::parse($input->protection));
161+
}
119162
}

src/Models/Images/Image.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace LKDev\HetznerCloud\Models\Images;
1010

1111
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Actions\Action;
1213
use LKDev\HetznerCloud\Models\Model;
14+
use LKDev\HetznerCloud\Models\Protection;
1315
use LKDev\HetznerCloud\Models\Servers\Server;
1416

1517
/**
@@ -82,6 +84,11 @@ class Image extends Model
8284
*/
8385
public $rapidDeploy;
8486

87+
/**
88+
* @var array|\LKDev\HetznerCloud\Models\Protection
89+
*/
90+
public $protection;
91+
8592
/**
8693
* Image constructor.
8794
*
@@ -98,6 +105,7 @@ class Image extends Model
98105
* @param string $osFlavor
99106
* @param string $osVersion
100107
* @param bool $rapidDeploy
108+
* @param Protection $protection
101109
*/
102110
public function __construct(
103111
int $id,
@@ -112,7 +120,8 @@ public function __construct(
112120
int $boundTo = null,
113121
string $osFlavor = null,
114122
string $osVersion = null,
115-
bool $rapidDeploy = null
123+
bool $rapidDeploy = null,
124+
Protection $protection = null
116125
) {
117126
$this->id = $id;
118127
$this->type = $type;
@@ -127,6 +136,7 @@ public function __construct(
127136
$this->osFlavor = $osFlavor;
128137
$this->osVersion = $osVersion;
129138
$this->rapidDeploy = $rapidDeploy;
139+
$this->protection = $protection;
130140
parent::__construct();
131141
}
132142

@@ -152,6 +162,26 @@ public function update(string $description, string $type): Image
152162
}
153163
}
154164

165+
/**
166+
* Changes the protection configuration of the image. Can only be used on snapshots.
167+
*
168+
* @see https://docs.hetzner.cloud/#resources-image-actions-post
169+
* @param bool $delete
170+
* @return \LKDev\HetznerCloud\Models\Actions\Action
171+
* @throws \LKDev\HetznerCloud\APIException
172+
*/
173+
public function changeProtection(bool $delete = true): Action
174+
{
175+
$response = $this->httpClient->post('images/'.$this->id.'/change_protection', [
176+
'json' => [
177+
'delete' => $delete,
178+
],
179+
]);
180+
if (! HetznerAPIClient::hasError($response)) {
181+
return Action::parse(json_decode((string) $response->getBody())->action);
182+
}
183+
}
184+
155185
/**
156186
* Deletes an Image. Only images of type snapshot and backup can be deleted.
157187
*
@@ -176,6 +206,7 @@ public static function parse($input)
176206
if ($input == null) {
177207
return null;
178208
}
179-
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy);
209+
210+
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy, Protection::parse($input->protection));
180211
}
181212
}

src/Models/Protection.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 01.04.18
6+
* Time: 19:02
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models;
10+
11+
class Protection extends Model
12+
{
13+
/**
14+
* @var boolean
15+
*/
16+
public $delete;
17+
18+
/**
19+
* @var boolean
20+
*/
21+
public $rebuild;
22+
23+
/**
24+
* Protection constructor.
25+
*
26+
* @param bool $delete
27+
* @param bool $rebuild
28+
*/
29+
public function __construct(bool $delete, bool $rebuild = null)
30+
{
31+
$this->delete = $delete;
32+
$this->rebuild = $rebuild;
33+
}
34+
35+
/**
36+
* @param $input
37+
* @return \LKDev\HetznerCloud\Models\Protection|null|static
38+
*/
39+
public static function parse($input)
40+
{
41+
42+
if ($input == null) {
43+
return null;
44+
}
45+
46+
return new self($input->delete, (property_exists($input, 'rebuild') ? $input->rebuild : null));
47+
}
48+
}

src/Models/Servers/Server.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use LKDev\HetznerCloud\Models\Images\Image;
1515
use LKDev\HetznerCloud\Models\ISOs\ISO;
1616
use LKDev\HetznerCloud\Models\Model;
17+
use LKDev\HetznerCloud\Models\Protection;
1718
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
1819

1920
/**
@@ -96,6 +97,11 @@ class Server extends Model
9697
*/
9798
public $includedTraffic;
9899

100+
/**
101+
* @var array|\LKDev\HetznerCloud\Models\Protection
102+
*/
103+
public $protection;
104+
99105
/**
100106
*
101107
*
@@ -126,7 +132,7 @@ public function setAdditionalData($data)
126132
$this->outgoingTraffic = $data->outgoing_traffic;
127133
$this->ingoingTraffic = $data->ingoing_traffic;
128134
$this->includedTraffic = $data->included_traffic;
129-
135+
$this->protection = Protection::parse($data->protection);
130136
return $this;
131137
}
132138

@@ -478,6 +484,28 @@ public function requestConsole(): Action
478484
}
479485
}
480486

487+
/**
488+
* Changes the protection configuration of the server.
489+
*
490+
* @see https://docs.hetzner.cloud/#resources-server-actions-post-16
491+
* @param bool $delete
492+
* @param bool $rebuild
493+
* @return \LKDev\HetznerCloud\Models\Actions\Action
494+
* @throws \LKDev\HetznerCloud\APIException
495+
*/
496+
public function changeProtection(bool $delete = true, bool $rebuild = true): Action
497+
{
498+
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/change_protection'), [
499+
'json' => [
500+
'delete' => $delete,
501+
'rebuild' => $rebuild,
502+
],
503+
]);
504+
if (! HetznerAPIClient::hasError($response)) {
505+
return Action::parse(json_decode((string) $response->getBody())->action);
506+
}
507+
}
508+
481509
/**
482510
* @param string $uri
483511
* @return string

0 commit comments

Comments
 (0)