def main()

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


def main():
    argument_spec = common_argument_spec()
    argument_spec.update(dict(
        listener_port=dict(type='int', required=True, aliases=['frontend_port']),
        state=dict(type='str', required=True, choices=['present', 'absent', 'stopped', 'running']),
        load_balancer_id=dict(type='str', required=True, aliases=['id']),
        backend_server_port=dict(type='int', aliases=['backend_port']),
        bandwidth=dict(type='int'),
        sticky_session=dict(type='str', choices=['on', 'off'], default='off'),
        protocol=dict(type='str', choices=['http', 'https', 'tcp', 'udp']),
        health_check=dict(type='str', default='on', choices=['on', 'off']),
        scheduler=dict(type='str', default='wrr', choices=['wrr', 'wlc']),
        sticky_session_type=dict(type='str', choices=['insert', 'server']),
        cookie_timeout=dict(type='str'),
        cookie=dict(type='str'),
        health_check_domain=dict(type='str'),
        health_check_uri=dict(type='str', default='/'),
        health_check_connect_port=dict(type='int'),
        healthy_threshold=dict(type='int', default=3),
        unhealthy_threshold=dict(type='int', default=3),
        health_check_timeout=dict(type='int', default=5),
        health_check_interval=dict(type='int', default=2),
        health_check_http_code=dict(type='str', default='http_2xx', choices=['http_2xx', 'http_3xx', 'http_4xx', 'http_5xx']),
        vserver_group_id=dict(type='str'),
        persistence_timeout=dict(type='int', default=0),
        server_certificate_id=dict(type='str'),
        health_check_type=dict(type='str', default='tcp', choices=['tcp', 'http']),
    ))

    module = AnsibleModule(argument_spec=argument_spec)

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

    slb = slb_connect(module)
    state = module.params['state']
    load_balancer_id = module.params['load_balancer_id']
    listener_port = module.params['listener_port']
    backend_server_port = module.params['backend_server_port']
    bandwidth = module.params['bandwidth']
    sticky_session = module.params['sticky_session']
    protocol = module.params['protocol']
    health_check = module.params['health_check']
    scheduler = module.params['scheduler']
    sticky_session_type = module.params['sticky_session_type']
    cookie_timeout = module.params['cookie_timeout']
    cookie = module.params['cookie']
    health_check_domain = module.params['health_check_domain']
    health_check_uri = module.params['health_check_uri']
    health_check_connect_port = module.params['health_check_connect_port']
    healthy_threshold = module.params['healthy_threshold']
    unhealthy_threshold = module.params['unhealthy_threshold']
    health_check_timeout = module.params['health_check_timeout']
    health_check_interval = module.params['health_check_interval']
    health_check_http_code = module.params['health_check_http_code']
    vserver_group_id = module.params['vserver_group_id']
    server_certificate_id = module.params['server_certificate_id']
    persistence_timeout = module.params['persistence_timeout']
    health_check_type = module.params['health_check_type']

    current_listener = slb.describe_load_balancer_listener_attribute(load_balancer_id, listener_port, protocol)
    changed = False
    if state == "present":
        if current_listener:
            changed = current_listener.set_attribute(load_balancer_id=load_balancer_id,
                                                     bandwidth=bandwidth,
                                                     sticky_session=sticky_session,
                                                     protocol=protocol,
                                                     health_check=health_check,
                                                     scheduler=scheduler,
                                                     sticky_session_type=sticky_session_type,
                                                     cookie_timeout=cookie_timeout,
                                                     cookie=cookie,
                                                     health_check_domain=health_check_domain,
                                                     health_check_uri=health_check_uri,
                                                     health_check_connect_port=health_check_connect_port,
                                                     healthy_threshold=healthy_threshold,
                                                     unhealthy_threshold=unhealthy_threshold,
                                                     health_check_timeout=health_check_timeout,
                                                     health_check_interval=health_check_interval,
                                                     health_check_http_code=health_check_http_code,
                                                     vserver_group_id=vserver_group_id,
                                                     server_certificate_id=server_certificate_id,
                                                     persistence_timeout=persistence_timeout,
                                                     health_check_type=health_check_type)
            module.exit_json(changed=changed, listener=get_info(current_listener.describe_attribute(load_balancer_id, protocol)))
        else:
            changed = slb.create_load_balancer_listener(load_balancer_id=load_balancer_id,
                                                        listener_port=listener_port,
                                                        backend_server_port=backend_server_port,
                                                        bandwidth=bandwidth,
                                                        sticky_session=sticky_session,
                                                        protocol=protocol,
                                                        health_check=health_check,
                                                        scheduler=scheduler,
                                                        sticky_session_type=sticky_session_type,
                                                        cookie_timeout=cookie_timeout,
                                                        cookie=cookie,
                                                        health_check_domain=health_check_domain,
                                                        health_check_uri=health_check_uri,
                                                        health_check_connect_port=health_check_connect_port,
                                                        healthy_threshold=healthy_threshold,
                                                        unhealthy_threshold=unhealthy_threshold,
                                                        health_check_timeout=health_check_timeout,
                                                        health_check_interval=health_check_interval,
                                                        health_check_http_code=health_check_http_code,
                                                        vserver_group_id=vserver_group_id,
                                                        server_certificate_id=server_certificate_id,
                                                        persistence_timeout=persistence_timeout)
            new_current_listener = slb.describe_load_balancer_listener_attribute(load_balancer_id, listener_port, protocol)
            module.exit_json(changed=changed, listener=get_info(new_current_listener))
    if not current_listener:
        module.fail_json(msg="The specified load balancer listener is not exist. Please check your load_balancer_id or listener_port and try again.")
    if state == "absent":
        changed = current_listener.delete(load_balancer_id)
        module.exit_json(changed=changed, listener=get_info(current_listener))
    if state == "running":
        if current_listener.status == "stopped":
            # start
            changed = current_listener.start(load_balancer_id)
        module.exit_json(changed=changed, listener=get_info(current_listener.describe_attribute(load_balancer_id, protocol)))
    if state == "stopped":
        if current_listener.status == "running":
            # stop
            changed = current_listener.stop(load_balancer_id)
        module.exit_json(changed=changed, listener=get_info(current_listener.describe_attribute(load_balancer_id, protocol)))