Skip to content

Commit

Permalink
ecs_service - fix validation for placementConstraints (ansible-coll…
Browse files Browse the repository at this point in the history
…ections#1170)

ecs_service - fix validation for `placementConstraints`

SUMMARY
Fixes ansible-collections#1058
ISSUE TYPE
Bugfix Pull Request
COMPONENT NAME
ecs_service

Reviewed-by: Markus Bergholz <[email protected]>
Reviewed-by: Oleksandr Novak <[email protected]>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
novak-oleksandr authored May 30, 2022
1 parent 20a87c0 commit 688c7e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ecs_service - fix validation for ``placement_constraints``. It's possible to use ``distinctInstance`` placement constraint now (https://github.com/ansible-collections/community.aws/issues/1058)
11 changes: 9 additions & 2 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
type: str
expression:
description: A cluster query language expression to apply to the constraint.
required: false
type: str
placement_strategy:
description:
Expand Down Expand Up @@ -584,7 +585,6 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan
clientToken=client_token,
role=role,
deploymentConfiguration=deployment_configuration,
placementConstraints=placement_constraints,
placementStrategy=placement_strategy
)
if network_configuration:
Expand All @@ -597,6 +597,13 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan
params['healthCheckGracePeriodSeconds'] = health_check_grace_period_seconds
if service_registries:
params['serviceRegistries'] = service_registries

# filter placement_constraint and left only those where value is not None
# use-case: `distinctInstance` type should never contain `expression`, but None will fail `str` type validation
if placement_constraints:
params['placementConstraints'] = [{key: value for key, value in constraint.items() if value is not None}
for constraint in placement_constraints]

# desired count is not required if scheduling strategy is daemon
if desired_count is not None:
params['desiredCount'] = desired_count
Expand Down Expand Up @@ -674,7 +681,7 @@ def main():
elements='dict',
options=dict(
type=dict(type='str'),
expression=dict(type='str')
expression=dict(required=False, type='str')
)
),
placement_strategy=dict(
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/ecs_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@
desired_count: 1
deployment_configuration: "{{ ecs_service_deployment_configuration }}"
placement_strategy: "{{ ecs_service_placement_strategy }}"
placement_constraints:
- type: distinctInstance
health_check_grace_period_seconds: "{{ ecs_service_health_check_grace_period }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
Expand All @@ -223,6 +225,12 @@
role: "ecsServiceRole"
register: ecs_service

- name: check that placement constraint has been applied
assert:
that:
- ecs_service.changed
- "ecs_service.service.placementConstraints[0].type == 'distinctInstance'"

- name: check that ECS service creation changed
assert:
that:
Expand All @@ -237,6 +245,8 @@
desired_count: 1
deployment_configuration: "{{ ecs_service_deployment_configuration }}"
placement_strategy: "{{ ecs_service_placement_strategy }}"
placement_constraints:
- type: distinctInstance
health_check_grace_period_seconds: "{{ ecs_service_health_check_grace_period }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
Expand All @@ -260,6 +270,8 @@
desired_count: 1
deployment_configuration: "{{ ecs_service_deployment_configuration }}"
placement_strategy: "{{ ecs_service_placement_strategy }}"
placement_constraints:
- type: distinctInstance
health_check_grace_period_seconds: "{{ ecs_service_health_check_grace_period }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
Expand Down

0 comments on commit 688c7e8

Please sign in to comment.