From ab756c82b80d346c87fb90ef5d0fb452e73e575d Mon Sep 17 00:00:00 2001 From: Brian Pandola Date: Sun, 22 May 2022 02:43:51 -0700 Subject: [PATCH] Add `route.gateway-id` filter for ec2:DescribeRouteTables (#5155) --- moto/ec2/models/route_tables.py | 6 ++++++ tests/test_ec2/test_route_tables.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/moto/ec2/models/route_tables.py b/moto/ec2/models/route_tables.py index f7ed90c9a..be72db04d 100644 --- a/moto/ec2/models/route_tables.py +++ b/moto/ec2/models/route_tables.py @@ -76,6 +76,12 @@ class RouteTable(TaggedEC2Resource, CloudFormationModel): return self.associations.keys() elif filter_name == "association.subnet-id": return self.associations.values() + elif filter_name == "route.gateway-id": + return [ + route.gateway.id + for route in self.routes.values() + if route.gateway is not None + ] else: return super().get_filter_value(filter_name, "DescribeRouteTables") diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py index f1be9d171..c772fb2b8 100644 --- a/tests/test_ec2/test_route_tables.py +++ b/tests/test_ec2/test_route_tables.py @@ -94,6 +94,8 @@ def test_route_tables_filters_standard(): vpc2 = ec2.create_vpc(CidrBlock="10.0.0.0/16") route_table2 = ec2.create_route_table(VpcId=vpc2.id) + igw = ec2.create_internet_gateway() + route_table2.create_route(DestinationCidrBlock="10.0.0.4/24", GatewayId=igw.id) all_route_tables = client.describe_route_tables()["RouteTables"] all_ids = [rt["RouteTableId"] for rt in all_route_tables] @@ -135,6 +137,16 @@ def test_route_tables_filters_standard(): vpc2_main_route_table_ids.should_not.contain(route_table1.id) vpc2_main_route_table_ids.should_not.contain(route_table2.id) + # Filter by route gateway id + resp = client.describe_route_tables( + Filters=[ + {"Name": "route.gateway-id", "Values": [igw.id]}, + ] + )["RouteTables"] + assert any( + [route["GatewayId"] == igw.id for table in resp for route in table["Routes"]] + ) + # Unsupported filter if not settings.TEST_SERVER_MODE: # ServerMode will just throw a generic 500