From df2279d39c7e3e6d2f847c598b67c97d89aba0fa Mon Sep 17 00:00:00 2001 From: Alex Tareshawty Date: Tue, 26 Nov 2019 09:17:54 -0500 Subject: [PATCH] Add familyPrefix option to ecs:ListTaskDefinitions AWS defines this option as: ``` --family-prefix (string) The full family name with which to filter the ListTaskDefinitions results. Specifying a familyPrefix limits the listed task defini-tions to task definition revisions that belong to that family. ``` This option behaves differently than ecs:ListTaskDefinitionFamilies. Instead of doing a comparison like `startswith`, it does a full string comparison by matching the entire task definition family to the prefix. For example, let's say there exists a task definition with the family `super-cool-task-def`. ListTaskDefinitionFamilies would look like this: ``` aws ecs list-task-definition-families --family-prefix super-cool { "families": [ "super-cool-task-def" ] } ``` ListTaskDefinitions would look like this: ``` aws ecs list-task-definitions --family-prefix super-cool { "taskDefinitionArns": [] } ``` --- moto/ecs/models.py | 6 +-- moto/ecs/responses.py | 3 +- tests/test_ecs/test_ecs_boto3.py | 65 +++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 87578202c..c9dc998ee 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -567,16 +567,14 @@ class EC2ContainerServiceBackend(BaseBackend): return task_definition - def list_task_definitions(self): - """ - Filtering not implemented - """ + def list_task_definitions(self, family_prefix): task_arns = [] for task_definition_list in self.task_definitions.values(): task_arns.extend( [ task_definition.arn for task_definition in task_definition_list.values() + if family_prefix is None or task_definition.family == family_prefix ] ) return task_arns diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 5116b0472..d08bded2c 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -68,7 +68,8 @@ class EC2ContainerServiceResponse(BaseResponse): return json.dumps({"taskDefinition": task_definition.response_object}) def list_task_definitions(self): - task_definition_arns = self.ecs_backend.list_task_definitions() + family_prefix = self._get_param("familyPrefix") + task_definition_arns = self.ecs_backend.list_task_definitions(family_prefix) return json.dumps( { "taskDefinitionArns": task_definition_arns diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index 224e6935b..973c95b81 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -171,6 +171,69 @@ def test_list_task_definitions(): ) +@mock_ecs +def test_list_task_definitions_with_family_prefix(): + client = boto3.client("ecs", region_name="us-east-1") + _ = client.register_task_definition( + family="test_ecs_task_a", + containerDefinitions=[ + { + "name": "hello_world", + "image": "docker/hello-world:latest", + "cpu": 1024, + "memory": 400, + "essential": True, + "environment": [ + {"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY"} + ], + "logConfiguration": {"logDriver": "json-file"}, + } + ], + ) + _ = client.register_task_definition( + family="test_ecs_task_a", + containerDefinitions=[ + { + "name": "hello_world", + "image": "docker/hello-world:latest", + "cpu": 1024, + "memory": 400, + "essential": True, + "environment": [ + {"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY"} + ], + "logConfiguration": {"logDriver": "json-file"}, + } + ], + ) + _ = client.register_task_definition( + family="test_ecs_task_b", + containerDefinitions=[ + { + "name": "hello_world2", + "image": "docker/hello-world2:latest", + "cpu": 1024, + "memory": 400, + "essential": True, + "environment": [ + {"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY2"} + ], + "logConfiguration": {"logDriver": "json-file"}, + } + ], + ) + empty_response = client.list_task_definitions(familyPrefix="test_ecs_task") + len(empty_response["taskDefinitionArns"]).should.equal(0) + filtered_response = client.list_task_definitions(familyPrefix="test_ecs_task_a") + len(filtered_response["taskDefinitionArns"]).should.equal(2) + filtered_response["taskDefinitionArns"][0].should.equal( + "arn:aws:ecs:us-east-1:012345678910:task-definition/test_ecs_task_a:1" + ) + filtered_response["taskDefinitionArns"][1].should.equal( + "arn:aws:ecs:us-east-1:012345678910:task-definition/test_ecs_task_a:2" + ) + + @mock_ecs def test_describe_task_definition(): client = boto3.client("ecs", region_name="us-east-1") @@ -1756,7 +1819,7 @@ def test_update_task_definition_family_through_cloudformation_should_trigger_a_r cfn_conn.update_stack(StackName="test_stack", TemplateBody=template2_json) ecs_conn = boto3.client("ecs", region_name="us-west-1") - resp = ecs_conn.list_task_definitions(familyPrefix="testTaskDefinition") + resp = ecs_conn.list_task_definitions(familyPrefix="testTaskDefinition2") len(resp["taskDefinitionArns"]).should.equal(1) resp["taskDefinitionArns"][0].endswith("testTaskDefinition2:1").should.be.true