Skip to content

Commit 7ea07f3

Browse files
TPT-4596: NAT Service for VPC (#762)
* TPT-4602: Added support for NAT Service #1033 (#738) * Added support for NAT Gateway * Fix lint * Address CoPilot suggestions * Added doc links * Address CoPilot fixes * Address more CoPilot suggestions * TPT-4602: Support NAT service int tests (#743) * Add int tests for networking NAT Gateway endpoints * Add int tests for VPC subnet with NAT Gateway * Remove condition for NAT gateway deletion * Refactor NAT Gateway integration tests * Clean up * Add fixture for Reserved IP created in the NAT Gateway region * Add test for NAT Gateway and Reserved IP in diff regions * Activate test skipped due to VPC-6253 (#749) * Removed VPC IP Changes * Updated NATGatewayType to include region_prices and transfer * Fix lint * Copilot fixes --------- Co-authored-by: Maciej Wilk <mawilk@akamai.com>
1 parent eec49c9 commit 7ea07f3

26 files changed

Lines changed: 1560 additions & 57 deletions

‎linode_api4/groups/networking.py‎

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
IPAddress,
1414
IPv6Pool,
1515
IPv6Range,
16+
NATGateway,
17+
NATGatewayAddress,
1618
NetworkTransferPrice,
1719
Region,
1820
)
1921
from linode_api4.objects.base import _flatten_request_body_recursive
20-
from linode_api4.objects.networking import ReservedIPAddress, ReservedIPType
22+
from linode_api4.objects.networking import (
23+
NATGatewaySettings,
24+
NATGatewayType,
25+
ReservedIPAddress,
26+
ReservedIPType,
27+
)
2128
from linode_api4.paginated_list import PaginatedList
2229
from linode_api4.util import drop_null_keys
2330

@@ -622,3 +629,116 @@ def reserved_ip_types(self, *filters) -> PaginatedList:
622629
return self.client._get_and_filter(
623630
ReservedIPType, *filters, endpoint="/networking/reserved/ips/types"
624631
)
632+
633+
def natgateways(self, *filters):
634+
"""
635+
Retrieves the NAT Gateways your user has access to.
636+
637+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-natgateways
638+
639+
:param filters: Any number of filters to apply to this query.
640+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
641+
for more details on filtering.
642+
643+
:returns: A list of NAT Gateways the acting user can access.
644+
:rtype: PaginatedList of NATGateway
645+
"""
646+
return self.client._get_and_filter(NATGateway, *filters)
647+
648+
def natgateway_create(
649+
self,
650+
region: Union[Region, str],
651+
label: str,
652+
addresses: Optional[
653+
List[Union[NATGatewayAddress, Dict[str, Any]]]
654+
] = None,
655+
default_ports_per_interface: Optional[int] = None,
656+
use_autoscaling: Optional[bool] = None,
657+
vpc_subnet_id: Optional[int] = None,
658+
**kwargs,
659+
) -> NATGateway:
660+
"""
661+
Create a NAT Gateway in the given region.
662+
663+
NOTE: NAT Gateways may not currently be available to all users.
664+
665+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-natgateways
666+
667+
:param region: The region in which to create the NAT Gateway.
668+
:type region: str or Region
669+
:param label: The label of the NAT Gateway.
670+
:type label: str
671+
:param addresses: The reserved IP addresses to assign to this NAT Gateway.
672+
:type addresses: list of NATGatewayAddress or list of dict
673+
:param default_ports_per_interface: The default ports per interface to use.
674+
Defaults to 4096 on the API side.
675+
:type default_ports_per_interface: int
676+
:param use_autoscaling: Whether the NAT Gateway should autoscale its address pool.
677+
:type use_autoscaling: bool
678+
:param vpc_subnet_id: The ID of the VPC subnet this NAT Gateway should attach to.
679+
:type vpc_subnet_id: int
680+
681+
:returns: The new NAT Gateway.
682+
:rtype: NATGateway
683+
"""
684+
params = {
685+
"region": region.id if isinstance(region, Region) else region,
686+
"label": label,
687+
"addresses": addresses,
688+
"default_ports_per_interface": default_ports_per_interface,
689+
"use_autoscaling": use_autoscaling,
690+
"vpc_subnet_id": vpc_subnet_id,
691+
}
692+
params.update(kwargs)
693+
694+
result = self.client.post(
695+
"/networking/natgateways",
696+
data=drop_null_keys(_flatten_request_body_recursive(params)),
697+
)
698+
699+
if "id" not in result:
700+
raise UnexpectedResponseError(
701+
"Unexpected response when creating NAT Gateway!", json=result
702+
)
703+
704+
return NATGateway(self.client, result["id"], result)
705+
706+
def natgateway_types(self, *filters) -> PaginatedList:
707+
"""
708+
Returns a list of NAT Gateway types with pricing information.
709+
710+
NOTE: NAT Gateways may not currently be available to all users.
711+
712+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-natgateway-types
713+
714+
:param filters: Any number of filters to apply to this query.
715+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
716+
for more details on filtering.
717+
718+
:returns: A list of NAT Gateway types.
719+
:rtype: PaginatedList of NATGatewayType
720+
"""
721+
return self.client._get_and_filter(
722+
NATGatewayType, *filters, endpoint="/networking/natgateways/types"
723+
)
724+
725+
def natgateway_settings(self) -> NATGatewaySettings:
726+
"""
727+
Returns the account-wide NAT Gateway settings and limits for the current user.
728+
729+
NOTE: NAT Gateways may not currently be available to all users.
730+
731+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-natgateway-settings
732+
733+
:returns: The NAT Gateway settings for the current user.
734+
:rtype: NATGatewaySettings
735+
"""
736+
result = self.client.get("/networking/natgateways/settings")
737+
738+
if "allowed_ports_per_interface" not in result:
739+
raise UnexpectedResponseError(
740+
"Unexpected response when getting NAT Gateway settings!",
741+
json=result,
742+
)
743+
744+
return NATGatewaySettings.from_json(result)

