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: ip_addresses_share to remove shared IPs #316

Merged
merged 4 commits into from
Aug 15, 2023
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
20 changes: 15 additions & 5 deletions linode_api4/groups/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,32 @@ def ips_share(self, linode, *ips):

def ip_addresses_share(self, ips, linode):
"""
Configure shared IPs. P sharing allows IP address reassignment
Configure shared IPs. IP sharing allows IP address reassignment
(also referred to as IP failover) from one Linode to another if the
primary Linode becomes unresponsive. This means that requests to the primary Linode’s
IP address can be automatically rerouted to secondary Linodes at the configured shared IP addresses.

API Documentation: https://www.linode.com/docs/api/networking/#ip-addresses-share

:param linode: The id of the Instance or the Instance to share the IPAddresses with.
This Instance will be able to bring up the given addresses.
:type: linode: int or Instance
:param ips: Any number of IPAddresses to share to the Instance.
:param ips: Any number of IPAddresses to share to the Instance. Enter an empty array to
remove all shared IP addresses.
:type ips: str or IPAddress
"""

shared_ips = []
for ip in ips:
if isinstance(ip, str):
shared_ips.append(ip)
elif isinstance(ip, IPAddress):
shared_ips.append(ip.address)
else:
shared_ips.append(str(ip)) # and hope that works

params = {
"ips": ips
if not isinstance(ips[0], IPAddress)
else [ip.address for ip in ips],
"ips": shared_ips,
"linode_id": linode
if not isinstance(linode, Instance)
else linode.id,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/networking_ips_share.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
36 changes: 36 additions & 0 deletions test/integration/models/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,39 @@ def test_get_networking_rules(get_client, create_firewall):
assert "inbound_policy" in str(rules)
assert "outbound" in str(rules)
assert "outbound_policy" in str(rules)


@pytest.mark.smoke
def test_ip_addresses_share(self):
"""
Test that you can share IP addresses with Linode.
"""
ip_share_url = "/networking/ips/share"
ips = ["127.0.0.1"]
linode_id = 12345
with self.mock_post(ip_share_url) as m:
result = self.client.networking.ip_addresses_share(ips, linode_id)

self.assertIsNotNone(result)
self.assertEqual(m.call_url, ip_share_url)
self.assertEqual(
m.call_data,
{
"ips": ips,
"linode": linode_id,
},
)

# Test that entering an empty IP array is allowed.
with self.mock_post(ip_share_url) as m:
result = self.client.networking.ip_addresses_share([], linode_id)

self.assertIsNotNone(result)
self.assertEqual(m.call_url, ip_share_url)
self.assertEqual(
m.call_data,
{
"ips": [],
"linode": linode_id,
},
)