Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve server typing #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## unreleased
* Feature: Add Certificate Support
* Improvements: Improve typing and structure of Server response

## 2.2.1 (22.07.2020)
* Make package requirements less strict
* Bugfix: getByName functions had a wrong return type and failed when a resource was not found by name [#50](https://github.com/LKDevelopment/hetzner-cloud-php-sdk/issues/50)
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Certificates/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function delete(): bool
*/
public static function parse($input)
{
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);
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, get_object_vars($input->labels));
}

/**
Expand Down
35 changes: 24 additions & 11 deletions src/Models/Servers/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class Server extends Model implements Resource
public $created;

/**
* @var array
* @var ServerPublicNet
*/
public $publicNet;

/**
* @var array
* @var ServerPrivateNet[]
*/
public $privateNet;
/**
Expand Down Expand Up @@ -103,7 +103,7 @@ class Server extends Model implements Resource
public $includedTraffic;

/**
* @var array|\LKDev\HetznerCloud\Models\Protection
* @var \LKDev\HetznerCloud\Models\Protection
*/
public $protection;

Expand Down Expand Up @@ -140,27 +140,40 @@ public function setAdditionalData($data)
{
$this->name = $data->name;
$this->status = $data->status ?: null;
$this->publicNet = $data->public_net ?: null;
$this->privateNet = property_exists($data, 'private_net') ? $data->private_net : [];
$this->serverType = $data->server_type ?: ServerType::parse($data->server_type);
$this->datacenter = $data->datacenter ?: Datacenter::parse($data->datacenter);
$this->publicNet = property_exists($data, 'public_net') ? ServerPublicNet::parse($data->public_net) : null;
$this->privateNet = property_exists($data, 'private_net') ? $this->parsePrivateNet($data->private_net) : [];
$this->serverType = property_exists($data, 'server_type') ? ServerType::parse($data->server_type) : null;
$this->datacenter = property_exists($data, 'datacenter') ? Datacenter::parse($data->datacenter) : null;
$this->created = $data->created;
$this->image = $data->image ?: Image::parse($data->image);
$this->iso = $data->iso ?: ISO::parse($data->iso);
$this->image = property_exists($data, 'image') ? Image::parse($data->image) : null;
$this->iso = property_exists($data, 'iso') ? ISO::parse($data->iso) : null;
$this->rescueEnabled = $data->rescue_enabled ?: null;
$this->locked = $data->locked ?: null;
$this->backupWindow = $data->backup_window ?: null;
$this->outgoingTraffic = $data->outgoing_traffic ?: null;
$this->ingoingTraffic = $data->ingoing_traffic ?: null;
$this->includedTraffic = $data->included_traffic ?: null;
$this->volumes = property_exists($data, 'volumes') ? $data->volumes : [];
$this->protection = $data->protection ?: Protection::parse($data->protection);
$this->labels = $data->labels;
$this->labels = get_object_vars($data->labels);
$this->primaryDiskSize = $data->primary_disk_size ?: null;

return $this;
}

/**
* @param \stdClass[] $privateNets
* @return ServerPrivateNet[]
*/
protected function parsePrivateNet(array $privateNets)
{
$parsed = [];
foreach ($privateNets as $privateNet) {
$parsed[] = new ServerPrivateNet($privateNet->network, $privateNet->ip, $privateNet->alias_ips, $privateNet->mac_address);
}

return $parsed;
}

/**
* Reload the data of the server.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Models/Servers/ServerPrivateNet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace LKDev\HetznerCloud\Models\Servers;

class ServerPrivateNet
{
/**
* @var int
*/
public $network;
/**
* @var string
*/
public $ip;
/**
* @var string[]
*/
public $aliasIps;
/**
* @var string
*/
public $macAddress;

/**
* ServerPrivateNet constructor.
* @param int $network
* @param string $ip
* @param string[] $aliasIps
* @param string $macAddress
*/
public function __construct(int $network, string $ip, array $aliasIps, string $macAddress)
{
$this->network = $network;
$this->ip = $ip;
$this->aliasIps = $aliasIps;
$this->macAddress = $macAddress;
}
}
44 changes: 44 additions & 0 deletions src/Models/Servers/ServerPublicNet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace LKDev\HetznerCloud\Models\Servers;

