Skip to content

Commit

Permalink
Implement Floating IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
LKaemmerling committed Jan 29, 2018
1 parent d81776c commit adfb876
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 4 deletions.
109 changes: 107 additions & 2 deletions src/Models/FloatingIps/FloatingIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,112 @@

namespace LKDev\HetznerCloud\Models\FloatingIps;

class FloatingIp
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Locations\Location;
use LKDev\HetznerCloud\Models\Model;

/**
*
*/
class FloatingIp extends Model
{
// ToDo
/**
* @var int
*/
public $id;

/**
* @var string
*/
public $description;

/**
* @var string
*/
public $ip;

/**
* @var int
*/
public $server;

/**
* @var array
*/
public $dnsPtr;

/**
* @var \LKDev\HetznerCloud\Models\Locations\Location
*/
public $homeLocation;

/**
* @var bool
*/
public $blocked;

/**
* FloatingIp constructor.
*
* @param int $id
* @param string $description
* @param string $ip
* @param int $server
* @param array $dnsPtr
* @param \LKDev\HetznerCloud\Models\Locations\Location $homeLocation
* @param bool $blocked
*/
public function __construct(
int $id,
string $description,
string $ip,
int $server,
array $dnsPtr,
Location $homeLocation,
bool $blocked
) {
$this->id = $id;
$this->description = $description;
$this->ip = $ip;
$this->server = $server;
$this->dnsPtr = $dnsPtr;
$this->homeLocation = $homeLocation;
$this->blocked = $blocked;
parent::__construct();
}

/**
* Changes the description of a Floating IP.
*
* @see https://docs.hetzner.cloud/#resources-floating-ips-put
* @param string $description
* @return static
* @throws \LKDev\HetznerCloud\APIException
*/
public function changeDescription(string $description)
{
$response = $this->httpClient->put('floating_ips/'.$this->id, [
'json' => [
'description' => $description,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody())->floating_ip);
}
}

/**
* Deletes a Floating IP. If it is currently assigned to a server it will automatically get unassigned.
*
* @see https://docs.hetzner.cloud/#resources-floating-ips-delete
* @return bool
* @throws \LKDev\HetznerCloud\APIException
*/
public function delete()
{
$response = $this->httpClient->delete('floating_ips/'.$this->id);
if (! HetznerAPIClient::hasError($response)) {
return true;
}
}
}
93 changes: 91 additions & 2 deletions src/Models/FloatingIps/FloatingIps.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,96 @@

namespace LKDev\HetznerCloud\Models\FloatingIps;

class FloatingIps
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Locations\Location;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\Models\Servers\Server;

class FloatingIps extends Model
{
// ToDos
/**
* @var array
*/
public $floatingIps;

/**
* Returns all floating ip objects.
*
* @see https://docs.hetzner.cloud/#resources-floating-ips-get
* @return array
* @throws \LKDev\HetznerCloud\APIException
*/
public function all(): array
{
$response = $this->httpClient->get('floating_ips');
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody()))->floating_ips;
}
}

/**
* Returns a specific floating ip object.
*
* @see https://docs.hetzner.cloud/#resources-floating-ips-get-1
* @param int $locationId
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
* @throws \LKDev\HetznerCloud\APIException
*/
public function get(int $floatingIpId): FloatingIp
{
$response = $this->httpClient->get('floating_ips/'.$floatingIpId);
if (! HetznerAPIClient::hasError($response)) {
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
}
}

/**
* Creates a new Floating IP assigned to a server.
*
* @see https://docs.hetzner.cloud/#resources-floating-ips-post
* @param string $type
* @param string|null $description
* @param \LKDev\HetznerCloud\Models\Locations\Location|null $location
* @param \LKDev\HetznerCloud\Models\Servers\Server|null $server
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
* @throws \LKDev\HetznerCloud\APIException
*/
public function create(
string $type,
string $description = null,
Location $location = null,
Server $server = null
): FloatingIp {
$response = $this->httpClient->post('floating_ips', [
'type' => $type,
'description' => $description,
'server' => $server ?: $server->id,
'location' => $location ?: $location->id,
]);
if (! HetznerAPIClient::hasError($response)) {
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
}
}

/**
* @param $input
* @return $this
*/
public function setAdditionalData($input)
{
$this->floatingIps = collect($input->floating_ips)->map(function ($floatingIp, $key) {
return FloatingIp::parse($floatingIp);
})->toArray();

return $this;
}

/**
* @param $input
* @return $this|static
*/
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
}

0 comments on commit adfb876

Please sign in to comment.