From 745e9b21c8782d04daf1ab57b197576ebfe89462 Mon Sep 17 00:00:00 2001 From: DoctorJohn Date: Thu, 27 Feb 2020 05:33:55 +0100 Subject: [PATCH 1/4] Add support for the networks parameter on server creation --- hcloud/servers/client.py | 5 +++ tests/integration/servers/test_servers.py | 1 + tests/unit/servers/test_client.py | 40 +++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/hcloud/servers/client.py b/hcloud/servers/client.py index 70fd8167..f36797a7 100644 --- a/hcloud/servers/client.py +++ b/hcloud/servers/client.py @@ -392,6 +392,7 @@ def create(self, image, # type: Image ssh_keys=None, # type: Optional[List[SSHKey]] volumes=None, # type: Optional[List[Volume]] + networks=None, # type: Optional[List[Network]] user_data=None, # type: Optional[str] labels=None, # type: Optional[Dict[str, str]] location=None, # type: Optional[Location] @@ -412,6 +413,8 @@ def create(self, SSH keys which should be injected into the server at creation time :param volumes: List[:class:`BoundVolume ` or :class:`Volume `] (optional) Volumes which should be attached to the server at the creation time. Volumes must be in the same location. + :param networks: List[:class:`BoundNetwork ` or :class:`Network `] (optional) + Network which should be attached to the server private network interface at the creation time. :param user_data: str (optional) Cloud-Init user data to use during server creation. This field is limited to 32KiB. :param labels: Dict[str,str] (optional) @@ -439,6 +442,8 @@ def create(self, data['ssh_keys'] = [ssh_key.id_or_name for ssh_key in ssh_keys] if volumes is not None: data['volumes'] = [volume.id for volume in volumes] + if networks is not None: + data['networks'] = [network.id for network in networks] if user_data is not None: data['user_data'] = user_data if labels is not None: diff --git a/tests/integration/servers/test_servers.py b/tests/integration/servers/test_servers.py index bd2baf9c..52a8d4a2 100644 --- a/tests/integration/servers/test_servers.py +++ b/tests/integration/servers/test_servers.py @@ -199,6 +199,7 @@ def test_create(self, hetzner_client): image=Image(name="ubuntu-16.04"), ssh_keys=[SSHKey(name="my-ssh-key")], volumes=[Volume(id=1)], + networks=[Network(id=1)], user_data="#cloud-config\\nruncmd:\\n- [touch, /root/cloud-init-worked]\\n", location=Location(name="nbg1"), automount=False diff --git a/tests/unit/servers/test_client.py b/tests/unit/servers/test_client.py index 6153d0e3..a67bdbfb 100644 --- a/tests/unit/servers/test_client.py +++ b/tests/unit/servers/test_client.py @@ -514,6 +514,46 @@ def test_create_with_volumes(self, servers_client, response_create_simple_server assert next_actions[0].id == 13 + def test_create_with_networks(self, servers_client, response_create_simple_server): + servers_client._client.request.return_value = response_create_simple_server + networks = [Network(id=1), BoundNetwork(mock.MagicMock(), dict(id=2))] + response = servers_client.create( + "my-server", + server_type=ServerType(name="cx11"), + image=Image(id=4711), + networks=networks, + start_after_create=False + ) + servers_client._client.request.assert_called_with( + url="/servers", + method="POST", + json={ + 'name': "my-server", + 'server_type': "cx11", + 'image': 4711, + 'networks': [1, 2], + "start_after_create": False + } + ) + + bound_server = response.server + bound_action = response.action + next_actions = response.next_actions + root_password = response.root_password + + assert root_password == "YItygq1v3GYjjMomLaKc" + + assert bound_server._client is servers_client + assert bound_server.id == 1 + assert bound_server.name == "my-server" + + assert isinstance(bound_action, BoundAction) + assert bound_action._client == servers_client._client.actions + assert bound_action.id == 1 + assert bound_action.command == "create_server" + + assert next_actions[0].id == 13 + @pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))]) def test_get_actions_list(self, servers_client, server, response_get_actions): servers_client._client.request.return_value = response_get_actions From c477356cac51d992625efbd9ccae4df06cb184f9 Mon Sep 17 00:00:00 2001 From: DoctorJohn Date: Thu, 27 Feb 2020 11:16:42 +0100 Subject: [PATCH 2/4] Tweak the server creation methods networks parameters docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lukas Kämmerling --- hcloud/servers/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hcloud/servers/client.py b/hcloud/servers/client.py index f36797a7..5876716b 100644 --- a/hcloud/servers/client.py +++ b/hcloud/servers/client.py @@ -414,7 +414,7 @@ def create(self, :param volumes: List[:class:`BoundVolume ` or :class:`Volume `] (optional) Volumes which should be attached to the server at the creation time. Volumes must be in the same location. :param networks: List[:class:`BoundNetwork ` or :class:`Network `] (optional) - Network which should be attached to the server private network interface at the creation time. + Networks which should be attached to the server at the creation time. :param user_data: str (optional) Cloud-Init user data to use during server creation. This field is limited to 32KiB. :param labels: Dict[str,str] (optional) From 7992e93b3362645a6ec24869fd63ed9a2fcb9472 Mon Sep 17 00:00:00 2001 From: DoctorJohn Date: Thu, 27 Feb 2020 14:21:49 +0100 Subject: [PATCH 3/4] Log the addition of the server creation networks parameter in the changelog --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 52b8d15d..f7e3fd68 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ History ======= +master (XXXX-XX-XX) +-------------------- +* Feature: Add support for the optional 'networks' paramater on server creation. + 1.6.3 (2020-01-09) -------------------- From 9bd987ba4d132fce3790fdf24311eb27688a6428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 27 Feb 2020 14:23:16 +0100 Subject: [PATCH 4/4] Update CHANGELOG.rst --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f7e3fd68..e7688ee1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ History master (XXXX-XX-XX) -------------------- -* Feature: Add support for the optional 'networks' paramater on server creation. +* Feature: Add support for the optional 'networks' parameter on server creation. 1.6.3 (2020-01-09) --------------------