From 3d34391c0960603b4522dac0bbe57f861e20974b Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 1 Jul 2024 18:38:13 +0200 Subject: [PATCH] fix!: make `datacenter` argument optional when creating a primary ip Create a primary ips assigned to a resource without having to pass the datacenter argument. --- hcloud/primary_ips/client.py | 19 ++++++++----------- tests/unit/primary_ips/test_client.py | 9 +++------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/hcloud/primary_ips/client.py b/hcloud/primary_ips/client.py index ece8d88..40b47cb 100644 --- a/hcloud/primary_ips/client.py +++ b/hcloud/primary_ips/client.py @@ -189,9 +189,8 @@ def get_by_name(self, name: str) -> BoundPrimaryIP | None: def create( self, type: str, - # TODO: Make the datacenter argument optional - datacenter: Datacenter | BoundDatacenter | None, name: str, + datacenter: Datacenter | BoundDatacenter | None = None, assignee_type: str | None = "server", assignee_id: int | None = None, auto_delete: bool | None = False, @@ -199,27 +198,25 @@ def create( ) -> CreatePrimaryIPResponse: """Creates a new Primary IP assigned to a server. - :param type: str - Primary IP type Choices: ipv4, ipv6 - :param assignee_type: str - :param assignee_id: int (optional) - :param datacenter: Datacenter - :param labels: Dict[str, str] (optional) - User-defined labels (key-value pairs) + :param type: str Primary IP type Choices: ipv4, ipv6 :param name: str + :param datacenter: Datacenter (optional) + :param assignee_type: str (optional) + :param assignee_id: int (optional) :param auto_delete: bool (optional) + :param labels: Dict[str, str] (optional) User-defined labels (key-value pairs) :return: :class:`CreatePrimaryIPResponse ` """ data: dict[str, Any] = { + "name": name, "type": type, - "assignee_type": assignee_type, "auto_delete": auto_delete, - "name": name, } if datacenter is not None: data["datacenter"] = datacenter.id_or_name if assignee_id is not None: + data["assignee_type"] = assignee_type data["assignee_id"] = assignee_id if labels is not None: data["labels"] = labels diff --git a/tests/unit/primary_ips/test_client.py b/tests/unit/primary_ips/test_client.py index 413ed2b..dd4b1aa 100644 --- a/tests/unit/primary_ips/test_client.py +++ b/tests/unit/primary_ips/test_client.py @@ -160,7 +160,6 @@ def test_create_with_datacenter(self, primary_ips_client, primary_ip_response): "type": "ipv6", "datacenter": "datacenter", "auto_delete": False, - "assignee_type": "server", }, ) @@ -179,20 +178,18 @@ def test_create_with_assignee_id( response = primary_ips_client.create( type="ipv6", name="my-ip", - assignee_id=1, + assignee_id=17, assignee_type="server", - datacenter=Datacenter(name="datacenter"), ) primary_ips_client._client.request.assert_called_with( url="/primary_ips", method="POST", json={ + "name": "my-ip", "type": "ipv6", - "assignee_id": 1, + "assignee_id": 17, "assignee_type": "server", - "name": "my-ip", "auto_delete": False, - "datacenter": "datacenter", }, ) bound_primary_ip = response.primary_ip