Skip to content

Commit bfdb1c4

Browse files
Add support for the networks parameter on server creation (#78)
Co-authored-by: Lukas Kämmerling <github@lukas-kaemmerling.de>
1 parent e153e31 commit bfdb1c4

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
History
33
=======
44

5+
master (XXXX-XX-XX)
6+
--------------------
7+
* Feature: Add support for the optional 'networks' parameter on server creation.
8+
59
1.6.3 (2020-01-09)
610
--------------------
711

hcloud/servers/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def create(self,
392392
image, # type: Image
393393
ssh_keys=None, # type: Optional[List[SSHKey]]
394394
volumes=None, # type: Optional[List[Volume]]
395+
networks=None, # type: Optional[List[Network]]
395396
user_data=None, # type: Optional[str]
396397
labels=None, # type: Optional[Dict[str, str]]
397398
location=None, # type: Optional[Location]
@@ -412,6 +413,8 @@ def create(self,
412413
SSH keys which should be injected into the server at creation time
413414
:param volumes: List[:class:`BoundVolume <hcloud.volumes.client.BoundVolume>` or :class:`Volume <hcloud.volumes.domain.Volume>`] (optional)
414415
Volumes which should be attached to the server at the creation time. Volumes must be in the same location.
416+
:param networks: List[:class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`] (optional)
417+
Networks which should be attached to the server at the creation time.
415418
:param user_data: str (optional)
416419
Cloud-Init user data to use during server creation. This field is limited to 32KiB.
417420
:param labels: Dict[str,str] (optional)
@@ -439,6 +442,8 @@ def create(self,
439442
data['ssh_keys'] = [ssh_key.id_or_name for ssh_key in ssh_keys]
440443
if volumes is not None:
441444
data['volumes'] = [volume.id for volume in volumes]
445+
if networks is not None:
446+
data['networks'] = [network.id for network in networks]
442447
if user_data is not None:
443448
data['user_data'] = user_data
444449
if labels is not None:

tests/integration/servers/test_servers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def test_create(self, hetzner_client):
199199
image=Image(name="ubuntu-16.04"),
200200
ssh_keys=[SSHKey(name="my-ssh-key")],
201201
volumes=[Volume(id=1)],
202+
networks=[Network(id=1)],
202203
user_data="#cloud-config\\nruncmd:\\n- [touch, /root/cloud-init-worked]\\n",
203204
location=Location(name="nbg1"),
204205
automount=False

tests/unit/servers/test_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,46 @@ def test_create_with_volumes(self, servers_client, response_create_simple_server
514514

515515
assert next_actions[0].id == 13
516516

517+
def test_create_with_networks(self, servers_client, response_create_simple_server):
518+
servers_client._client.request.return_value = response_create_simple_server
519+
networks = [Network(id=1), BoundNetwork(mock.MagicMock(), dict(id=2))]
520+
response = servers_client.create(
521+
"my-server",
522+
server_type=ServerType(name="cx11"),
523+
image=Image(id=4711),
524+
networks=networks,
525+
start_after_create=False
526+
)
527+
servers_client._client.request.assert_called_with(
528+
url="/servers",
529+
method="POST",
530+
json={
531+
'name': "my-server",
532+
'server_type': "cx11",
533+
'image': 4711,
534+
'networks': [1, 2],
535+
"start_after_create": False
536+
}
537+
)
538+
539+
bound_server = response.server
540+
bound_action = response.action
541+
next_actions = response.next_actions
542+
root_password = response.root_password
543+
544+
assert root_password == "YItygq1v3GYjjMomLaKc"
545+
546+
assert bound_server._client is servers_client
547+
assert bound_server.id == 1
548+
assert bound_server.name == "my-server"
549+
550+
assert isinstance(bound_action, BoundAction)
551+
assert bound_action._client == servers_client._client.actions
552+
assert bound_action.id == 1
553+
assert bound_action.command == "create_server"
554+
555+
assert next_actions[0].id == 13
556+
517557
@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
518558
def test_get_actions_list(self, servers_client, server, response_get_actions):
519559
servers_client._client.request.return_value = response_get_actions

0 commit comments

Comments
 (0)