From 3e21ddd606aa45901ee01505b5b135278e58d2bf Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 18 Apr 2023 13:54:08 +0000 Subject: [PATCH] ECS: Fix a bug where a cluster cannot be tagged after creation (#6226) --- moto/ecs/models.py | 2 +- tests/test_ecs/test_ecs_boto3.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 1566cbbe3..b89bd8a7c 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -2011,7 +2011,7 @@ class EC2ContainerServiceBackend(BaseBackend): def tag_resource(self, resource_arn: str, tags: List[Dict[str, str]]) -> None: parsed_arn = self._parse_resource_arn(resource_arn) resource = self._get_resource(resource_arn, parsed_arn) - resource.tags = self._merge_tags(resource.tags, tags) + resource.tags = self._merge_tags(resource.tags or [], tags) def _merge_tags( self, existing_tags: List[Dict[str, str]], new_tags: List[Dict[str, str]] diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index 1447bd20d..d834162f0 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -115,7 +115,16 @@ def test_list_clusters(): def test_create_cluster_with_tags(): client = boto3.client("ecs", region_name="us-east-1") tag_list = [{"key": "tagName", "value": "TagValue"}] - cluster = client.create_cluster(clusterName="c_with_tags", tags=tag_list)["cluster"] + cluster = client.create_cluster(clusterName="c1")["cluster"] + + resp = client.list_tags_for_resource(resourceArn=cluster["clusterArn"]) + assert "tags" not in resp + + client.tag_resource(resourceArn=cluster["clusterArn"], tags=tag_list) + tags = client.list_tags_for_resource(resourceArn=cluster["clusterArn"])["tags"] + tags.should.equal([{"key": "tagName", "value": "TagValue"}]) + + cluster = client.create_cluster(clusterName="c2", tags=tag_list)["cluster"] tags = client.list_tags_for_resource(resourceArn=cluster["clusterArn"])["tags"] tags.should.equal([{"key": "tagName", "value": "TagValue"}])