Skip to content

Commit

Permalink
fix!: make datacenter argument optional when creating a primary ip (#…
Browse files Browse the repository at this point in the history
…363)

Create a primary ips assigned to a resource without having to pass the
datacenter argument.
  • Loading branch information
jooola committed Jul 2, 2024
1 parent 6b977e2 commit ebef774
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
19 changes: 8 additions & 11 deletions hcloud/primary_ips/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,37 +189,34 @@ 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,
labels: dict | None = None,
) -> 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 <hcloud.primary_ips.domain.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
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/primary_ips/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
)

Expand All @@ -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
Expand Down

0 comments on commit ebef774

Please sign in to comment.