Skip to content

Commit d4705d0

Browse files
authored
Add getByName to most resources (#24)
1 parent 7dde0cd commit d4705d0

File tree

17 files changed

+224
-17
lines changed

17 files changed

+224
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.8.0 (18.09.2019)
4+
+ Add ability to get `Datacenters`,`FloatingIPs`,`Images`,`Locations` and `ServerTypes` per name (`getByName`)
5+
+ Add `name` support to Floating IPs
6+
37
## 1.7.1 (01.08.2019)
48
+ Add missing `networks()` - method on `HetznerAPIClient`
59

src/HetznerAPIClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class HetznerAPIClient
2424
/**
2525
* Version of the API Client
2626
*/
27-
const VERSION = "1.7.1";
27+
const VERSION = "1.8.0";
2828

2929
/**
3030
* @var string

src/Models/Datacenters/Datacenters.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public function get(int $datacenterId): Datacenter
5050
}
5151
}
5252

53+
/**
54+
* Returns a specific datacenter object by its name.
55+
*
56+
* @see https://docs.hetzner.cloud/#resources-datacenters-get-1
57+
* @param int $datacenterId
58+
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter
59+
* @throws \LKDev\HetznerCloud\APIException
60+
*/
61+
public function getByName(string $name): Datacenter
62+
{
63+
$datacenters = $this->all($name);
64+
65+
return (count($datacenters) > 0) ? $datacenters[0] : null;
66+
}
67+
5368
/**
5469
* @param $input
5570
* @return $this
@@ -71,4 +86,4 @@ public static function parse($input)
7186
{
7287
return (new self())->setAdditionalData($input);
7388
}
74-
}
89+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models\FloatingIps;
4+
5+
6+
use LKDev\HetznerCloud\RequestOpts;
7+
8+
/**
9+
* Class FloatingIPRequestOpts
10+
* @package LKDev\HetznerCloud\Models\FloatingIps
11+
*/
12+
class FloatingIPRequestOpts extends RequestOpts
13+
{
14+
/**
15+
* @var string
16+
*/
17+
public $name;
18+
19+
/**
20+
* RequestOpts constructor.
21+
*
22+
* @param $name
23+
* @param $status
24+
* @param $perPage
25+
* @param $page
26+
* @param $labelSelector
27+
*/
28+
public function __construct(string $name = null, int $perPage = null, int $page = null, string $labelSelector = null)
29+
{
30+
$this->name = $name;
31+
parent::__construct($perPage, $page, $labelSelector);
32+
}
33+
}

src/Models/FloatingIps/FloatingIp.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class FloatingIp extends Model
2626
*/
2727
public $id;
2828

29+
/**
30+
* @var string
31+
*/
32+
public $name;
33+
2934
/**
3035
* @var string
3136
*/
@@ -69,7 +74,6 @@ class FloatingIp extends Model
6974
*/
7075
public $protection;
7176

72-
7377
/**
7478
* @var array
7579
*/
@@ -89,6 +93,7 @@ class FloatingIp extends Model
8993
* @param Protection $protection
9094
* @param array $labels
9195
* @param string $created
96+
* @param string $name
9297
*/
9398
public function __construct(
9499
int $id,
@@ -101,7 +106,8 @@ public function __construct(
101106
bool $blocked,
102107
Protection $protection,
103108
array $labels = [],
104-
string $created = ''
109+
string $created = '',
110+
string $name = ''
105111
)
106112
{
107113
$this->id = $id;
@@ -115,6 +121,7 @@ public function __construct(
115121
$this->protection = $protection;
116122
$this->labels = $labels;
117123
$this->created = $created;
124+
$this->name = $name;
118125
parent::__construct();
119126
}
120127

@@ -259,7 +266,6 @@ public static function parse($input): FloatingIp
259266
if ($input == null) {
260267
return null;
261268
}
262-
263-
return new self($input->id, $input->description, $input->ip, $input->type, $input->server, $input->dns_ptr, Location::parse($input->home_location), $input->blocked, Protection::parse($input->protection));
269+
return new self($input->id, $input->description, $input->ip, $input->type, $input->server, $input->dns_ptr, Location::parse($input->home_location), $input->blocked, Protection::parse($input->protection), get_object_vars($input->labels), $input->created, $input->name);
264270
}
265271
}

src/Models/FloatingIps/FloatingIps.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class FloatingIps extends Model
2525
* Returns all floating ip objects.
2626
*
2727
* @see https://docs.hetzner.cloud/#resources-floating-ips-get
28-
* @param RequestOpts|null $requestOpts
28+
* @param FloatingIPRequestOpts|RequestOpts|null $requestOpts
2929
* @return array
3030
* @throws \LKDev\HetznerCloud\APIException
3131
*/
32-
public function all(RequestOpts $requestOpts = null): array
32+
public function all(FloatingIPRequestOpts $requestOpts = null): array
3333
{
3434
if ($requestOpts == null) {
35-
$requestOpts = new RequestOpts();
35+
$requestOpts = new FloatingIPRequestOpts();
3636
}
3737
$response = $this->httpClient->get('floating_ips' . $requestOpts->buildQuery());
3838
if (!HetznerAPIClient::hasError($response)) {
@@ -56,6 +56,22 @@ public function get(int $floatingIpId): FloatingIp
5656
}
5757
}
5858

59+
/**
60+
* Returns a specific Floating IP object by its name.
61+
*
62+
* @see https://docs.hetzner.cloud/#resources-floating-ips-get-1
63+
* @param string $name
64+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
65+
* @throws \LKDev\HetznerCloud\APIException
66+
*/
67+
public function getByName(string $name)
68+
{
69+
$floatingIPs = $this->all(new FloatingIPRequestOpts($name));
70+
71+
return (count($floatingIPs) > 0) ? $floatingIPs[0] : null;
72+
}
73+
74+
5975
/**
6076
* Creates a new Floating IP assigned to a server.
6177
*
@@ -64,21 +80,24 @@ public function get(int $floatingIpId): FloatingIp
6480
* @param string|null $description
6581
* @param \LKDev\HetznerCloud\Models\Locations\Location|null $location
6682
* @param \LKDev\HetznerCloud\Models\Servers\Server|null $server
83+
* @param string|null $name
6784
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
6885
* @throws \LKDev\HetznerCloud\APIException
6986
*/
7087
public function create(
7188
string $type,
7289
string $description = null,
7390
Location $location = null,
74-
Server $server = null
91+
Server $server = null,
92+
string $name = null
7593
): FloatingIp
7694
{
7795
$response = $this->httpClient->post('floating_ips', [
7896
'type' => $type,
7997
'description' => $description,
8098
'server' => $server ?: $server->id,
8199
'home_location' => $location ?: $location->name,
100+
'name' => $name ?: $name,
82101
]);
83102
if (!HetznerAPIClient::hasError($response)) {
84103
return FloatingIp::parse(json_decode((string)$response->getBody())->floating_ip);
@@ -106,4 +125,4 @@ public static function parse($input)
106125
{
107126
return (new self())->setAdditionalData($input);
108127
}
109-
}
128+
}

src/Models/Images/Images.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public function get(int $imageId): Image
5050
}
5151
}
5252

