Skip to content

Commit 12c60ef

Browse files
authored
fix: ip_addresses_share to remove shared IPs (#316)
## 📝 Description Fix an issue in `ip_addresses_share` to allow it unshare IPs. In the API doc, entering an empty IP array is allowed and it means removing the shared IPs from the Linode. In the previous implementation, we always looked for `ips[0]` when building the request param. It brought the index out of range error when an empty IP array is given. ## ✔️ How to Test `tox`
1 parent 620091f commit 12c60ef

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

linode_api4/groups/networking.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -284,22 +284,32 @@ def ips_share(self, linode, *ips):
284284

285285
def ip_addresses_share(self, ips, linode):
286286
"""
287-
Configure shared IPs. P sharing allows IP address reassignment
287+
Configure shared IPs. IP sharing allows IP address reassignment
288288
(also referred to as IP failover) from one Linode to another if the
289289
primary Linode becomes unresponsive. This means that requests to the primary Linode’s
290290
IP address can be automatically rerouted to secondary Linodes at the configured shared IP addresses.
291291
292+
API Documentation: https://www.linode.com/docs/api/networking/#ip-addresses-share
293+
292294
:param linode: The id of the Instance or the Instance to share the IPAddresses with.
293295
This Instance will be able to bring up the given addresses.
294296
:type: linode: int or Instance
295-
:param ips: Any number of IPAddresses to share to the Instance.
297+
:param ips: Any number of IPAddresses to share to the Instance. Enter an empty array to
298+
remove all shared IP addresses.
296299
:type ips: str or IPAddress
297300
"""
298301

302+
shared_ips = []
303+
for ip in ips:
304+
if isinstance(ip, str):
305+
shared_ips.append(ip)
306+
elif isinstance(ip, IPAddress):
307+
shared_ips.append(ip.address)
308+
else:
309+
shared_ips.append(str(ip)) # and hope that works
310+
299311
params = {
300-
"ips": ips
301-
if not isinstance(ips[0], IPAddress)
302-
else [ip.address for ip in ips],
312+
"ips": shared_ips,
303313
"linode_id": linode
304314
if not isinstance(linode, Instance)
305315
else linode.id,
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/integration/models/test_networking.py

+36
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,39 @@ def test_get_networking_rules(get_client, create_firewall):
1313
assert "inbound_policy" in str(rules)
1414
assert "outbound" in str(rules)
1515
assert "outbound_policy" in str(rules)
16+
17+
18+
@pytest.mark.smoke
19+
def test_ip_addresses_share(self):
20+
"""
21+
Test that you can share IP addresses with Linode.
22+
"""
23+
ip_share_url = "/networking/ips/share"
24+
ips = ["127.0.0.1"]
25+
linode_id = 12345
26+
with self.mock_post(ip_share_url) as m:
27+
result = self.client.networking.ip_addresses_share(ips, linode_id)
28+
29+
self.assertIsNotNone(result)
30+
self.assertEqual(m.call_url, ip_share_url)
31+
self.assertEqual(
32+
m.call_data,
33+
{
34+
"ips": ips,
35+
"linode": linode_id,
36+
},
37+
)
38+
39+
# Test that entering an empty IP array is allowed.
40+
with self.mock_post(ip_share_url) as m:
41+
result = self.client.networking.ip_addresses_share([], linode_id)
42+
43+
self.assertIsNotNone(result)
44+
self.assertEqual(m.call_url, ip_share_url)
45+
self.assertEqual(
46+
m.call_data,
47+
{
48+
"ips": [],
49+
"linode": linode_id,
50+
},
51+
)

0 commit comments

Comments
 (0)