EC2: Support filtering transit gateways by tag (#6774)
This commit is contained in:
parent
bf9bbcc506
commit
c653b597f3
@ -4,7 +4,10 @@ from moto.core import CloudFormationModel
|
|||||||
from moto.core.utils import iso_8601_datetime_with_milliseconds
|
from moto.core.utils import iso_8601_datetime_with_milliseconds
|
||||||
from moto.utilities.utils import filter_resources, merge_multiple_dicts
|
from moto.utilities.utils import filter_resources, merge_multiple_dicts
|
||||||
from .core import TaggedEC2Resource
|
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):
|
class TransitGateway(TaggedEC2Resource, CloudFormationModel):
|
||||||
@ -123,6 +126,8 @@ class TransitGatewayBackend:
|
|||||||
result = transit_gateways
|
result = transit_gateways
|
||||||
if filters:
|
if filters:
|
||||||
result = filter_resources(transit_gateways, filters, attr_pairs)
|
result = filter_resources(transit_gateways, filters, attr_pairs)
|
||||||
|
result = describe_tag_filter(filters, result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def delete_transit_gateway(self, transit_gateway_id: str) -> TransitGateway:
|
def delete_transit_gateway(self, transit_gateway_id: str) -> TransitGateway:
|
||||||
|
@ -111,9 +111,54 @@ def test_describe_transit_gateway_by_id():
|
|||||||
g2_id = g2["TransitGatewayId"]
|
g2_id = g2["TransitGatewayId"]
|
||||||
ec2.create_transit_gateway(Description="my third gatway")["TransitGateway"]
|
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"
|
"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["TransitGatewayId"] == g2_id
|
||||||
assert my_gateway["Description"] == "my second gatway"
|
assert my_gateway["Description"] == "my second gatway"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user