add tags support to ECS tasks (#3715)

* add tags support to ECS tasks

* lint

* lint

* fmt

* fmt with same version

Co-authored-by: ltagliamonte <ltagliamonte@users.noreply.github.com>
This commit is contained in:
Luigi Tagliamonte 2021-02-21 02:11:34 -08:00 committed by GitHub
parent c8c6b0bf58
commit 442fcd4e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View File

@ -264,6 +264,7 @@ class Task(BaseObject):
resource_requirements, resource_requirements,
overrides={}, overrides={},
started_by="", started_by="",
tags=[],
): ):
self.cluster_arn = cluster.arn self.cluster_arn = cluster.arn
self.task_arn = "arn:aws:ecs:{0}:{1}:task/{2}".format( self.task_arn = "arn:aws:ecs:{0}:{1}:task/{2}".format(
@ -276,6 +277,7 @@ class Task(BaseObject):
self.overrides = overrides self.overrides = overrides
self.containers = [] self.containers = []
self.started_by = started_by self.started_by = started_by
self.tags = tags
self.stopped_reason = "" self.stopped_reason = ""
self.resource_requirements = resource_requirements self.resource_requirements = resource_requirements
@ -790,7 +792,9 @@ class EC2ContainerServiceBackend(BaseBackend):
else: else:
raise TaskDefinitionNotFoundException 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) cluster = self._get_cluster(cluster_str)
task_definition = self.describe_task_definition(task_definition_str) task_definition = self.describe_task_definition(task_definition_str)
@ -830,6 +834,7 @@ class EC2ContainerServiceBackend(BaseBackend):
resource_requirements, resource_requirements,
overrides or {}, overrides or {},
started_by or "", started_by or "",
tags or [],
) )
self.update_container_instance_resources( self.update_container_instance_resources(
container_instance, resource_requirements container_instance, resource_requirements

View File

@ -111,8 +111,9 @@ class EC2ContainerServiceResponse(BaseResponse):
task_definition_str = self._get_param("taskDefinition") task_definition_str = self._get_param("taskDefinition")
count = self._get_int_param("count") count = self._get_int_param("count")
started_by = self._get_param("startedBy") started_by = self._get_param("startedBy")
tags = self._get_param("tags")
tasks = self.ecs_backend.run_task( 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( return json.dumps(
{"tasks": [task.response_object for task in tasks], "failures": []} {"tasks": [task.response_object for task in tasks], "failures": []}

View File

@ -1457,6 +1457,10 @@ def test_run_task():
taskDefinition="test_ecs_task", taskDefinition="test_ecs_task",
count=2, count=2,
startedBy="moto", startedBy="moto",
tags=[
{"key": "tagKey0", "value": "tagValue0"},
{"key": "tagKey1", "value": "tagValue1"},
],
) )
len(response["tasks"]).should.equal(2) len(response["tasks"]).should.equal(2)
response["tasks"][0]["taskArn"].should.contain( response["tasks"][0]["taskArn"].should.contain(
@ -1476,6 +1480,7 @@ def test_run_task():
response["tasks"][0]["desiredStatus"].should.equal("RUNNING") response["tasks"][0]["desiredStatus"].should.equal("RUNNING")
response["tasks"][0]["startedBy"].should.equal("moto") response["tasks"][0]["startedBy"].should.equal("moto")
response["tasks"][0]["stoppedReason"].should.equal("") response["tasks"][0]["stoppedReason"].should.equal("")
response["tasks"][0]["tags"][0].get("value").should.equal("tagValue0")
@mock_ec2 @mock_ec2