Skip to content

Commit adfb876

Browse files
committed
Implement Floating IPs
1 parent d81776c commit adfb876

File tree

2 files changed

+198
-4
lines changed

2 files changed

+198
-4
lines changed

src/Models/FloatingIps/FloatingIp.php

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,112 @@
88

99
namespace LKDev\HetznerCloud\Models\FloatingIps;
1010

11-
class FloatingIp
11+
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Locations\Location;
13+
use LKDev\HetznerCloud\Models\Model;
14+
15+
/**
16+
*
17+
*/
18+
class FloatingIp extends Model
1219
{
13-
// ToDo
20+
/**
21+
* @var int
22+
*/
23+
public $id;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $description;
29+
30+
/**
31+
* @var string
32+
*/
33+
public $ip;
34+
35+
/**
36+
* @var int
37+
*/
38+
public $server;
39+
40+
/**
41+
* @var array
42+
*/
43+
public $dnsPtr;
44+
45+
/**
46+
* @var \LKDev\HetznerCloud\Models\Locations\Location
47+
*/
48+
public $homeLocation;
49+
50+
/**
51+
* @var bool
52+
*/
53+
public $blocked;
54+
55+
/**
56+
* FloatingIp constructor.
57+
*
58+
* @param int $id
59+
* @param string $description
60+
* @param string $ip
61+
* @param int $server
62+
* @param array $dnsPtr
63+
* @param \LKDev\HetznerCloud\Models\Locations\Location $homeLocation
64+
* @param bool $blocked
65+
*/
66+
public function __construct(
67+
int $id,
68+
string $description,
69+
string $ip,
70+
int $server,
71+
array $dnsPtr,
72+
Location $homeLocation,
73+
bool $blocked
74+
) {
75+
$this->id = $id;
76+
$this->description = $description;
77+
$this->ip = $ip;
78+
$this->server = $server;
79+
$this->dnsPtr = $dnsPtr;
80+
$this->homeLocation = $homeLocation;
81+
$this->blocked = $blocked;
82+
parent::__construct();
83+
}
84+
85+
/**
86+
* Changes the description of a Floating IP.
87+
*
88+
* @see https://docs.hetzner.cloud/#resources-floating-ips-put
89+
* @param string $description
90+
* @return static
91+
* @throws \LKDev\HetznerCloud\APIException
92+
*/
93+
public function changeDescription(string $description)
94+
{
95+
$response = $this->httpClient->put('floating_ips/'.$this->id, [
96+
'json' => [
97+
'description' => $description,
98+
],
99+
]);
100+
if (! HetznerAPIClient::hasError($response)) {
101+
return self::parse(json_decode((string) $response->getBody())->floating_ip);
102+
}
103+
}
104+
105+
/**
106+
* Deletes a Floating IP. If it is currently assigned to a server it will automatically get unassigned.
107+
*
108+
* @see https://docs.hetzner.cloud/#resources-floating-ips-delete
109+
* @return bool
110+
* @throws \LKDev\HetznerCloud\APIException
111+
*/
112+
public function delete()
113+
{
114+
$response = $this->httpClient->delete('floating_ips/'.$this->id);
115+
if (! HetznerAPIClient::hasError($response)) {
116+
return true;
117+
}
118+
}
14119
}

src/Models/FloatingIps/FloatingIps.php

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,96 @@
88

99
namespace LKDev\HetznerCloud\Models\FloatingIps;
1010

11-
class FloatingIps
11+
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Locations\Location;
13+
use LKDev\HetznerCloud\Models\Model;
14+
use LKDev\HetznerCloud\Models\Servers\Server;
15+
16+
class FloatingIps extends Model
1217
{
13-
// ToDos
18+
/**
19+
* @var array
20+
*/
21+
public $floatingIps;
22+
23+
/**
24+
* Returns all floating ip objects.
25+
*
26+
* @see https://docs.hetzner.cloud/#resources-floating-ips-get
27+
* @return array
28+
* @throws \LKDev\HetznerCloud\APIException
29+
*/
30+
public function all(): array
31+
{
32+
$response = $this->httpClient->get('floating_ips');
33+
if (! HetznerAPIClient::hasError($response)) {
34+
return self::parse(json_decode((string) $response->getBody()))->floating_ips;
35+
}
36+
}
37+
38+
/**
39+
* Returns a specific floating ip object.
40+
*
41+
* @see https://docs.hetzner.cloud/#resources-floating-ips-get-1
42+
* @param int $locationId
43+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
44+
* @throws \LKDev\HetznerCloud\APIException
45+
*/
46+
public function get(int $floatingIpId): FloatingIp
47+
{
48+
$response = $this->httpClient->get('floating_ips/'.$floatingIpId);
49+
if (! HetznerAPIClient::hasError($response)) {
50+
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
51+
}
52+
}
53+
54+
/**
55+
* Creates a new Floating IP assigned to a server.
56+
*
57+
* @see https://docs.hetzner.cloud/#resources-floating-ips-post
58+
* @param string $type
59+
* @param string|null $description
60+
* @param \LKDev\HetznerCloud\Models\Locations\Location|null $location
61+
* @param \LKDev\HetznerCloud\Models\Servers\Server|null $server
62+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
63+
* @throws \LKDev\HetznerCloud\APIException
64+
*/
65+
public function create(
66+
string $type,
67+
string $description = null,
68+
Location $location = null,
69+
Server $server = null
70+
): FloatingIp {
71+
$response = $this->httpClient->post('floating_ips', [
72+
'type' => $type,
73+
'description' => $description,
74+
'server' => $server ?: $server->id,
75+
'location' => $location ?: $location->id,
76+
]);
77+
if (! HetznerAPIClient::hasError($response)) {
78+
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
79+
}
80+
}
81+
82+
/**
83+
* @param $input
84+
* @return $this
85+
*/
86+
public function setAdditionalData($input)
87+
{
88+
$this->floatingIps = collect($input->floating_ips)->map(function ($floatingIp, $key) {
89+
return FloatingIp::parse($floatingIp);
90+
})->toArray();
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param $input
97+
* @return $this|static
98+
*/
99+
public static function parse($input)
100+
{
101+
return (new self())->setAdditionalData($input);
102+
}
14103
}

0 commit comments

Comments
 (0)