Skip to content

Commit c5d519f

Browse files
committed
delete ip
1 parent 8e5134e commit c5d519f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

linode_api4/objects/networking.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ def to(self, linode):
112112

113113
return {"address": self.address, "linode_id": linode.id}
114114

115+
def delete(self):
116+
"""
117+
Override the delete() function from Base to use the correct endpoint.
118+
"""
119+
resp = self._client.delete(
120+
"/linode/instances/{}/ips/{}".format(self.linode_id, self.address),
121+
model=self,
122+
)
123+
124+
if "error" in resp:
125+
return False
126+
self.invalidate()
127+
return True
128+
115129

116130
@dataclass
117131
class VPCIPAddress(JSONObject):

test/integration/models/networking/test_networking.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ def test_network_transfer_prices(test_linode_client):
150150
transfer_prices[0].price is None
151151
or transfer_prices[0].price.hourly >= 0
152152
)
153+
154+
155+
def test_allocate_and_delete_ip(test_linode_client, create_linode):
156+
linode = create_linode
157+
ip = test_linode_client.networking.ip_allocate(linode.id)
158+
linode.invalidate()
159+
160+
assert ip.linode_id == linode.id
161+
assert ip.address in linode.ipv4
162+
163+
is_deleted = ip.delete()
164+
165+
assert is_deleted is True

test/unit/objects/networking_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from test.unit.base import ClientBaseCase
22

3-
from linode_api4 import ExplicitNullValue
3+
from linode_api4 import ExplicitNullValue, Instance
44
from linode_api4.objects import Firewall, IPAddress, IPv6Range
55

66

@@ -83,3 +83,14 @@ def test_vpc_nat_1_1(self):
8383
self.assertEqual(ip.vpc_nat_1_1.vpc_id, 242)
8484
self.assertEqual(ip.vpc_nat_1_1.subnet_id, 194)
8585
self.assertEqual(ip.vpc_nat_1_1.address, "139.144.244.36")
86+
87+
def test_delete_ip(self):
88+
"""
89+
Tests that deleting an IP creates the correct api request
90+
"""
91+
with self.mock_delete() as m:
92+
ip = IPAddress(self.client, "127.0.0.1")
93+
ip.to(Instance(self.client, 123))
94+
ip.delete()
95+
96+
self.assertEqual(m.call_url, "/linode/instances/123/ips/127.0.0.1")

0 commit comments

Comments
 (0)