diff --git a/moto/ec2/models/transit_gateway.py b/moto/ec2/models/transit_gateway.py index 2805a0367..eef85c7a5 100644 --- a/moto/ec2/models/transit_gateway.py +++ b/moto/ec2/models/transit_gateway.py @@ -4,7 +4,10 @@ from moto.core import CloudFormationModel from moto.core.utils import iso_8601_datetime_with_milliseconds from moto.utilities.utils import filter_resources, merge_multiple_dicts from .core import TaggedEC2Resource -from ..utils import random_transit_gateway_id +from ..utils import ( + random_transit_gateway_id, + describe_tag_filter, +) class TransitGateway(TaggedEC2Resource, CloudFormationModel): @@ -123,6 +126,8 @@ class TransitGatewayBackend: result = transit_gateways if filters: result = filter_resources(transit_gateways, filters, attr_pairs) + result = describe_tag_filter(filters, result) + return result def delete_transit_gateway(self, transit_gateway_id: str) -> TransitGateway: diff --git a/tests/test_ec2/test_transit_gateway.py b/tests/test_ec2/test_transit_gateway.py index d65551e7a..7d1094a18 100644 --- a/tests/test_ec2/test_transit_gateway.py +++ b/tests/test_ec2/test_transit_gateway.py @@ -111,9 +111,54 @@ def test_describe_transit_gateway_by_id(): g2_id = g2["TransitGatewayId"] ec2.create_transit_gateway(Description="my third gatway")["TransitGateway"] - my_gateway = ec2.describe_transit_gateways(TransitGatewayIds=[g2_id])[ + gateways = ec2.describe_transit_gateways(TransitGatewayIds=[g2_id])[ "TransitGateways" - ][0] + ] + assert len(gateways) == 1 + + my_gateway = gateways[0] + assert my_gateway["TransitGatewayId"] == g2_id + assert my_gateway["Description"] == "my second gatway" + + +@mock_ec2 +def test_describe_transit_gateway_by_tags(): + ec2 = boto3.client("ec2", region_name="us-west-1") + ec2.create_transit_gateway( + Description="my first gatway", + TagSpecifications=[ + { + "ResourceType": "transit-gateway-route-table", + "Tags": [ + {"Key": "tag1", "Value": "val1"}, + {"Key": "tag2", "Value": "val2"}, + ], + } + ], + )["TransitGateway"] + g2 = ec2.create_transit_gateway(Description="my second gatway")["TransitGateway"] + g2 = ec2.create_transit_gateway( + Description="my second gatway", + TagSpecifications=[ + { + "ResourceType": "transit-gateway-route-table", + "Tags": [ + {"Key": "the-tag", "Value": "the-value"}, + {"Key": "tag2", "Value": "val2"}, + ], + } + ], + )["TransitGateway"] + g2_id = g2["TransitGatewayId"] + ec2.create_transit_gateway(Description="my third gatway")["TransitGateway"] + + gateways = ec2.describe_transit_gateways( + Filters=[{"Name": "tag:the-tag", "Values": ["the-value"]}] + )["TransitGateways"] + + assert len(gateways) == 1 + + my_gateway = gateways[0] assert my_gateway["TransitGatewayId"] == g2_id assert my_gateway["Description"] == "my second gatway"