88
99namespace 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