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": []
}
```
This commit is contained in:
Alex Tareshawty 2019-11-26 09:17:54 -05:00
parent 502957f1f9
commit df2279d39c
3 changed files with 68 additions and 6 deletions

View File

@ -567,16 +567,14 @@ class EC2ContainerServiceBackend(BaseBackend):
return task_definition return task_definition
def list_task_definitions(self): def list_task_definitions(self, family_prefix):
"""
Filtering not implemented
"""
task_arns = [] task_arns = []
for task_definition_list in self.task_definitions.values(): for task_definition_list in self.task_definitions.values():
task_arns.extend( task_arns.extend(
[ [
task_definition.arn task_definition.arn
for task_definition in task_definition_list.values() for task_definition in task_definition_list.values()
if family_prefix is None or task_definition.family == family_prefix
] ]
) )
return task_arns return task_arns

View File

@ -68,7 +68,8 @@ class EC2ContainerServiceResponse(BaseResponse):
return json.dumps({"taskDefinition": task_definition.response_object}) return json.dumps({"taskDefinition": task_definition.response_object})
def list_task_definitions(self): 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( return json.dumps(
{ {
"taskDefinitionArns": task_definition_arns "taskDefinitionArns": task_definition_arns

View File

@ -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 @mock_ecs
def test_describe_task_definition(): def test_describe_task_definition():
client = boto3.client("ecs", region_name="us-east-1") 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) cfn_conn.update_stack(StackName="test_stack", TemplateBody=template2_json)
ecs_conn = boto3.client("ecs", region_name="us-west-1") 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) len(resp["taskDefinitionArns"]).should.equal(1)
resp["taskDefinitionArns"][0].endswith("testTaskDefinition2:1").should.be.true resp["taskDefinitionArns"][0].endswith("testTaskDefinition2:1").should.be.true