Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: make datacenter argument optional when creating a primary ip #363

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
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