def main()

in plugins/modules/ali_ess_task.py [0:0]


def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(dict(
        rule_id=dict(type='str'),
        launch_time=dict(type='str'),
        launch_expiration=dict(type='int', defalut=600, aliases=['expiration']),
        name=dict(type='str', aliases=['task_name']),
        description=dict(type='str'),
        recurrence_type=dict(type='str', choices=['Daily', 'Weekly', 'Monthly'], aliases=['type']),
        recurrence_value=dict(type='str', aliases=['value']),
        recurrence_endtime=dict(type='str', aliases=['endtime']),
        state=dict(type='str', default='present', choices=['present', 'absent']),
        enabled=dict(type='bool', default=True),
        id=dict(type='str', aliases=['task_id'])
    ))

    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for the module ali_ess_task.")

    ess = ess_connect(module)
    state = module.params['state']
    task_id = module.params['id']
    task_name = module.params['name']
    rule_id = module.params['rule_id']
    launch_time = module.params['launch_time']
    launch_expiration = module.params['launch_expiration']
    recurrence_type = module.params['recurrence_type']
    recurrence_value = module.params['recurrence_value']
    recurrence_endtime = module.params['recurrence_endtime']
    enabled = module.params['enabled']
    description = module.params['description']

    # Get scaling rule ari according rule ID
    rule_ari = None
    if rule_id:
        rules = ess.describe_rules(scaling_rule_ids=[rule_id])
        rule_ari = rules[0].ari

    count = 0
    if recurrence_type:
        count += 1
    if recurrence_value:
        count += 1
    if recurrence_endtime:
        count += 1
    if count in (1, 2):
        module.fail_json(msg="'recurrence_type', 'recurrence_value' and 'recurrence_endtime' must be specified or not at the same time")

    changed = False

    current = None
    all_tasks = []
    if task_id or task_name:
        tasks = ess.describe_scheduled_tasks(scheduled_task_ids=[task_id], scheduled_task_names=[task_name],
                                             scaling_rule_aris=[rule_ari])

        if tasks:
            if len(tasks) > 1:
                for task in tasks:
                    all_tasks.append(task.id)
                module.fail_json(msg="There are several scheduled tasks in our record based on name {0}: {1}. "
                                     "Please specified one using 'id' and try again.".format(task_name, all_tasks))
            current = tasks[0]

    if state == 'present':
        if current is None:
            try:
                if not rule_id:
                    module.exit_json(msg="'rule_id': required field when state is present, aborting.")
                if not rule_ari:
                    module.exit_json(msg="There is no scheduled task in our record based on rule id {0}, aborting."
                                         "Please check it and try again.".format(rule_id))
                if not launch_time:
                    module.exit_json(msg="'launch_time': required field when state is present, aborting.")

                current = ess.create_scheduled_task(scaling_rule_ari=rule_ari, launch_time=launch_time, name=task_name,
                                                    description=description, launch_expiration_time=launch_expiration,
                                                    recurrence_type=recurrence_type, recurrence_value=recurrence_value,
                                                    recurrence_end_time=recurrence_endtime, task_enabled=enabled)
                changed = True
            except Exception as e:
                module.fail_json(msg="Create scheduled task got an error: {0}".format(e))

        else:
            try:
                changed = current.modify(scaling_rule_ari=rule_ari, launch_time=launch_time, name=task_name,
                                         description=description, launch_expiration_time=launch_expiration,
                                         recurrence_type=recurrence_type, recurrence_value=recurrence_value,
                                         recurrence_end_time=recurrence_endtime, task_enabled=enabled)
                if changed:
                    current = current.update(validate=True)
            except Exception as e:
                module.fail_json(msg="Modify scheduled rule got an error: {0}".format(e))

        module.exit_json(changed=changed, id=current.id, name=current.name, task=get_details(current))

    if current is None:
        if task_id or task_name:
                module.fail_json(msg="There are no scheduled task in our record based on id {0} or name {1}. "
                                     "Please check it and try again.".format(task_id, task_name))
        module.fail_json(msg='Please specify a scheduled task that you want to terminate by field id or name, aborting')

    try:
        module.exit_json(changed=current.terminate())
    except Exception as e:
        module.fail_json(msg='Delete scheduled task {0} got an error: {1}'.format(current.id, e))