Skip to content

Commit

Permalink
EC2: Enabled regex matching in filters (#7789)
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjan1002 authored Jun 29, 2024
1 parent bfc7616 commit c701978
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion moto/ec2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ def filter_internet_gateways(

def is_filter_matching(obj: Any, _filter: str, filter_value: Any) -> bool:
value = obj.get_filter_value(_filter)

if filter_value is None:
return False

if isinstance(value, str):
if not isinstance(filter_value, list):
filter_value = [filter_value]

if any(fnmatch.fnmatch(value, pattern) for pattern in filter_value):
return True
return False
Expand All @@ -534,6 +534,9 @@ def is_filter_matching(obj: Any, _filter: str, filter_value: Any) -> bool:

try:
value = set(value)
if isinstance(filter_value, list) and len(filter_value) == 1:
return any(fnmatch.fnmatch(element, filter_value[0]) for element in value)

return (value and value.issubset(filter_value)) or value.issuperset(
filter_value
)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_ec2/test_route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ def test_route_tables_filters_transit_gateway():

gateway = response["TransitGateway"]
main_route_table.create_route(TransitGatewayId=gateway["TransitGatewayId"])

route_tables = client.describe_route_tables(
Filters=[
{
Expand All @@ -297,6 +296,20 @@ def test_route_tables_filters_transit_gateway():
route_table = route_tables[0]
assert route_table["RouteTableId"] == main_route_table_id

# Filter using regex
route_tables = client.describe_route_tables(
Filters=[
{
"Name": "route.transit-gateway-id",
"Values": ["tgw-*"],
}
]
)["RouteTables"]

assert len(route_tables) == 1
route_table = route_tables[0]
assert route_table["RouteTableId"] == main_route_table_id


@mock_aws
def test_route_table_associations():
Expand Down

0 comments on commit c701978

Please sign in to comment.