Skip to content

Commit ebef774

Browse files
authored
fix!: make datacenter argument optional when creating a primary ip (#363)
Create a primary ips assigned to a resource without having to pass the datacenter argument.
1 parent 6b977e2 commit ebef774

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

hcloud/primary_ips/client.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,37 +189,34 @@ def get_by_name(self, name: str) -> BoundPrimaryIP | None:
189189
def create(
190190
self,
191191
type: str,
192-
# TODO: Make the datacenter argument optional
193-
datacenter: Datacenter | BoundDatacenter | None,
194192
name: str,
193+
datacenter: Datacenter | BoundDatacenter | None = None,
195194
assignee_type: str | None = "server",
196195
assignee_id: int | None = None,
197196
auto_delete: bool | None = False,
198197
labels: dict | None = None,
199198
) -> CreatePrimaryIPResponse:
200199
"""Creates a new Primary IP assigned to a server.
201200
202-
:param type: str
203-
Primary IP type Choices: ipv4, ipv6
204-
:param assignee_type: str
205-
:param assignee_id: int (optional)
206-
:param datacenter: Datacenter
207-
:param labels: Dict[str, str] (optional)
208-
User-defined labels (key-value pairs)
201+
:param type: str Primary IP type Choices: ipv4, ipv6
209202
:param name: str
203+
:param datacenter: Datacenter (optional)
204+
:param assignee_type: str (optional)
205+
:param assignee_id: int (optional)
210206
:param auto_delete: bool (optional)
207+
:param labels: Dict[str, str] (optional) User-defined labels (key-value pairs)
211208
:return: :class:`CreatePrimaryIPResponse <hcloud.primary_ips.domain.CreatePrimaryIPResponse>`
212209
"""
213210

214211
data: dict[str, Any] = {
212+
"name": name,
215213
"type": type,
216-
"assignee_type": assignee_type,
217214
"auto_delete": auto_delete,
218-
"name": name,
219215
}
220216
if datacenter is not None:
221217
data["datacenter"] = datacenter.id_or_name
222218
if assignee_id is not None:
219+
data["assignee_type"] = assignee_type
223220
data["assignee_id"] = assignee_id
224221
if labels is not None:
225222
data["labels"] = labels

tests/unit/primary_ips/test_client.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ def test_create_with_datacenter(self, primary_ips_client, primary_ip_response):
160160
"type": "ipv6",
161161
"datacenter": "datacenter",
162162
"auto_delete": False,
163-
"assignee_type": "server",
164163
},
165164
)
166165

@@ -179,20 +178,18 @@ def test_create_with_assignee_id(
179178
response = primary_ips_client.create(
180179
type="ipv6",
181180
name="my-ip",
182-
assignee_id=1,
181+
assignee_id=17,
183182
assignee_type="server",
184-
datacenter=Datacenter(name="datacenter"),
185183
)
186184
primary_ips_client._client.request.assert_called_with(
187185
url="/primary_ips",
188186
method="POST",
189187
json={
188+
"name": "my-ip",
190189
"type": "ipv6",
191-
"assignee_id": 1,
190+
"assignee_id": 17,
192191
"assignee_type": "server",
193-
"name": "my-ip",
194192
"auto_delete": False,
195-
"datacenter": "datacenter",
196193
},
197194
)
198195
bound_primary_ip = response.primary_ip

0 commit comments

Comments
 (0)