53+
/**
54+
* Returns a specific datacenter object by its name.
55+
*
56+
* @see https://docs.hetzner.cloud/#resources-datacenters-get-1
57+
* @param int $datacenterId
58+
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter
59+
* @throws \LKDev\HetznerCloud\APIException
60+
*/
61+
public function getByName(string $name): Image
62+
{
63+
$images = $this->all($name);
64+
65+
return (count($images) > 0) ? $images[0] : null;
66+
}
67+
5368
/**
5469
* @param $input
5570
* @return $this
@@ -71,4 +86,4 @@ public static function parse($input)
7186
{
7287
return (new self())->setAdditionalData($input);
7388
}
74-
}
89+
}

src/Models/Locations/Locations.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ public function get(int $locationId): Location
5353
}
5454
}
5555

56+
/**
57+
* Returns a specific location object by its name.
58+
*
59+
* @see https://docs.hetzner.cloud/#resources-locations-get-1
60+
* @param int $locationId
61+
* @return \LKDev\HetznerCloud\Models\Locations\Location
62+
* @throws \LKDev\HetznerCloud\APIException
63+
*/
64+
public function getByName(string $name): Location
65+
{
66+
$locations = $this->all($name);
67+
68+
return (count($locations) > 0) ? $locations[0] : null;
69+
}
70+
5671
/**
5772
* @param $input
5873
* @return $this

src/Models/Networks/Network.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function setAdditionalData($data)
188188

189189

190190
$this->labels = $data->labels;
191-
//$this->created = $data->created; //ToDo This should be uncomment, when the backend MR is done
191+
$this->created = $data->created;
192192
return $this;
193193
}
194194

src/Models/Servers/Servers.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function getByName(string $serverName)
7676
$servers = $this->all(new ServerRequestOpts($serverName));
7777

7878
return (count($servers) > 0) ? $servers[0] : null;
79-
8079
}
8180

8281
/**

0 commit comments

Comments
 (0)