Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions localstack/services/ec2/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from moto.ec2.exceptions import InvalidVpcEndPointIdError
from moto.ec2.models import SubnetBackend, TransitGatewayAttachmentBackend
from moto.ec2.models.launch_templates import LaunchTemplate as MotoLaunchTemplate
from moto.ec2.models.route_tables import RouteTable
from moto.ec2.models.subnets import Subnet

from localstack.aws.api import RequestContext, handler
Expand Down Expand Up @@ -405,6 +406,18 @@ def get_filter_value(fn, self, filter_name):
return fn(self, filter_name)


# TODO: can be removed once https://github.com/getmoto/moto/pull/5932 is merged
@patch(RouteTable.get_filter_value)
def route_table_get_filter_value(fn, self, filter_name):
if filter_name == "route.destination-cidr-block":
return [
route.destination_cidr_block
for route in self.routes.values()
if route.destination_cidr_block is not None
]
return fn(self, filter_name)


@patch(TransitGatewayAttachmentBackend.delete_transit_gateway_vpc_attachment)
def delete_transit_gateway_vpc_attachment(fn, self, transit_gateway_attachment_id):
transit_gateway_attachment = self.transit_gateway_attachments.get(transit_gateway_attachment_id)
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,46 @@ def test_pickle_ec2_backend(ec2_client, pickle_backends):
_ = ec2_client.describe_account_attributes()
pickle_backends(ec2_backends)
assert pickle_backends(ec2_backends)


@pytest.mark.aws_validated
def test_route_table_filter(ec2_client, cleanups):
"""
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html

Tests two filters:
- "route.destination-cidr-block"
- "route-table-id"

"""
vpc = ec2_client.create_vpc(CidrBlock="10.0.99.0/24")
vpc_id = vpc["Vpc"]["VpcId"]
cleanups.append(lambda: ec2_client.delete_vpc(VpcId=vpc_id))

gateway_id = ec2_client.create_internet_gateway()["InternetGateway"]["InternetGatewayId"]
cleanups.append(lambda: ec2_client.delete_internet_gateway(InternetGatewayId=gateway_id))

ec2_client.attach_internet_gateway(InternetGatewayId=gateway_id, VpcId=vpc_id)
cleanups.append(
lambda: ec2_client.detach_internet_gateway(InternetGatewayId=gateway_id, VpcId=vpc_id)
)

create_route_table_response = ec2_client.create_route_table(VpcId=vpc_id)
route_table_id = create_route_table_response["RouteTable"]["RouteTableId"]
cleanups.append(lambda: ec2_client.delete_route_table(RouteTableId=route_table_id))

ec2_client.create_route(
RouteTableId=route_table_id, DestinationCidrBlock="0.0.0.0/0", GatewayId=gateway_id
)

result = ec2_client.describe_route_tables(
Filters=[
{"Name": "route.destination-cidr-block", "Values": ["0.0.0.0/0"]},
{"Name": "route-table-id", "Values": [route_table_id]},
]
)
tables = result["RouteTables"]
assert len(tables) == 1
table = tables[0]
assert table["VpcId"] == vpc_id
assert table["RouteTableId"] == route_table_id