Skip to content
Merged
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
14 changes: 9 additions & 5 deletions src/Models/Servers/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function deleteById(int $serverId): ?Action
* @param array $networks
* @param array $labels
* @param array $firewalls
* @param array $public_net
* @param array|null $public_net
* @param int|null $placement_group
* @return APIResponse|null
*
Expand All @@ -174,7 +174,7 @@ public function createInDatacenter(
$networks = [],
array $labels = [],
array $firewalls = [],
array $public_net = [],
?array $public_net = null,
?int $placement_group = null
): ?APIResponse {
$parameters = [
Expand All @@ -188,8 +188,10 @@ public function createInDatacenter(
'volumes' => $volumes,
'automount' => $automount,
'networks' => $networks,
'public_net' => $public_net,
];
if ($public_net !== null) {
$parameters['public_net'] = $public_net;
}
if (! empty($labels)) {
$parameters['labels'] = $labels;
}
Expand Down Expand Up @@ -252,7 +254,7 @@ public function createInLocation(string $name,
array $networks = [],
array $labels = [],
array $firewalls = [],
array $public_net = [],
?array $public_net = null,
?int $placement_group = null
): ?APIResponse {
$parameters = [
Expand All @@ -266,8 +268,10 @@ public function createInLocation(string $name,
'volumes' => $volumes,
'automount' => $automount,
'networks' => $networks,
'public_net' => $public_net,
];
if ($public_net !== null) {
$parameters['public_net'] = $public_net;
}
if (! empty($labels)) {
$parameters['labels'] = $labels;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Models/Servers/ServersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
namespace LKDev\Tests\Integration\Servers;

use GuzzleHttp\Psr7\Response;
use LKDev\HetznerCloud\Models\Datacenters\Datacenter;
use LKDev\HetznerCloud\Models\Images\Image;
use LKDev\HetznerCloud\Models\Locations\Location;
use LKDev\HetznerCloud\Models\Servers\Servers;
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
use LKDev\Tests\TestCase;

class ServersTest extends TestCase
Expand Down Expand Up @@ -68,4 +72,44 @@ public function testList()
$this->assertEquals($servers[0]->name, 'my-server');
$this->assertLastRequestEquals('GET', '/servers');
}

public function testCreateInLocationOmitsPublicNetByDefault()
{
$this->mockHandler->append(new Response(201, [], file_get_contents(__DIR__.'/fixtures/server_create.json')));
$this->servers->createInLocation('my-server', new ServerType(1), new Image(4711), new Location(1, 'fsn1'));

$body = json_decode((string) $this->mockHandler->getLastRequest()->getBody(), true);
$this->assertArrayNotHasKey('public_net', $body);
$this->assertLastRequestEquals('POST', '/servers');
}

public function testCreateInLocationSendsPublicNetWhenProvided()
{
$this->mockHandler->append(new Response(201, [], file_get_contents(__DIR__.'/fixtures/server_create.json')));
$publicNet = ['enable_ipv4' => true, 'enable_ipv6' => false];
$this->servers->createInLocation('my-server', new ServerType(1), new Image(4711), new Location(1, 'fsn1'), public_net: $publicNet);

$this->assertLastRequestEquals('POST', '/servers');
$this->assertLastRequestBodyParametersEqual(['public_net' => $publicNet]);
}

public function testCreateInDatacenterOmitsPublicNetByDefault()
{
$this->mockHandler->append(new Response(201, [], file_get_contents(__DIR__.'/fixtures/server_create.json')));
$this->servers->createInDatacenter('my-server', new ServerType(1), new Image(4711), new Datacenter(1, 'fsn1-dc8', 'Falkenstein 1 DC 8', new Location(1, 'fsn1')));

$body = json_decode((string) $this->mockHandler->getLastRequest()->getBody(), true);
$this->assertArrayNotHasKey('public_net', $body);
$this->assertLastRequestEquals('POST', '/servers');
}

public function testCreateInDatacenterSendsPublicNetWhenProvided()
{
$this->mockHandler->append(new Response(201, [], file_get_contents(__DIR__.'/fixtures/server_create.json')));
$publicNet = ['enable_ipv4' => true, 'enable_ipv6' => false];
$this->servers->createInDatacenter('my-server', new ServerType(1), new Image(4711), new Datacenter(1, 'fsn1-dc8', 'Falkenstein 1 DC 8', new Location(1, 'fsn1')), public_net: $publicNet);

$this->assertLastRequestEquals('POST', '/servers');
$this->assertLastRequestBodyParametersEqual(['public_net' => $publicNet]);
}
}
43 changes: 43 additions & 0 deletions tests/Unit/Models/Servers/fixtures/server_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"server": {
"id": 42,
"name": "my-server",
"status": "running",
"created": "2016-01-30T23:50:00+00:00",
"public_net": {
"ipv4": { "ip": "1.2.3.4", "blocked": false, "dns_ptr": "server01.example.com" },
"ipv6": { "ip": "2001:db8::/64", "blocked": false, "dns_ptr": [] },
"floating_ips": []
},
"private_net": [],
"server_type": {
"id": 1, "name": "cx11", "description": "CX11", "cores": 1, "memory": 1, "disk": 25,
"deprecated": false, "prices": [], "storage_type": "local", "cpu_type": "shared"
},
"datacenter": {
"id": 1, "name": "fsn1-dc8", "description": "Falkenstein 1 DC 8",
"location": { "id": 1, "name": "fsn1", "description": "Falkenstein DC Park 1", "country": "DE", "city": "Falkenstein", "latitude": 50.47612, "longitude": 12.370071, "network_zone": "eu-central" },
"server_types": { "supported": [], "available": [], "available_for_migration": [] }
},
"image": { "id": 4711, "type": "snapshot", "status": "available", "name": null, "description": "my snapshot", "image_size": 2.3, "disk_size": 10, "created": "2016-01-30T23:50:00+00:00", "created_from": null, "bound_to": null, "os_flavor": "ubuntu", "os_version": "16.04", "rapid_deploy": false, "protection": { "delete": false }, "deprecated": null, "labels": {} },
"iso": null,
"rescue_enabled": false,
"locked": false,
"backup_window": null,
"outgoing_traffic": null,
"ingoing_traffic": null,
"included_traffic": null,
"protection": { "delete": false, "rebuild": false },
"labels": {},
"volumes": [],
"load_balancers": [],
"placement_group": null
},
"action": {
"id": 1, "command": "create_server", "status": "success", "progress": 100,
"started": "2016-01-30T23:55:00+00:00", "finished": "2016-01-30T23:56:00+00:00",
"resources": [{ "id": 42, "type": "server" }],
"error": { "code": "action_failed", "message": "Action failed" }
},
"next_actions": []
}
Loading