Add ACM support for ResourceGroupsTaggingAPI (#7010)

This commit is contained in:
afuentes-19 2023-11-28 17:29:57 -05:00 committed by GitHub
parent d6377ff905
commit ecf3de7851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# autogenerated by /home/bblommers/Software/Code/bblommers/moto/scripts/update_backend_index.py
# autogenerated by moto/scripts/update_backend_index.py
import re
backend_url_patterns = [

View File

@ -18,6 +18,7 @@ from moto.redshift.models import redshift_backends, RedshiftBackend
from moto.emr.models import emr_backends, ElasticMapReduceBackend
from moto.awslambda.models import lambda_backends, LambdaBackend
from moto.ecs.models import ecs_backends, EC2ContainerServiceBackend
from moto.acm.models import acm_backends, AWSCertificateManagerBackend
# Left: EC2 ElastiCache RDS ELB CloudFront WorkSpaces Lambda EMR Glacier Kinesis Redshift Route53
# StorageGateway DynamoDB MachineLearning ACM DirectConnect DirectoryService CloudHSM
@ -90,6 +91,10 @@ class ResourceGroupsTaggingAPIBackend(BaseBackend):
def ecs_backend(self) -> EC2ContainerServiceBackend:
return ecs_backends[self.account_id][self.region_name]
@property
def acm_backend(self) -> AWSCertificateManagerBackend:
return acm_backends[self.account_id][self.region_name]
def _get_resources_generator(
self,
tag_filters: Optional[List[Dict[str, Any]]] = None,
@ -118,7 +123,9 @@ class ResourceGroupsTaggingAPIBackend(BaseBackend):
and v in vl
)
def tag_filter(tag_list: List[Dict[str, str]]) -> bool:
# Any in this case means: str | Optional[str]
# When we drop Python 3.7 we should look into replacing this with a TypedDict
def tag_filter(tag_list: List[Dict[str, Any]]) -> bool:
result = []
if tag_filters:
for f in filters:
@ -131,20 +138,28 @@ class ResourceGroupsTaggingAPIBackend(BaseBackend):
else:
return True
def format_tags(tags: Dict[str, str]) -> List[Dict[str, str]]:
def format_tags(tags: Dict[str, Any]) -> List[Dict[str, Any]]:
result = []
for key, value in tags.items():
result.append({"Key": key, "Value": value})
return result
def format_tag_keys(
tags: List[Dict[str, str]], keys: List[str]
) -> List[Dict[str, str]]:
tags: List[Dict[str, Any]], keys: List[str]
) -> List[Dict[str, Any]]:
result = []
for tag in tags:
result.append({"Key": tag[keys[0]], "Value": tag[keys[1]]})
return result
# ACM
if not resource_type_filters or "acm" in resource_type_filters:
for certificate in self.acm_backend._certificates.values():
tags = format_tags(certificate.tags)
if not tags or not tag_filter(tags):
continue
yield {"ResourceARN": f"{certificate.arn}", "Tags": tags}
# S3
if not resource_type_filters or "s3" in resource_type_filters:
for bucket in self.s3_backend.buckets.values():

View File

@ -3,6 +3,7 @@ import json
import boto3
from botocore.client import ClientError
from moto import mock_acm
from moto import mock_ec2
from moto import mock_elbv2
from moto import mock_kms
@ -13,6 +14,7 @@ from moto import mock_iam
from moto import mock_cloudformation
from moto import mock_ecs
from tests import EXAMPLE_AMI_ID, EXAMPLE_AMI_ID2
import logging
@mock_kms
@ -75,6 +77,45 @@ def test_get_resources_cloudformation():
assert stack_two in resp["ResourceTagMappingList"][0]["ResourceARN"]
@mock_acm
@mock_resourcegroupstaggingapi
def test_get_resources_acm():
client = boto3.client("acm", region_name="us-east-1")
cert_blue = client.request_certificate(
DomainName="helloworldone.com",
ValidationMethod="DNS",
Tags=[
{"Key": "TagKey1", "Value": "TagValue1"},
{"Key": "TagKey2", "Value": "TagValue2"},
{"Key": "Color", "Value": "Blue"},
],
)
client.request_certificate(
DomainName="helloworldtwo.com",
ValidationMethod="DNS",
Tags=[
{"Key": "TagKey1", "Value": "TagValue1"},
{"Key": "TagKey2", "Value": ""},
{"Key": "Color", "Value": "Green"},
],
)
rgta_client = boto3.client("resourcegroupstaggingapi", region_name="us-east-1")
resources_no_filter = rgta_client.get_resources(
ResourceTypeFilters=["acm"],
)
assert len(resources_no_filter["ResourceTagMappingList"]) == 2
logging.info(f"RESOURCES NO FILTER: {resources_no_filter}\n")
resources_blue_filter = rgta_client.get_resources(
TagFilters=[{"Key": "Color", "Values": ["Blue"]}]
)
logging.info(f"RESOURCES BLUE FILTER: {resources_blue_filter}")
assert len(resources_blue_filter["ResourceTagMappingList"]) == 1
assert (
cert_blue["CertificateArn"]
== resources_blue_filter["ResourceTagMappingList"][0]["ResourceARN"]
)
@mock_ecs
@mock_ec2
@mock_resourcegroupstaggingapi

View File

@ -0,0 +1,5 @@
"""Unit tests for sns-supported APIs."""
# See our Development Tips on writing tests for hints on how to write good tests:
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html