diff --git a/moto/ec2/models.py b/moto/ec2/models.py index afb23dc80..af69fdabc 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -3302,6 +3302,7 @@ class Route(object): local=False, gateway=None, instance=None, + nat_gateway=None, interface=None, vpc_pcx=None, ): @@ -3311,6 +3312,7 @@ class Route(object): self.local = local self.gateway = gateway self.instance = instance + self.nat_gateway = nat_gateway self.interface = interface self.vpc_pcx = vpc_pcx @@ -3323,6 +3325,7 @@ class Route(object): gateway_id = properties.get("GatewayId") instance_id = properties.get("InstanceId") interface_id = properties.get("NetworkInterfaceId") + nat_gateway_id = properties.get("NatGatewayId") pcx_id = properties.get("VpcPeeringConnectionId") route_table_id = properties["RouteTableId"] @@ -3332,6 +3335,7 @@ class Route(object): destination_cidr_block=properties.get("DestinationCidrBlock"), gateway_id=gateway_id, instance_id=instance_id, + nat_gateway_id=nat_gateway_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id, ) @@ -3349,6 +3353,7 @@ class RouteBackend(object): local=False, gateway_id=None, instance_id=None, + nat_gateway_id=None, interface_id=None, vpc_peering_connection_id=None, ): @@ -3369,12 +3374,17 @@ class RouteBackend(object): except ValueError: raise InvalidDestinationCIDRBlockParameterError(destination_cidr_block) + nat_gateway = None + if nat_gateway_id is not None: + nat_gateway = self.nat_gateways.get(nat_gateway_id) + route = Route( route_table, destination_cidr_block, local=local, gateway=gateway, instance=self.get_instance(instance_id) if instance_id else None, + nat_gateway=nat_gateway, interface=None, vpc_pcx=self.get_vpc_peering_connection(vpc_peering_connection_id) if vpc_peering_connection_id diff --git a/moto/ec2/responses/route_tables.py b/moto/ec2/responses/route_tables.py index ef796e401..b5d65f831 100644 --- a/moto/ec2/responses/route_tables.py +++ b/moto/ec2/responses/route_tables.py @@ -18,6 +18,7 @@ class RouteTables(BaseResponse): destination_cidr_block = self._get_param("DestinationCidrBlock") gateway_id = self._get_param("GatewayId") instance_id = self._get_param("InstanceId") + nat_gateway_id = self._get_param("NatGatewayId") interface_id = self._get_param("NetworkInterfaceId") pcx_id = self._get_param("VpcPeeringConnectionId") @@ -26,6 +27,7 @@ class RouteTables(BaseResponse): destination_cidr_block, gateway_id=gateway_id, instance_id=instance_id, + nat_gateway_id=nat_gateway_id, interface_id=interface_id, vpc_peering_connection_id=pcx_id, ) @@ -173,6 +175,10 @@ DESCRIBE_ROUTE_TABLES_RESPONSE = """ CreateRoute blackhole {% endif %} + {% if route.nat_gateway %} + {{ route.nat_gateway.id }} + active + {% endif %} {% endfor %} diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py index b82313bc8..dfb3292b6 100644 --- a/tests/test_ec2/test_route_tables.py +++ b/tests/test_ec2/test_route_tables.py @@ -581,3 +581,40 @@ def test_create_route_with_invalid_destination_cidr_block_parameter(): destination_cidr_block ) ) + + +@mock_ec2 +def test_describe_route_tables_with_nat_gateway(): + ec2 = boto3.client("ec2", region_name="us-west-1") + vpc_id = ec2.create_vpc(CidrBlock="192.168.0.0/23")["Vpc"]["VpcId"] + igw_id = ec2.create_internet_gateway()["InternetGateway"]["InternetGatewayId"] + ec2.attach_internet_gateway(VpcId=vpc_id, InternetGatewayId=igw_id) + 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"] + ec2.create_route( + DestinationCidrBlock="0.0.0.0/0", + NatGatewayId=nat_gw_id, + RouteTableId=route_table_id, + ) + + route_table = ec2.describe_route_tables( + Filters=[{"Name": "route-table-id", "Values": [route_table_id]}] + )["RouteTables"][0] + nat_gw_routes = [ + route + for route in route_table["Routes"] + if route["DestinationCidrBlock"] == "0.0.0.0/0" + ] + + nat_gw_routes.should.have.length_of(1) + nat_gw_routes[0]["DestinationCidrBlock"].should.equal("0.0.0.0/0") + nat_gw_routes[0]["NatGatewayId"].should.equal(nat_gw_id) + nat_gw_routes[0]["State"].should.equal("active")