From c8f4bd59dcf2207f81a291d43c6c7fe1ccd5fbc5 Mon Sep 17 00:00:00 2001 From: Ye Chen <127243817+yec-akamai@users.noreply.github.com> Date: Mon, 8 Apr 2024 11:57:32 -0400 Subject: [PATCH] new: List ips under a specific VPC (#391) * list ips under a vpc * fix --- linode_api4/objects/vpc.py | 21 ++++++++++++++++ test/fixtures/vpcs_123456_ips.json | 34 ++++++++++++++++++++++++++ test/integration/conftest.py | 16 ------------ test/integration/models/test_linode.py | 9 ++++++- test/unit/objects/vpc_test.py | 25 +++++++++++++++++++ 5 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/vpcs_123456_ips.json diff --git a/linode_api4/objects/vpc.py b/linode_api4/objects/vpc.py index 989c542ee..682b7a0ab 100644 --- a/linode_api4/objects/vpc.py +++ b/linode_api4/objects/vpc.py @@ -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 @@ -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) + ) diff --git a/test/fixtures/vpcs_123456_ips.json b/test/fixtures/vpcs_123456_ips.json new file mode 100644 index 000000000..70b4b8a60 --- /dev/null +++ b/test/fixtures/vpcs_123456_ips.json @@ -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" + } + ] +} diff --git a/test/integration/conftest.py b/test/integration/conftest.py index 93cff7867..03295e59c 100644 --- a/test/integration/conftest.py +++ b/test/integration/conftest.py @@ -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 diff --git a/test/integration/models/test_linode.py b/test/integration/models/test_linode.py index 40d1e735f..ce122226a 100644 --- a/test/integration/models/test_linode.py +++ b/test/integration/models/test_linode.py @@ -15,7 +15,6 @@ ConfigInterface, ConfigInterfaceIPv4, Disk, - Image, Instance, Type, ) @@ -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, diff --git a/test/unit/objects/vpc_test.py b/test/unit/objects/vpc_test.py index 830e9fb9f..7e4963d33 100644 --- a/test/unit/objects/vpc_test.py +++ b/test/unit/objects/vpc_test.py @@ -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")