Skip to content

Commit fd151d5

Browse files
Support LinodeClient(...).vpcs.ips(...) (#385)
1 parent 95d0b20 commit fd151d5

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

linode_api4/groups/vpc.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from typing import Any, Dict, List, Optional, Union
22

3-
from linode_api4 import VPCSubnet
43
from linode_api4.errors import UnexpectedResponseError
54
from linode_api4.groups import Group
6-
from linode_api4.objects import VPC, Base, Region
5+
from linode_api4.objects import VPC, Region, VPCIPAddress
76
from linode_api4.paginated_list import PaginatedList
87

98

@@ -81,3 +80,25 @@ def create(
8180

8281
d = VPC(self.client, result["id"], result)
8382
return d
83+
84+
def ips(self, *filters) -> PaginatedList:
85+
"""
86+
Retrieves all of the VPC IP addresses for the current account matching the given filters.
87+
88+
This is intended to be called from the :any:`LinodeClient`
89+
class, like this::
90+
91+
vpc_ips = client.vpcs.ips()
92+
93+
API Documentation: TODO
94+
95+
:param filters: Any number of filters to apply to this query.
96+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
97+
for more details on filtering.
98+
99+
:returns: A list of VPCIPAddresses the acting user can access.
100+
:rtype: PaginatedList of VPCIPAddress
101+
"""
102+
return self.client._get_and_filter(
103+
VPCIPAddress, *filters, endpoint="/vpcs/ips"
104+
)

test/fixtures/vpcs_ips.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"data": [
3+
{
4+
"address": "10.0.0.2",
5+
"address_range": null,
6+
"vpc_id": 123,
7+
"subnet_id": 456,
8+
"region": "us-mia",
9+
"linode_id": 123,
10+
"config_id": 456,
11+
"interface_id": 789,
12+
"active": true,
13+
"nat_1_1": "172.233.179.133",
14+
"gateway": "10.0.0.1",
15+
"prefix": 24,
16+
"subnet_mask": "255.255.255.0"
17+
}
18+
],
19+
"page": 1,
20+
"pages": 1,
21+
"results": 1
22+
}

test/integration/models/test_linode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010

11+
from linode_api4 import VPCIPAddress
1112
from linode_api4.errors import ApiError
1213
from linode_api4.objects import (
1314
Config,
@@ -595,6 +596,7 @@ def test_create_vlan(self, linode_for_network_interface_tests):
595596

596597
def test_create_vpc(
597598
self,
599+
test_linode_client,
598600
linode_for_network_interface_tests,
599601
create_vpc_with_subnet_and_linode,
600602
):
@@ -635,6 +637,12 @@ def test_create_vpc(
635637
assert vpc_range_ip.address_range == "10.0.0.5/32"
636638
assert not vpc_range_ip.active
637639

640+
# Attempt to resolve the IP from /vpcs/ips
641+
all_vpc_ips = test_linode_client.vpcs.ips(
642+
VPCIPAddress.filters.linode_id == linode.id
643+
)
644+
assert all_vpc_ips[0].dict == vpc_ip.dict
645+
638646
def test_update_vpc(
639647
self,
640648
linode_for_network_interface_tests,

test/unit/objects/vpc_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ def test_create_subnet(self):
126126

127127
self.validate_vpc_subnet_789(subnet)
128128

129+
def test_list_ips(self):
130+
"""
131+
Validates that all VPC IPs can be listed.
132+
"""
133+
134+
with self.mock_get("/vpcs/ips") as m:
135+
result = self.client.vpcs.ips()
136+
137+
assert m.call_url == "/vpcs/ips"
138+
assert len(result) == 1
139+
140+
ip = result[0]
141+
assert ip.address == "10.0.0.2"
142+
assert ip.address_range == None
143+
assert ip.vpc_id == 123
144+
assert ip.subnet_id == 456
145+
assert ip.region == "us-mia"
146+
assert ip.linode_id == 123
147+
assert ip.config_id == 456
148+
assert ip.interface_id == 789
149+
assert ip.active
150+
assert ip.nat_1_1 == "172.233.179.133"
151+
assert ip.gateway == "10.0.0.1"
152+
assert ip.prefix == 24
153+
assert ip.subnet_mask == "255.255.255.0"
154+
129155
def validate_vpc_123456(self, vpc: VPC):
130156
expected_dt = datetime.datetime.strptime(
131157
"2018-01-01T00:01:01", DATE_FORMAT

0 commit comments

Comments
 (0)