Skip to content

Commit

Permalink
RouteTables: Filter by nat_gateway_id (#7677)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelsusanto committed May 8, 2024
1 parent 18011a3 commit fe6317f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions moto/ec2/models/route_tables.py
Expand Up @@ -116,6 +116,12 @@ def get_filter_value(
for route in self.routes.values()
if route.vpc_pcx is not None
]
elif filter_name == "route.nat-gateway-id":
return [
route.nat_gateway.id
for route in self.routes.values()
if route.nat_gateway is not None
]
else:
return super().get_filter_value(filter_name, "DescribeRouteTables")

Expand Down
41 changes: 41 additions & 0 deletions tests/test_ec2/test_route_tables.py
Expand Up @@ -919,6 +919,47 @@ def test_describe_route_tables_with_nat_gateway():
assert nat_gw_routes[0]["State"] == "active"


@mock_aws
def test_describe_route_tables_filter_with_nat_gateway_id():
ec2 = boto3.client("ec2", region_name="us-west-1")
vpc_id = ec2.create_vpc(CidrBlock="192.168.0.0/23")["Vpc"]["VpcId"]
az = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
sn_id = ec2.create_subnet(
AvailabilityZone=az, CidrBlock="192.168.0.0/24", VpcId=vpc_id
)["Subnet"]["SubnetId"]
route_table_id = ec2.create_route_table(VpcId=vpc_id)["RouteTable"]["RouteTableId"]
ec2.associate_route_table(SubnetId=sn_id, RouteTableId=route_table_id)
alloc_id = ec2.allocate_address(Domain="vpc")["AllocationId"]
nat_gw_id = ec2.create_nat_gateway(SubnetId=sn_id, AllocationId=alloc_id)[
"NatGateway"
]["NatGatewayId"]

route_tables_by_nat_gateway_id = ec2.describe_route_tables(
Filters=[{"Name": "route.nat-gateway-id", "Values": [nat_gw_id]}]
)["RouteTables"]
assert len(route_tables_by_nat_gateway_id) == 0

ec2.create_route(
DestinationCidrBlock="0.0.0.0/0",
NatGatewayId=nat_gw_id,
RouteTableId=route_table_id,
)

route_tables_by_nat_gateway_id = ec2.describe_route_tables(
Filters=[{"Name": "route.nat-gateway-id", "Values": [nat_gw_id]}]
)["RouteTables"][0]
nat_gw_routes = [
route
for route in route_tables_by_nat_gateway_id["Routes"]
if route["DestinationCidrBlock"] == "0.0.0.0/0"
]

assert len(nat_gw_routes) == 1
assert nat_gw_routes[0]["DestinationCidrBlock"] == "0.0.0.0/0"
assert nat_gw_routes[0]["NatGatewayId"] == nat_gw_id
assert nat_gw_routes[0]["State"] == "active"


@mock_aws
def test_create_vpc_end_point():
ec2 = boto3.client("ec2", region_name="us-west-1")
Expand Down

0 comments on commit fe6317f

Please sign in to comment.