From c0152f206fb868a97e0334e518c9d2e879dd16de Mon Sep 17 00:00:00 2001 From: vgiridhar97 <11745812+vgiridhar97@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:04:52 -0400 Subject: [PATCH] added resourcegroupstaggingapi support for ssm (#7430) --- moto/resourcegroupstaggingapi/models.py | 21 ++++++++++++++ .../test_resourcegroupstaggingapi.py | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/moto/resourcegroupstaggingapi/models.py b/moto/resourcegroupstaggingapi/models.py index 6a39730fd..db3a0eb3c 100644 --- a/moto/resourcegroupstaggingapi/models.py +++ b/moto/resourcegroupstaggingapi/models.py @@ -22,6 +22,7 @@ from moto.redshift.models import RedshiftBackend, redshift_backends from moto.s3.models import S3Backend, s3_backends from moto.sns.models import SNSBackend, sns_backends from moto.sqs.models import SQSBackend, sqs_backends +from moto.ssm.models import SimpleSystemManagerBackend, ssm_backends from moto.utilities.tagging_service import TaggingService # Left: EC2 ElastiCache RDS ELB CloudFront WorkSpaces Lambda EMR Glacier Kinesis Redshift Route53 @@ -102,6 +103,10 @@ class ResourceGroupsTaggingAPIBackend(BaseBackend): @property def sns_backend(self) -> SNSBackend: return sns_backends[self.account_id][self.region_name] + + @property + def ssm_backend(self) -> SimpleSystemManagerBackend: + return ssm_backends[self.account_id][self.region_name] @property def sqs_backend(self) -> SQSBackend: @@ -512,6 +517,22 @@ class ResourceGroupsTaggingAPIBackend(BaseBackend): continue yield {"ResourceARN": f"{topic.arn}", "Tags": tags} + + # SSM + if not resource_type_filters or "ssm" in resource_type_filters: + for document in self.ssm_backend._documents.values(): + doc_name = document.describe()["Name"] + tags = self.ssm_backend._get_documents_tags(doc_name) + if not tags or not tag_filter( + tags + ): # Skip if no tags, or invalid filter + continue + yield { + "ResourceARN": f"arn:aws:ssm:{self.region_name}:{self.account_id}:document/{doc_name}", + "Tags": tags, + } + + # VPC if ( not resource_type_filters diff --git a/tests/test_resourcegroupstaggingapi/test_resourcegroupstaggingapi.py b/tests/test_resourcegroupstaggingapi/test_resourcegroupstaggingapi.py index 4034621d9..5b410c563 100644 --- a/tests/test_resourcegroupstaggingapi/test_resourcegroupstaggingapi.py +++ b/tests/test_resourcegroupstaggingapi/test_resourcegroupstaggingapi.py @@ -759,6 +759,34 @@ def test_get_resources_sns(): "Tags" ] +@mock_aws +def test_get_resources_ssm(): + import json + + import yaml + + from tests.test_ssm.test_ssm_docs import _get_yaml_template + + template_file = _get_yaml_template() + json_doc = yaml.safe_load(template_file) + + ssm = boto3.client("ssm", region_name="us-east-1") + ssm.create_document( + Content=json.dumps(json_doc), + Name="TestDocument", + DocumentType="Command", + DocumentFormat="JSON", + Tags=[{"Key": 'testing', "Value": "testingValue"}], + ) + + rtapi = boto3.client("resourcegroupstaggingapi", region_name="us-east-1") + resp = rtapi.get_resources(ResourceTypeFilters=["ssm"]) + + assert len(resp["ResourceTagMappingList"]) == 1 + assert {"Key": 'testing', "Value": "testingValue"} in resp["ResourceTagMappingList"][0][ + "Tags" + ] + @mock_aws def test_tag_resources_for_unknown_service():