/**
* Class ServerPublicNet.
*/
class ServerPublicNet
{
/**
* @var ServerPublicNetIPv4
*/
public $ipv4;
/**
* @var ServerPublicNetIPv6
*/
public $ipv6;
/**
* @var int[]
*/
public $floatingIps;

/**
* ServerPublicNet constructor.
* @param ServerPublicNetIPv4 $ipv4
* @param ServerPublicNetIPv6 $ipv6
* @param array $floatingIps
*/
public function __construct(ServerPublicNetIPv4 $ipv4, ServerPublicNetIPv6 $ipv6, array $floatingIps)
{
$this->ipv4 = $ipv4;
$this->ipv6 = $ipv6;
$this->floatingIps = $floatingIps;
}

/**
* @param \stdClass $data
* @return ServerPublicNet
*/
public static function parse(\stdClass $data)
{
return new ServerPublicNet(ServerPublicNetIPv4::parse($data->ipv4), ServerPublicNetIPv6::parse($data->ipv6), $data->floating_ips);
}
}
44 changes: 44 additions & 0 deletions src/Models/Servers/ServerPublicNetIPv4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace LKDev\HetznerCloud\Models\Servers;

/**
* Class ServerPublicNetIPv4.
*/
class ServerPublicNetIPv4
{
/**
* @var string
*/
public $ip;
/**
* @var bool
*/
public $blocked;
/**
* @var string
*/
public $dnsPtr;

/**
* ServerPublicNetIPv4 constructor.
* @param string $ip
* @param bool $blocked
* @param string $dnsPtr
*/
public function __construct(string $ip, bool $blocked, string $dnsPtr)
{
$this->ip = $ip;
$this->blocked = $blocked;
$this->dnsPtr = $dnsPtr;
}

/**
* @param \stdClass $data
* @return ServerPublicNetIPv4
*/
public static function parse(\stdClass $data)
{
return new self($data->ip, $data->blocked, $data->dns_ptr);
}
}
49 changes: 49 additions & 0 deletions src/Models/Servers/ServerPublicNetIPv6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace LKDev\HetznerCloud\Models\Servers;

/**
* Class ServerPublicNetIPv6.
*/
class ServerPublicNetIPv6
{
/**
* @var string
*/
public $ip;
/**
* @var bool
*/
public $blocked;
/**
* @var array
*/
public $dnsPtr;

/**
* ServerPublicNetIPv6 constructor.
* @param string $ip
* @param bool $blocked
* @param array $dnsPtr
*/
public function __construct(string $ip, bool $blocked, array $dnsPtr)
{
$this->ip = $ip;
$this->blocked = $blocked;
$this->dnsPtr = $dnsPtr;
}

/**
* @param \stdClass $data
* @return ServerPublicNetIPv6
*/
public static function parse(\stdClass $data)
{
$dnsPtrs = [];
foreach ($data->dns_ptr as $dnsPtr) {
$dnsPtrs[] = new ServerPublicNetIPv6DnsPtr($dnsPtr->ip, $dnsPtr->dns_ptr);
}

return new self($data->ip, $data->blocked, $dnsPtrs);
}
}
30 changes: 30 additions & 0 deletions src/Models/Servers/ServerPublicNetIPv6DnsPtr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace LKDev\HetznerCloud\Models\Servers;

/**
* Class ServerPublicNetIPv6DnsPtr.
*/
class ServerPublicNetIPv6DnsPtr
{
/**
* @var string
*/
public $ip;

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

/**
* ServerPublicNetIPv6DnsPtr constructor.
* @param string $ip
* @param string $dnsPtr
*/
public function __construct(string $ip, string $dnsPtr)
{
$this->ip = $ip;
$this->dnsPtr = $dnsPtr;
}
}