Skip to content

Commit 3535a89

Browse files
committed
TPT-4278: python-sdk: Implement support for Reserved IP for IPv4
1 parent 220daed commit 3535a89

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

linode_api4/groups/networking.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ def ip_allocate(
364364
:returns: The new IPAddress.
365365
:rtype: IPAddress
366366
"""
367+
if not reserved and linode is None:
368+
raise ValueError(
369+
"linode is required when reserved is False."
370+
)
371+
if reserved and linode is None and region is None:
372+
raise ValueError(
373+
"Either linode or region must be provided when reserved is True."
374+
)
375+
if not reserved and region is not None:
376+
raise ValueError(
377+
"region is only valid when reserved is True."
378+
)
379+
367380
data = {
368381
"type": "ipv4",
369382
"public": public,

linode_api4/groups/tag.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create(
3333
nodebalancers=None,
3434
volumes=None,
3535
reserved_ipv4_addresses=None,
36-
entities=[],
36+
entities=None,
3737
):
3838
"""
3939
Creates a new Tag and optionally applies it to the given entities.
@@ -69,6 +69,7 @@ def create(
6969
:returns: The new Tag
7070
:rtype: Tag
7171
"""
72+
entities = entities or []
7273
linode_ids, nodebalancer_ids, domain_ids, volume_ids = [], [], [], []
7374

7475
# filter input into lists of ids

test/unit/groups/networking_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,42 @@ def test_ip_allocate_ephemeral(self):
199199
assert "reserved" not in body
200200
assert ip.linode_id == 456
201201
assert ip.reserved is False
202+
203+
def test_ip_allocate_requires_linode_when_not_reserved(self):
204+
"""
205+
Tests that ip_allocate rejects ephemeral allocation without a linode.
206+
"""
207+
with MethodMock("post", {}) as m:
208+
with self.assertRaises(ValueError) as ctx:
209+
self.client.networking.ip_allocate()
210+
211+
assert str(ctx.exception) == (
212+
"linode is required when reserved is False."
213+
)
214+
assert m.called is False
215+
216+
def test_ip_allocate_requires_linode_or_region_when_reserved(self):
217+
"""
218+
Tests that ip_allocate rejects reserved allocation without a linode or region.
219+
"""
220+
with MethodMock("post", {}) as m:
221+
with self.assertRaises(ValueError) as ctx:
222+
self.client.networking.ip_allocate(reserved=True)
223+
224+
assert str(ctx.exception) == (
225+
"Either linode or region must be provided when reserved is True."
226+
)
227+
assert m.called is False
228+
229+
def test_ip_allocate_rejects_region_when_not_reserved(self):
230+
"""
231+
Tests that ip_allocate rejects region when reserved is False.
232+
"""
233+
with MethodMock("post", {}) as m:
234+
with self.assertRaises(ValueError) as ctx:
235+
self.client.networking.ip_allocate(region="us-east", linode=456)
236+
237+
assert str(ctx.exception) == (
238+
"region is only valid when reserved is True."
239+
)
240+
assert m.called is False

0 commit comments

Comments
 (0)