Skip to content

Commit

Permalink
ResourceGroupsTagging: Add support for elasticfilesystem resource t…
Browse files Browse the repository at this point in the history
…ype filter (#7813)
  • Loading branch information
bpandola authored Jul 2, 2024
1 parent 5cc0727 commit 39d8419
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
29 changes: 29 additions & 0 deletions moto/resourcegroupstaggingapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from moto.dynamodb.models import DynamoDBBackend, dynamodb_backends
from moto.ec2 import ec2_backends
from moto.ecs.models import EC2ContainerServiceBackend, ecs_backends
from moto.efs.models import EFSBackend, efs_backends
from moto.elb.models import ELBBackend, elb_backends
from moto.elbv2.models import ELBv2Backend, elbv2_backends
from moto.emr.models import ElasticMapReduceBackend, emr_backends
Expand Down Expand Up @@ -51,6 +52,10 @@ def s3_backend(self) -> S3Backend:
def ec2_backend(self) -> Any: # type: ignore[misc]
return ec2_backends[self.account_id][self.region_name]

@property
def efs_backend(self) -> EFSBackend:
return efs_backends[self.account_id][self.region_name]

@property
def elb_backend(self) -> ELBBackend:
return elb_backends[self.account_id][self.region_name]
Expand Down Expand Up @@ -372,6 +377,30 @@ def format_tag_keys(
"Tags": tags,
}

# EFS, resource type elasticfilesystem:access-point
if (
not resource_type_filters
or "elasticfilesystem" in resource_type_filters
or "elasticfilesystem:access-point" in resource_type_filters
):
for ap in self.efs_backend.access_points.values():
tags = self.efs_backend.list_tags_for_resource(ap.access_point_id)
if not tag_filter(tags):
continue
yield {"ResourceARN": f"{ap.access_point_arn}", "Tags": tags}

# EFS, resource type elasticfilesystem:file-system
if (
not resource_type_filters
or "elasticfilesystem" in resource_type_filters
or "elasticfilesystem:file-system" in resource_type_filters
):
for fs in self.efs_backend.file_systems_by_id.values():
tags = self.efs_backend.list_tags_for_resource(fs.file_system_id)
if not tag_filter(tags):
continue
yield {"ResourceARN": f"{fs.file_system_arn}", "Tags": tags}

# ELB (Classic Load Balancers)
if (
not resource_type_filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,61 @@ def test_get_resources_sagemaker_cluster():
assert {"Key": "sagemakerkey", "Value": "sagemakervalue"} in resp[
"ResourceTagMappingList"
][0]["Tags"]


@mock_aws
def test_get_resources_efs():
client = boto3.client("resourcegroupstaggingapi", region_name="us-east-1")
efs = boto3.client("efs", region_name="us-east-1")
# elasticfilesystem:file-system
fs_one = efs.create_file_system(
CreationToken="test-token-1", Tags=[{"Key": "tag", "Value": "a tag"}]
)
fs_two = efs.create_file_system(
CreationToken="test-token-2", Tags=[{"Key": "tag", "Value": "b tag"}]
)
resp = client.get_resources(ResourceTypeFilters=["elasticfilesystem:file-system"])
assert len(resp["ResourceTagMappingList"]) == 2
resp = client.get_resources(
ResourceTypeFilters=["elasticfilesystem:file-system"],
TagFilters=[{"Key": "tag", "Values": ["a tag"]}],
)
assert len(resp["ResourceTagMappingList"]) == 1
returned_arns = [i["ResourceARN"] for i in resp["ResourceTagMappingList"]]
assert fs_one["FileSystemArn"] in returned_arns
assert fs_two["FileSystemArn"] not in returned_arns

# elasticfilesystem:access-point
ap_one = efs.create_access_point(
ClientToken="ct-1",
FileSystemId=fs_one["FileSystemId"],
Tags=[{"Key": "tag", "Value": "a tag"}],
)
ap_two = efs.create_access_point(
ClientToken="ct-2",
FileSystemId=fs_two["FileSystemId"],
Tags=[{"Key": "tag", "Value": "b tag"}],
)
resp = client.get_resources(ResourceTypeFilters=["elasticfilesystem:access-point"])
assert len(resp["ResourceTagMappingList"]) == 2
resp = client.get_resources(
ResourceTypeFilters=["elasticfilesystem:access-point"],
TagFilters=[{"Key": "tag", "Values": ["a tag"]}],
)
assert len(resp["ResourceTagMappingList"]) == 1
returned_arns = [i["ResourceARN"] for i in resp["ResourceTagMappingList"]]
assert ap_one["AccessPointArn"] in returned_arns
assert ap_two["AccessPointArn"] not in returned_arns

resp = client.get_resources(ResourceTypeFilters=["elasticfilesystem"])
assert len(resp["ResourceTagMappingList"]) == 4
resp = client.get_resources(
ResourceTypeFilters=["elasticfilesystem"],
TagFilters=[{"Key": "tag", "Values": ["b tag"]}],
)
assert len(resp["ResourceTagMappingList"]) == 2
returned_arns = [i["ResourceARN"] for i in resp["ResourceTagMappingList"]]
assert fs_one["FileSystemArn"] not in returned_arns
assert fs_two["FileSystemArn"] in returned_arns
assert ap_one["AccessPointArn"] not in returned_arns
assert ap_two["AccessPointArn"] in returned_arns

0 comments on commit 39d8419

Please # to comment.