Skip to content

new: Support deleting IP address #497

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

Merged
merged 1 commit into from
Jan 23, 2025
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
14 changes: 14 additions & 0 deletions linode_api4/objects/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ def to(self, linode):

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

def delete(self):
"""
Override the delete() function from Base to use the correct endpoint.
"""
resp = self._client.delete(
"/linode/instances/{}/ips/{}".format(self.linode_id, self.address),
model=self,
)

if "error" in resp:
return False
self.invalidate()
return True


@dataclass
class VPCIPAddress(JSONObject):
Expand Down
13 changes: 13 additions & 0 deletions test/integration/models/networking/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ def test_network_transfer_prices(test_linode_client):
transfer_prices[0].price is None
or transfer_prices[0].price.hourly >= 0
)


def test_allocate_and_delete_ip(test_linode_client, create_linode):
linode = create_linode
ip = test_linode_client.networking.ip_allocate(linode.id)
linode.invalidate()

assert ip.linode_id == linode.id
assert ip.address in linode.ipv4

is_deleted = ip.delete()

assert is_deleted is True
13 changes: 12 additions & 1 deletion test/unit/objects/networking_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from test.unit.base import ClientBaseCase

from linode_api4 import ExplicitNullValue
from linode_api4 import ExplicitNullValue, Instance
from linode_api4.objects import Firewall, IPAddress, IPv6Range


Expand Down Expand Up @@ -83,3 +83,14 @@ def test_vpc_nat_1_1(self):
self.assertEqual(ip.vpc_nat_1_1.vpc_id, 242)
self.assertEqual(ip.vpc_nat_1_1.subnet_id, 194)
self.assertEqual(ip.vpc_nat_1_1.address, "139.144.244.36")

def test_delete_ip(self):
"""
Tests that deleting an IP creates the correct api request
"""
with self.mock_delete() as m:
ip = IPAddress(self.client, "127.0.0.1")
ip.to(Instance(self.client, 123))
ip.delete()

self.assertEqual(m.call_url, "/linode/instances/123/ips/127.0.0.1")
Loading