Repository: getmoto/moto
Pull request #7744 (2024-06-03 00:45:36)

EC2: TransitGatewayId filter for DescribeRouteTables
Added filter 'transit gateway id' for the DescribeRouteTables operation

aranjan1002: Linked to issue: https://github.com/getmoto/moto/issues/7735
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/getmoto/moto/pull/7744?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 94.34%. Comparing base [(`cccc418`)](https://app.codecov.io/gh/getmoto/moto/commit/cccc4188d9e0c2c30728b79d9c063bc8a53a3648?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) to head [(`3dd2d1f`)](https://app.codecov.io/gh/getmoto/moto/commit/3dd2d1f2029e45dd6facb451c0144d82904024c6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #7744      +/-   ##
==========================================
- Coverage   94.34%   94.34%   -0.01%     
==========================================
  Files        1092     1092              
  Lines       93135    93137       +2     
==========================================
+ Hits        87865    87866       +1     
- Misses       5270     5271       +1     
```

| [Flag](https://app.codecov.io/gh/getmoto/moto/pull/7744/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | Coverage Δ | |
|---|---|---|
| [servertests](https://app.codecov.io/gh/getmoto/moto/pull/7744/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | `29.55% <0.00%> (-0.01%)` | :arrow_down: |
| [unittests](https://app.codecov.io/gh/getmoto/moto/pull/7744/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto) | `94.31% <100.00%> (-0.01%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/getmoto/moto/pull/7744?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getmoto).

aranjan1002: @bblommers The checks passed now. Could you please see if this can be merged? Thanks.

Patch:
diff --git a/moto/ec2/models/route_tables.py b/moto/ec2/models/route_tables.py
index c557b5a70..0932e2103 100644
--- a/moto/ec2/models/route_tables.py
+++ b/moto/ec2/models/route_tables.py
@@ -122,6 +122,12 @@ class RouteTable(TaggedEC2Resource, CloudFormationModel):
                 for route in self.routes.values()
                 if route.nat_gateway is not None
             ]
+        elif filter_name == "route.transit-gateway-id":
+            return [
+                route.transit_gateway.id
+                for route in self.routes.values()
+                if route.transit_gateway is not None
+            ]
         else:
             return super().get_filter_value(filter_name, "DescribeRouteTables")
 


Test patch:
diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py
index 4187103c4..9db133ef2 100644
--- a/tests/test_ec2/test_route_tables.py
+++ b/tests/test_ec2/test_route_tables.py
@@ -269,6 +269,35 @@ def test_route_tables_filters_vpc_peering_connection():
     all(vpc_pcx_id == vpc_pcx.id for vpc_pcx_id in vpc_pcx_ids)
 
 
+@mock_aws
+def test_route_tables_filters_transit_gateway():
+    client = boto3.client("ec2", region_name="us-east-1")
+    ec2 = boto3.resource("ec2", region_name="us-east-1")
+    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
+    main_route_table_id = client.describe_route_tables(
+        Filters=[{"Name": "vpc-id", "Values": [vpc.id]}]
+    )["RouteTables"][0]["RouteTableId"]
+    main_route_table = ec2.RouteTable(main_route_table_id)
+
+    response = client.create_transit_gateway()
+
+    gateway = response["TransitGateway"]
+    main_route_table.create_route(TransitGatewayId=gateway["TransitGatewayId"])
+
+    route_tables = client.describe_route_tables(
+        Filters=[
+            {
+                "Name": "route.transit-gateway-id",
+                "Values": [gateway["TransitGatewayId"]],
+            }
+        ]
+    )["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():
     client = boto3.client("ec2", region_name="us-east-1")