Skip to content

Commit c8f4bd5

Browse files
authored
new: List ips under a specific VPC (#391)
* list ips under a vpc * fix
1 parent 0951c34 commit c8f4bd5

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

linode_api4/objects/vpc.py

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from linode_api4.errors import UnexpectedResponseError
55
from linode_api4.objects import Base, DerivedBase, Property, Region
66
from linode_api4.objects.serializable import JSONObject
7+
from linode_api4.paginated_list import PaginatedList
78

89

910
@dataclass
@@ -97,3 +98,23 @@ def subnet_create(
9798

9899
d = VPCSubnet(self._client, result["id"], self.id, result)
99100
return d
101+
102+
@property
103+
def ips(self, *filters) -> PaginatedList:
104+
"""
105+
Get all the IP addresses under this VPC.
106+
107+
API Documentation: TODO
108+
109+
:returns: A list of VPCIPAddresses the acting user can access.
110+
:rtype: PaginatedList of VPCIPAddress
111+
"""
112+
113+
# need to avoid circular import
114+
from linode_api4.objects import ( # pylint: disable=import-outside-toplevel
115+
VPCIPAddress,
116+
)
117+
118+
return self._client._get_and_filter(
119+
VPCIPAddress, *filters, endpoint="/vpcs/{}/ips".format(self.id)
120+
)

test/fixtures/vpcs_123456_ips.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"data": [
3+
{
4+
"address": "10.0.0.2",
5+
"address_range": null,
6+
"vpc_id": 123456,
7+
"subnet_id": 654321,
8+
"region": "us-ord",
9+
"linode_id": 111,
10+
"config_id": 222,
11+
"interface_id": 333,
12+
"active": true,
13+
"nat_1_1": null,
14+
"gateway": "10.0.0.1",
15+
"prefix": 8,
16+
"subnet_mask": "255.0.0.0"
17+
},
18+
{
19+
"address": "10.0.0.3",
20+
"address_range": null,
21+
"vpc_id": 41220,
22+
"subnet_id": 41184,
23+
"region": "us-ord",
24+
"linode_id": 56323949,
25+
"config_id": 59467106,
26+
"interface_id": 1248358,
27+
"active": true,
28+
"nat_1_1": null,
29+
"gateway": "10.0.0.1",
30+
"prefix": 8,
31+
"subnet_mask": "255.0.0.0"
32+
}
33+
]
34+
}

test/integration/conftest.py

-16
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,6 @@ def create_vpc_with_subnet_and_linode(
316316
instance.delete()
317317

318318

319-
@pytest.fixture(scope="session")
320-
def create_vpc(test_linode_client):
321-
client = test_linode_client
322-
323-
timestamp = str(int(time.time_ns() % 10**10))
324-
325-
vpc = client.vpcs.create(
326-
"pythonsdk-" + timestamp,
327-
get_region(test_linode_client, {"VPCs"}),
328-
description="test description",
329-
)
330-
yield vpc
331-
332-
vpc.delete()
333-
334-
335319
@pytest.fixture(scope="session")
336320
def create_multiple_vpcs(test_linode_client):
337321
client = test_linode_client

test/integration/models/test_linode.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
ConfigInterface,
1616
ConfigInterfaceIPv4,
1717
Disk,
18-
Image,
1918
Instance,
2019
Type,
2120
)
@@ -643,6 +642,14 @@ def test_create_vpc(
643642
)
644643
assert all_vpc_ips[0].dict == vpc_ip.dict
645644

645+
# Test getting the ips under this specific VPC
646+
vpc_ips = vpc.ips
647+
648+
assert len(vpc_ips) > 0
649+
assert vpc_ips[0].vpc_id == vpc.id
650+
assert vpc_ips[0].linode_id == linode.id
651+
assert vpc_ips[0].nat_1_1 == linode.ips.ipv4.public[0].address
652+
646653
def test_update_vpc(
647654
self,
648655
linode_for_network_interface_tests,

test/unit/objects/vpc_test.py

+25
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,28 @@ def validate_vpc_subnet_789(self, subnet: VPCSubnet):
173173
self.assertEqual(subnet.linodes[0].id, 12345)
174174
self.assertEqual(subnet.created, expected_dt)
175175
self.assertEqual(subnet.updated, expected_dt)
176+
177+
def test_list_vpc_ips(self):
178+
"""
179+
Test that the ips under a specific VPC can be listed.
180+
"""
181+
vpc = VPC(self.client, 123456)
182+
vpc_ips = vpc.ips
183+
184+
self.assertGreater(len(vpc_ips), 0)
185+
186+
vpc_ip = vpc_ips[0]
187+
188+
self.assertEqual(vpc_ip.vpc_id, vpc.id)
189+
self.assertEqual(vpc_ip.address, "10.0.0.2")
190+
self.assertEqual(vpc_ip.address_range, None)
191+
self.assertEqual(vpc_ip.subnet_id, 654321)
192+
self.assertEqual(vpc_ip.region, "us-ord")
193+
self.assertEqual(vpc_ip.linode_id, 111)
194+
self.assertEqual(vpc_ip.config_id, 222)
195+
self.assertEqual(vpc_ip.interface_id, 333)
196+
self.assertEqual(vpc_ip.active, True)
197+
self.assertEqual(vpc_ip.nat_1_1, None)
198+
self.assertEqual(vpc_ip.gateway, "10.0.0.1")
199+
self.assertEqual(vpc_ip.prefix, 8)
200+
self.assertEqual(vpc_ip.subnet_mask, "255.0.0.0")

0 commit comments

Comments
 (0)