def main()

in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_slb_lb.py [0:0]


def main():
    argument_spec = common_argument_spec()
    argument_spec.update(dict(
        internet_charge_type=dict(type='str', choices=['PayByBandwidth', 'PayByTraffic'], default='PayByTraffic'),
        state=dict(type='str', choices=['present', 'absent', 'running', 'stopped'], default='present'),
        load_balancer_name=dict(type='str', required=True, aliases=['name', 'lb_name']),
        load_balancer_id=dict(type='str', aliases=['id']),
        is_internet=dict(type='bool', default=False),
        bandwidth=dict(type='int', default=1),
        vswitch_id=dict(type='str', aliases=['subnet_id']),
        master_zone_id=dict(type='str'),
        slave_zone_id=dict(type='str'),
        load_balancer_spec=dict(type='str', aliases=['spec', 'lb_spec'],
                                choices=['slb.s1.small', 'slb.s2.small', 'slb.s2.medium', 'slb.s3.small', 'slb.s3.medium', 'slb.s3.large']),
        multi_ok=dict(type='bool', default=False),
        tags=dict(type='dict'),
        purge_tags=dict(type='bool', default=False)
    ))

    module = AnsibleModule(argument_spec=argument_spec)

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

    slb = slb_connect(module)
    state = module.params['state']
    name = module.params['load_balancer_name']
    load_balancer_id = module.params['load_balancer_id']
    is_internet = module.params['is_internet']
    internet_charge_type = str(module.params['internet_charge_type']).lower()

    changed = False
    matching = None

    filters = {}
    if name:
        filters['load_balancer_name'] = name
    if load_balancer_id:
        filters['load_balancer_id'] = load_balancer_id
    if not module.params['multi_ok']:
        try:
            matching_slbs = slb.describe_load_balancers(**filters)
            if len(matching_slbs) == 1:
                matching = matching_slbs[0]
            elif len(matching_slbs) > 1:
                module.fail_json(msg='Currently there are {0} Load Balancers that have the same name {1}. '
                                     'If you would like to create anyway '
                                     'please pass True to the multi_ok param.'.format(len(matching_slbs), name))
        except Exception as e:
            module.fail_json(msg="Failed to describe Load Balancers: {0}".format(e))

    if state == "absent":
        if matching:
            try:
                changed = matching.delete()
            except Exception as e:
                module.fail_json(msg="Failed to delete Load Balancers: {0}".format(e))
        module.exit_json(changed=changed, load_balancer={})

    if state == "present":
        if not matching:
            params = module.params
            params['internet_charge_type'] = internet_charge_type
            params['client_token'] = "Ansible-Apsarastack-%s-%s" % (hash(str(module.params)), str(time.time()))
            address_type = "intranet"
            if is_internet:
                address_type = "internet"
            params['address_type'] = address_type
            try:
                matching = slb.create_load_balancer(**params)
                changed = False
            except Exception as e:
                module.fail_json(msg="Failed to create Load Balancer: {0}".format(e))

    if not matching:
        module.fail_json(msg="The specified load balancer {0} is not exist. Please check your name and try again.".format(name))

    if not internet_charge_type:
        internet_charge_type = str(matching.internet_charge_type).lower()

    bandwidth = module.params['bandwidth']
    if not bandwidth:
        bandwidth = matching.bandwidth
    try:
        if matching.modify_spec(internet_charge_type=internet_charge_type, bandwidth=bandwidth):
            changed = True
        matching = matching.get()
    except Exception as e:
        module.fail_json(msg="Failed to modify Load Balancer spec: {0}".format(e))

    status = "active"
    if state == "stopped":
        status = "inactive"

    try:
        if matching.set_status(status):
            changed = True
    except Exception as e:
        module.fail_json(msg="Failed to modify Load Balancer status: {0}".format(e))

    tags = module.params['tags']
    if module.params['purge_tags']: