import unittest

import boto3

from moto import mock_aws


@mock_aws
class TestLogsTagging(unittest.TestCase):
    def setUp(self) -> None:
        self.logs = boto3.client("logs", region_name="us-east-2")
        self.rtapi = boto3.client("resourcegroupstaggingapi", region_name="us-east-2")
        self.resources_tagged = []
        self.resources_untagged = []
        for i in range(3):
            self.logs.create_log_group(logGroupName=f"test{i}", tags={"key1": "val1"})
        self.arns = [
            lg["logGroupArn"] for lg in self.logs.describe_log_groups()["logGroups"]
        ]

    def test_get_resources_logs(self):
        resp = self.rtapi.get_resources(ResourceTypeFilters=["logs"])
        assert len(resp["ResourceTagMappingList"]) == 3

        resp = self.rtapi.get_resources(ResourceTypeFilters=["logs:loggroup"])
        assert len(resp["ResourceTagMappingList"]) == 3

    def test_tag_resources_logs(self):
        # WHEN
        # we tag resources
        self.rtapi.tag_resources(
            ResourceARNList=self.arns,
            Tags={"key2": "val2"},
        )

        # THEN
        # we can retrieve the tags using the RDS API
        def get_tags(arn):
            return self.logs.list_tags_for_resource(resourceArn=arn)["tags"]

        for arn in self.arns:
            assert get_tags(arn) == {"key1": "val1", "key2": "val2"}