‎linode_api4/groups/vpc.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,30 @@ def create(
9393

9494
params.update(kwargs)
9595

96-
result = self.client.post(
97-
"/vpcs",
98-
data=drop_null_keys(_flatten_request_body_recursive(params)),
99-
)
96+
data = drop_null_keys(_flatten_request_body_recursive(params))
97+
98+
# Recursively clean each subnet while preserving the ``{"id": null}``
99+
# detach signal on any explicitly-supplied ``natgateway`` object.
100+
if subnets is not None:
101+
cleaned_subnets = []
102+
for subnet in subnets:
103+
flattened = _flatten_request_body_recursive(subnet)
104+
cleaned = drop_null_keys(flattened)
105+
106+
# If the caller explicitly supplied a ``natgateway`` field,
107+
# re-inject its flattened (but not null-stripped) form so
108+
# the detach signal survives.
109+
if (
110+
isinstance(flattened, dict)
111+
and "natgateway" in flattened
112+
and flattened["natgateway"] is not None
113+
):
114+
cleaned["natgateway"] = flattened["natgateway"]
115+
116+
cleaned_subnets.append(cleaned)
117+
data["subnets"] = cleaned_subnets
118+
119+
result = self.client.post("/vpcs", data=data)
100120

101121
if not "id" in result:
102122
raise UnexpectedResponseError(

‎linode_api4/objects/linode_interfaces.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ class LinodeInterfaceVPCIPv4Range(JSONObject):
287287
range: str = ""
288288

289289

290+
@dataclass
291+
class LinodeInterfaceVPCIPv4NATGatewayPortsetPort(JSONObject):
292+
start: int = 0
293+
end: int = 0
294+
295+
296+
@dataclass
297+
class LinodeInterfaceVPCIPv4NATGatewayPortset(JSONObject):
298+
address: str = ""
299+
ports: List[LinodeInterfaceVPCIPv4NATGatewayPortsetPort] = field(
300+
default_factory=list
301+
)
302+
303+
304+
@dataclass
305+
class LinodeInterfaceVPCIPv4NATGateway(JSONObject):
306+
"""
307+
A NAT gateway under the IPv4 configuration of a VPC Linode Interface.
308+
"""
309+
310+
id: int = 0
311+
label: str = ""
312+
type: str = ""
313+
url: str = ""
314+
addresses: List[str] = field(default_factory=list)
315+
portset_assignments: int = 0
316+
portset_capacity: int = 0
317+
portsets: List[LinodeInterfaceVPCIPv4NATGatewayPortset] = field(
318+
default_factory=list
319+
) # NOTE: This field may not be available to all users.
320+
321+
290322
@dataclass
291323
class LinodeInterfaceVPCIPv4(JSONObject):
292324
"""
@@ -297,6 +329,7 @@ class LinodeInterfaceVPCIPv4(JSONObject):
297329

298330
addresses: List[LinodeInterfaceVPCIPv4Address] = field(default_factory=list)
299331
ranges: List[LinodeInterfaceVPCIPv4Range] = field(default_factory=list)
332+
natgateway: Optional[LinodeInterfaceVPCIPv4NATGateway] = None
300333

301334

302335
@dataclass

0 commit comments

Comments
 (0)