Skip to content

Commit

Permalink
Fix parameter validation in ecs_task (ansible-collections#402)
Browse files Browse the repository at this point in the history
* Fix parameter validation in ecs_task
* Require cluster parameter in ecs_task module
* Move parameter validation to AnsibleAWSModule
* Fix pep8 formatting line too long
* changelog
  • Loading branch information
mjmayer authored Feb 11, 2021
1 parent ba3bdcb commit 064c508
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions ecs_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
operation:
description:
- Which task operation to execute.
- When I(operation=run) I(task_definition) must be set.
- When I(operation=start) both I(task_definition) and I(container_instances) must be set.
- When I(operation=stop) both I(task_definition) and I(task) must be set.
required: True
choices: ['run', 'start', 'stop']
type: str
cluster:
description:
- The name of the cluster to run the task on.
required: False
required: True
type: str
task_definition:
description:
- The task definition to start or run.
- The task definition to start, run or stop.
required: False
type: str
overrides:
Expand All @@ -44,7 +47,7 @@
type: int
task:
description:
- The task to stop.
- The ARN of the task to stop.
required: False
type: str
container_instances:
Expand Down Expand Up @@ -332,7 +335,7 @@ def ecs_api_handles_network_configuration(self):
def main():
argument_spec = dict(
operation=dict(required=True, choices=['run', 'start', 'stop']),
cluster=dict(required=False, type='str'), # R S P
cluster=dict(required=True, type='str'), # R S P
task_definition=dict(required=False, type='str'), # R* S*
overrides=dict(required=False, type='dict'), # R S
count=dict(required=False, type='int'), # R
Expand All @@ -345,28 +348,26 @@ def main():
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True,
required_if=[('launch_type', 'FARGATE', ['network_configuration'])])
required_if=[
('launch_type', 'FARGATE', ['network_configuration']),
('operation', 'run', ['task_definition']),
('operation', 'start', [
'task_definition',
'container_instances'
]),
('operation', 'stop', ['task_definition', 'task']),
])

# Validate Inputs
if module.params['operation'] == 'run':
if 'task_definition' not in module.params and module.params['task_definition'] is None:
module.fail_json(msg="To run a task, a task_definition must be specified")
task_to_list = module.params['task_definition']
status_type = "RUNNING"

if module.params['operation'] == 'start':
if 'task_definition' not in module.params and module.params['task_definition'] is None:
module.fail_json(msg="To start a task, a task_definition must be specified")
if 'container_instances' not in module.params and module.params['container_instances'] is None:
module.fail_json(msg="To start a task, container instances must be specified")
task_to_list = module.params['task']
status_type = "RUNNING"

if module.params['operation'] == 'stop':
if 'task' not in module.params and module.params['task'] is None:
module.fail_json(msg="To stop a task, a task must be specified")
if 'task_definition' not in module.params and module.params['task_definition'] is None:
module.fail_json(msg="To stop a task, a task definition must be specified")
task_to_list = module.params['task_definition']
status_type = "STOPPED"

Expand Down

0 comments on commit 064c508

Please sign in to comment.