Skip to content

Commit 7dc59d9

Browse files
committed
v3.0.0
- Refactor lib to always return an API Response - Add missing fields/functions from API documentation
1 parent 0f29f7f commit 7dc59d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+818
-129
lines changed

src/HetznerAPIClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class HetznerAPIClient
3232
/**
3333
* Version of the API Client.
3434
*/
35-
const VERSION = '2.9.1';
35+
const VERSION = '3.0.0-alpha.1';
3636

3737
const MAX_ENTITIES_PER_PAGE = 50;
3838

src/Models/Actions/Action.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class Action extends Model implements Resource
2828
public $progress;
2929

3030
/**
31-
* @var string
31+
* @var string|null
3232
*/
3333
public $started;
3434

3535
/**
36-
* @var string
36+
* @var string|null
3737
*/
3838
public $finished;
3939

@@ -66,7 +66,7 @@ public function __construct(
6666
string $command,
6767
int $progress,
6868
string $status,
69-
string $started,
69+
?string $started = null,
7070
?string $finished = null,
7171
$resources = null,
7272
$error = null
@@ -148,6 +148,6 @@ public static function parse($input)
148148
return;
149149
}
150150

151-
return new self($input->id, $input->command, $input->progress, $input->status, $input->started, $input->finished, $input->resources, $input->error ?? null);
151+
return new self($input->id, $input->command, $input->progress, $input->status, $input->started ?? null, $input->finished ?? null, $input->resources, $input->error ?? null);
152152
}
153153
}

src/Models/Certificates/Certificate.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class Certificate extends Model implements Resource
5757
* @var array
5858
*/
5959
public $labels;
60+
/**
61+
* @var string
62+
*/
63+
public $type;
6064

