From 442fcd4e51462d757b9ce58b5ef9ecbc02ac75ea Mon Sep 17 00:00:00 2001 From: Luigi Tagliamonte <10259338+ltagliamonte@users.noreply.github.com> Date: Sun, 21 Feb 2021 02:11:34 -0800 Subject: [PATCH] add tags support to ECS tasks (#3715) * add tags support to ECS tasks * lint * lint * fmt * fmt with same version Co-authored-by: ltagliamonte --- moto/ecs/models.py | 7 ++++++- moto/ecs/responses.py | 3 ++- tests/test_ecs/test_ecs_boto3.py | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 45688fefe..a0b3b9ac8 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -264,6 +264,7 @@ class Task(BaseObject): resource_requirements, overrides={}, started_by="", + tags=[], ): self.cluster_arn = cluster.arn self.task_arn = "arn:aws:ecs:{0}:{1}:task/{2}".format( @@ -276,6 +277,7 @@ class Task(BaseObject): self.overrides = overrides self.containers = [] self.started_by = started_by + self.tags = tags self.stopped_reason = "" self.resource_requirements = resource_requirements @@ -790,7 +792,9 @@ class EC2ContainerServiceBackend(BaseBackend): else: raise TaskDefinitionNotFoundException - def run_task(self, cluster_str, task_definition_str, count, overrides, started_by): + def run_task( + self, cluster_str, task_definition_str, count, overrides, started_by, tags + ): cluster = self._get_cluster(cluster_str) task_definition = self.describe_task_definition(task_definition_str) @@ -830,6 +834,7 @@ class EC2ContainerServiceBackend(BaseBackend): resource_requirements, overrides or {}, started_by or "", + tags or [], ) self.update_container_instance_resources( container_instance, resource_requirements diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 18b998ec3..03b8b2618 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -111,8 +111,9 @@ class EC2ContainerServiceResponse(BaseResponse): task_definition_str = self._get_param("taskDefinition") count = self._get_int_param("count") started_by = self._get_param("startedBy") + tags = self._get_param("tags") tasks = self.ecs_backend.run_task( - cluster_str, task_definition_str, count, overrides, started_by + cluster_str, task_definition_str, count, overrides, started_by, tags ) return json.dumps( {"tasks": [task.response_object for task in tasks], "failures": []} diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index 210b1c247..cc8d30955 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -1457,6 +1457,10 @@ def test_run_task(): taskDefinition="test_ecs_task", count=2, startedBy="moto", + tags=[ + {"key": "tagKey0", "value": "tagValue0"}, + {"key": "tagKey1", "value": "tagValue1"}, + ], ) len(response["tasks"]).should.equal(2) response["tasks"][0]["taskArn"].should.contain( @@ -1476,6 +1480,7 @@ def test_run_task(): response["tasks"][0]["desiredStatus"].should.equal("RUNNING") response["tasks"][0]["startedBy"].should.equal("moto") response["tasks"][0]["stoppedReason"].should.equal("") + response["tasks"][0]["tags"][0].get("value").should.equal("tagValue0") @mock_ec2