Skip to content

Commit

Permalink
new: List ips under a specific VPC (#391)
Browse files Browse the repository at this point in the history
* list ips under a vpc

* fix
  • Loading branch information
yec-akamai authored Apr 8, 2024
1 parent 0951c34 commit c8f4bd5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
21 changes: 21 additions & 0 deletions linode_api4/objects/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from linode_api4.errors import UnexpectedResponseError
from linode_api4.objects import Base, DerivedBase, Property, Region
from linode_api4.objects.serializable import JSONObject
from linode_api4.paginated_list import PaginatedList


@dataclass
Expand Down Expand Up @@ -97,3 +98,23 @@ def subnet_create(

d = VPCSubnet(self._client, result["id"], self.id, result)
return d

@property
def ips(self, *filters) -> PaginatedList:
"""
Get all the IP addresses under this VPC.
API Documentation: TODO
:returns: A list of VPCIPAddresses the acting user can access.
:rtype: PaginatedList of VPCIPAddress
"""

# need to avoid circular import
from linode_api4.objects import ( # pylint: disable=import-outside-toplevel
VPCIPAddress,
)

return self._client._get_and_filter(
VPCIPAddress, *filters, endpoint="/vpcs/{}/ips".format(self.id)
)
34 changes: 34 additions & 0 deletions test/fixtures/vpcs_123456_ips.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": [
{
"address": "10.0.0.2",
"address_range": null,
"vpc_id": 123456,
"subnet_id": 654321,
"region": "us-ord",
"linode_id": 111,
"config_id": 222,
"interface_id": 333,
"active": true,
"nat_1_1": null,
"gateway": "10.0.0.1",
"prefix": 8,
"subnet_mask": "255.0.0.0"
},
{
"address": "10.0.0.3",
"address_range": null,
"vpc_id": 41220,
"subnet_id": 41184,
"region": "us-ord",
"linode_id": 56323949,
"config_id": 59467106,
"interface_id": 1248358,
"active": true,
"nat_1_1": null,
"gateway": "10.0.0.1",
"prefix": 8,
"subnet_mask": "255.0.0.0"
}
]
}
16 changes: 0 additions & 16 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,6 @@ def create_vpc_with_subnet_and_linode(
instance.delete()


@pytest.fixture(scope="session")
def create_vpc(test_linode_client):
client = test_linode_client

timestamp = str(int(time.time_ns() % 10**10))

vpc = client.vpcs.create(
"pythonsdk-" + timestamp,
get_region(test_linode_client, {"VPCs"}),
description="test description",
)
yield vpc

vpc.delete()


@pytest.fixture(scope="session")
def create_multiple_vpcs(test_linode_client):
client = test_linode_client
Expand Down
9 changes: 8 additions & 1 deletion test/integration/models/test_linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
ConfigInterface,
ConfigInterfaceIPv4,
Disk,
Image,
Instance,
Type,
)
Expand Down Expand Up @@ -643,6 +642,14 @@ def test_create_vpc(
)
assert all_vpc_ips[0].dict == vpc_ip.dict

# Test getting the ips under this specific VPC
vpc_ips = vpc.ips

assert len(vpc_ips) > 0
assert vpc_ips[0].vpc_id == vpc.id
assert vpc_ips[0].linode_id == linode.id
assert vpc_ips[0].nat_1_1 == linode.ips.ipv4.public[0].address

def test_update_vpc(
self,
linode_for_network_interface_tests,
Expand Down
25 changes: 25 additions & 0 deletions test/unit/objects/vpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,28 @@ def validate_vpc_subnet_789(self, subnet: VPCSubnet):
self.assertEqual(subnet.linodes[0].id, 12345)
self.assertEqual(subnet.created, expected_dt)
self.assertEqual(subnet.updated, expected_dt)

def test_list_vpc_ips(self):
"""
Test that the ips under a specific VPC can be listed.
"""
vpc = VPC(self.client, 123456)
vpc_ips = vpc.ips

self.assertGreater(len(vpc_ips), 0)

vpc_ip = vpc_ips[0]

self.assertEqual(vpc_ip.vpc_id, vpc.id)
self.assertEqual(vpc_ip.address, "10.0.0.2")
self.assertEqual(vpc_ip.address_range, None)
self.assertEqual(vpc_ip.subnet_id, 654321)
self.assertEqual(vpc_ip.region, "us-ord")
self.assertEqual(vpc_ip.linode_id, 111)
self.assertEqual(vpc_ip.config_id, 222)
self.assertEqual(vpc_ip.interface_id, 333)
self.assertEqual(vpc_ip.active, True)
self.assertEqual(vpc_ip.nat_1_1, None)
self.assertEqual(vpc_ip.gateway, "10.0.0.1")
self.assertEqual(vpc_ip.prefix, 8)
self.assertEqual(vpc_ip.subnet_mask, "255.0.0.0")

0 comments on commit c8f4bd5

Please sign in to comment.