6165
/**
6266
* Certificate constructor.
@@ -71,8 +75,9 @@ class Certificate extends Model implements Resource
7175
* @param string|null $fingerprint
7276
* @param array|null $used_by
7377
* @param array|null $labels
78+
* @param string|null $type
7479
*/
75-
public function __construct(int $id, ?string $name = null, ?string $certificate = null, ?string $created = null, ?string $not_valid_before = null, ?string $not_valid_after = null, ?array $domain_names = null, ?string $fingerprint = null, $used_by = null, $labels = [])
80+
public function __construct(int $id, ?string $name = null, ?string $certificate = null, ?string $created = null, ?string $not_valid_before = null, ?string $not_valid_after = null, ?array $domain_names = null, ?string $fingerprint = null, $used_by = null, $labels = [], ?string $type = null)
7681
{
7782
$this->id = $id;
7883
$this->name = $name;
@@ -84,6 +89,7 @@ public function __construct(int $id, ?string $name = null, ?string $certificate
8489
$this->fingerprint = $fingerprint;
8590
$this->used_by = $used_by;
8691
$this->labels = $labels;
92+
$this->type = $type;
8793

8894
parent::__construct();
8995
}
@@ -136,7 +142,7 @@ public function delete(): bool
136142
*/
137143
public static function parse($input)
138144
{
139-
return new self($input->id, $input->name, $input->certificate, $input->created, $input->not_valid_before, $input->not_valid_after, $input->domain_names, $input->fingerprint, $input->used_by, $input->labels);
145+
return new self($input->id, $input->name, $input->certificate, $input->created, $input->not_valid_before, $input->not_valid_after, $input->domain_names, $input->fingerprint, $input->used_by, $input->labels, $input->type ?? null);
140146
}
141147

142148
/**

src/Models/Certificates/Certificates.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ class Certificates extends Model implements Resources
4141
*/
4242
public function create(
4343
string $name,
44-
string $certificate,
45-
string $privateKey,
46-
array $labels = []
44+
?string $certificate = null,
45+
?string $privateKey = null,
46+
array $labels = [],
47+
string $type = 'uploaded'
4748
): ?Certificate {
4849
$parameters = [
4950
'name' => $name,
50-
'certificate' => $certificate,
51-
'private_key' => $privateKey,
51+
'type' => $type,
5252
];
53+
if ($certificate !== null) {
54+
$parameters['certificate'] = $certificate;
55+
}
56+
if ($privateKey !== null) {
57+
$parameters['private_key'] = $privateKey;
58+
}
5359
if (! empty($labels)) {
5460
$parameters['labels'] = $labels;
5561
}

src/Models/Firewalls/Firewall.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,14 @@ public static function parse($input): ?self
119119
$rules = [];
120120

121121
foreach ($input->rules as $r) {
122-
$rules[] = new FirewallRule($r->direction, $r->protocol, $r->source_ips, $r->destination_ips, (string) $r->port);
122+
$rules[] = new FirewallRule($r->direction, $r->protocol, (isset($r->source_ips)) ? $r->source_ips : [], (isset($r->destination_ips)) ? $r->destination_ips : [], (isset($r->port)) ? (string) $r->port : '', (isset($r->description)) ? $r->description : '');
123123
}
124124

125125
foreach ($input->applied_to as $a) {
126126
if ($a->type === 'server') {
127127
$appliedTo[] = new FirewallResource($a->type, new Server($a->server->id));
128+
} elseif ($a->type === 'label_selector') {
129+
$appliedTo[] = new FirewallResource($a->type, null, get_object_vars($a->label_selector));
128130
}
129131
}
130132

@@ -168,18 +170,18 @@ public function setRules(array $rules): ?ApiResponse
168170
*
169171
* @see https://docs.hetzner.cloud/#firewalls-delete-a-firewall
170172
*
171-
* @return bool
173+
* @return APIResponse|null
172174
*
173175
* @throws \LKDev\HetznerCloud\APIException
174176
*/
175-
public function delete(): bool
177+
public function delete(): ?APIResponse
176178
{
177179
$response = $this->httpClient->delete('firewalls/'.$this->id);
178180
if (! HetznerAPIClient::hasError($response)) {
179-
return true;
181+
return APIResponse::create([], $response->getHeaders());
180182
}
181183

182-
return false;
184+
return null;
183185
}
184186

185187
/**

src/Models/Firewalls/FirewallResource.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class FirewallResource
1111
{
1212
const TYPE_SERVER = 'server';
1313

14+
const TYPE_LABEL_SELECTOR = 'label_selector';
15+
1416
/**
1517
* @var string
1618
*/
@@ -20,26 +22,35 @@ class FirewallResource
2022
*/
2123
public $server;
2224

25+
/**
26+
* @var ?array
27+
*/
28+
public $labelSelector;
29+
2330
/**
2431
* FirewallResource constructor.
2532
*
2633
* @param string $type
2734
* @param Server|null $server
35+
* @param array|null $labelSelector
2836
*/
29-
public function __construct(string $type, ?Server $server)
37+
public function __construct(string $type, ?Server $server = null, ?array $labelSelector = null)
3038
{
3139
$this->type = $type;
3240
$this->server = $server;
41+
$this->labelSelector = $labelSelector;
3342
}
3443

3544
/**
36-
* @return string[]
45+
* @return array
3746
*/
3847
public function toRequestSchema(): array
3948
{
4049
$s = ['type' => $this->type];
4150
if ($this->type == self::TYPE_SERVER) {
4251
$s['server'] = ['id' => $this->server->id];
52+
} elseif ($this->type == self::TYPE_LABEL_SELECTOR) {
53+
$s['label_selector'] = $this->labelSelector;
4354
}
4455

4556
return $s;

src/Models/Firewalls/FirewallRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class FirewallRule
3737
*/
3838
public $port;
3939

40+
/**
41+
* @var string
42+
*/
43+
public $description;
44+
4045
/**
4146
* FirewallRule constructor.
4247
*
@@ -45,14 +50,16 @@ class FirewallRule
4550
* @param string[] $destinationIPs
4651
* @param string $protocol
4752
* @param string $port
53+
* @param string $description
4854
*/
49-
public function __construct(string $direction, string $protocol, array $sourceIPs = [], array $destinationIPs = [], ?string $port = '')
55+
public function __construct(string $direction, string $protocol, array $sourceIPs = [], array $destinationIPs = [], ?string $port = '', ?string $description = '')
5056
{
5157
$this->direction = $direction;
5258
$this->sourceIPs = $sourceIPs;
5359
$this->destinationIPs = $destinationIPs;
5460
$this->protocol = $protocol;
5561
$this->port = $port;
62+
$this->description = $description;
5663
}
5764

5865
/**
@@ -71,6 +78,9 @@ public function toRequestSchema(): array
7178
if ($this->port != '') {
7279
$s['port'] = $this->port;
7380
}
81+
if ($this->description != '') {
82+
$s['description'] = $this->description;
83+
}
7484

7585
return $s;
7686
}

src/Models/FloatingIps/FloatingIp.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,18 @@ public function changeDescription(string $description): ?self
181181
*
182182
* @see https://docs.hetzner.cloud/#resources-floating-ips-delete
183183
*
184-
* @return bool
184+
* @return APIResponse|null
185185
*
186186
* @throws \LKDev\HetznerCloud\APIException
187187
*/
188-
public function delete(): bool
188+
public function delete(): ?APIResponse
189189
{
190190
$response = $this->httpClient->delete('floating_ips/'.$this->id);
191191
if (! HetznerAPIClient::hasError($response)) {
192-
return true;
192+
return APIResponse::create([], $response->getHeaders());
193193
}
194194

195-
return false;
195+
return null;
196196
}
197197

198198
/**

src/Models/FloatingIps/FloatingIps.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getByName(string $name): ?FloatingIp
123123
* @param \LKDev\HetznerCloud\Models\Servers\Server|null $server
124124
* @param string|null $name
125125
* @param array $labels
126-
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp|null
126+
* @return \LKDev\HetznerCloud\APIResponse|null
127127
*
128128
* @throws \LKDev\HetznerCloud\APIException
129129
*/
@@ -134,7 +134,7 @@ public function create(
134134
?Server $server = null,
135135
?string $name = null,
136136
array $labels = []
137-
): ?FloatingIp {
137+
): ?APIResponse {
138138
$parameters = [
139139
'type' => $type,
140140
];
@@ -157,7 +157,12 @@ public function create(
157157
'json' => $parameters,
158158
]);
159159
if (! HetznerAPIClient::hasError($response)) {
160-
return FloatingIp::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
160+
$payload = json_decode((string) $response->getBody());
161+
162+
return APIResponse::create([
163+
'floating_ip' => FloatingIp::parse($payload->floating_ip),
164+
'action' => property_exists($payload, 'action') ? \LKDev\HetznerCloud\Models\Actions\Action::parse($payload->action) : null,
165+
], $response->getHeaders());
161166
}
162167

163168
return null;

src/Models/ISOs/ISO.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,34 @@ class ISO extends Model implements Resource
2828
*/
2929
public $type;
3030

31+
/**
32+
* @var string
33+
*/
34+
public $architecture;
35+
36+
/**
37+
* @var string|null
38+
*/
39+
public $deprecated;
40+
3141
/**
3242
* ISO constructor.
3343
*
3444
* @param int $id
3545
* @param string $name
3646
* @param string $description
3747
* @param string $type
48+
* @param string $architecture
49+
* @param string|null $deprecated
3850
*/
39-
public function __construct(int $id, ?string $name = null, ?string $description = null, ?string $type = null)
51+
public function __construct(int $id, ?string $name = null, ?string $description = null, ?string $type = null, ?string $architecture = null, ?string $deprecated = null)
4052
{
4153
$this->id = $id;
4254
$this->name = $name;
4355
$this->description = $description;
4456
$this->type = $type;
57+
$this->architecture = $architecture;
58+
$this->deprecated = $deprecated;
4559
parent::__construct();
4660
}
4761

@@ -55,7 +69,7 @@ public static function parse($input): ?self
5569
return null;
5670
}
5771

58-
return new self($input->id, $input->name, $input->description, $input->type);
72+
return new self($input->id, $input->name, $input->description, $input->type, $input->architecture ?? null, $input->deprecated ?? null);
5973
}
6074

6175
public function reload()

0 commit comments

Comments
 (0)