"""
Autogenerated state module using `pop-create-idem <https://gitlab.com/saltstack/pop/pop-create-idem>`__

hub.exec.boto3.client.globalaccelerator.create_listener
hub.exec.boto3.client.globalaccelerator.delete_listener
hub.exec.boto3.client.globalaccelerator.describe_listener
hub.exec.boto3.client.globalaccelerator.list_listeners
hub.exec.boto3.client.globalaccelerator.update_listener
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, accelerator_arn: Text, port_ranges: List, protocol: Text, idempotency_token: Text, client_affinity: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Create a listener to process inbound connections from clients to an accelerator. Connections arrive to assigned
    static IP addresses on a port, port range, or list of port ranges that you specify.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        accelerator_arn(Text): The Amazon Resource Name (ARN) of your accelerator.
        port_ranges(List): The list of port ranges to support for connections from clients to your accelerator.
        protocol(Text): The protocol for connections from clients to your accelerator.
        client_affinity(Text, optional): Client affinity lets you direct all requests from a user to the same endpoint, if you have
            stateful applications, regardless of the port and protocol of the client request. Client
            affinity gives you control over whether to always route each client to the same specific
            endpoint. AWS Global Accelerator uses a consistent-flow hashing algorithm to choose the optimal
            endpoint for a connection. If client affinity is NONE, Global Accelerator uses the "five-tuple"
            (5-tuple) properties—source IP address, source port, destination IP address, destination port,
            and protocol—to select the hash value, and then chooses the best endpoint. However, with this
            setting, if someone uses different ports to connect to Global Accelerator, their connections
            might not be always routed to the same endpoint because the hash value changes.  If you want a
            given client to always be routed to the same endpoint, set client affinity to SOURCE_IP instead.
            When you use the SOURCE_IP setting, Global Accelerator uses the "two-tuple" (2-tuple)
            properties— source (client) IP address and destination IP address—to select the hash value. The
            default value is NONE. Defaults to None.
        idempotency_token(Text): A unique, case-sensitive identifier that you provide to ensure the idempotency—that is, the
            uniqueness—of the request.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.globalaccelerator.listener.present:
                - name: value
                - accelerator_arn: value
                - port_ranges: value
                - protocol: value
                - idempotency_token: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.globalaccelerator.listener.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    
    before = await hub.exec.boto3.client.globalaccelerator.describe_listener(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.globalaccelerator.create_listener(
                ctx,
                
                
                **{"AcceleratorArn": accelerator_arn, "PortRanges": port_ranges, "Protocol": protocol, "ClientAffinity": client_affinity, "IdempotencyToken": idempotency_token}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            ret["comment"] = f"Created '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    
    # TODO perform other modifications as needed here
    ...

    after = await hub.exec.boto3.client.globalaccelerator.describe_listener(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, listener_arn: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Delete a listener from an accelerator.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        listener_arn(Text): The Amazon Resource Name (ARN) of the listener.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.globalaccelerator.listener.absent:
                - name: value
                - listener_arn: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.globalaccelerator.listener.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    

    before = await hub.exec.boto3.client.globalaccelerator.describe_listener(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.globalaccelerator.delete_listener(
                ctx,
                
                
                **{"ListenerArn": listener_arn}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            result["comment"] = f"Deleted '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    

    after = await hub.exec.boto3.client.globalaccelerator.describe_listener(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

