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

hub.exec.boto3.client.elbv2.create_listener
hub.exec.boto3.client.elbv2.delete_listener
hub.exec.boto3.client.elbv2.describe_listeners
hub.exec.boto3.client.elbv2.modify_listener
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, load_balancer_arn: Text, default_actions: List, protocol: Text = None, port: int = None, ssl_policy: Text = None, certificates: List = None, alpn_policy: List = None, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a listener for the specified Application Load Balancer, Network Load Balancer, or Gateway Load Balancer.
    For more information, see the following:    Listeners for your Application Load Balancers     Listeners for your
    Network Load Balancers     Listeners for your Gateway Load Balancers    This operation is idempotent, which
    means that it completes at most one time. If you attempt to create multiple listeners with the same settings,
    each call succeeds.

    Args:
        name(Text): The name of the state.
        load_balancer_arn(Text): The Amazon Resource Name (ARN) of the load balancer.
        protocol(Text, optional): The protocol for connections from clients to the load balancer. For Application Load Balancers,
            the supported protocols are HTTP and HTTPS. For Network Load Balancers, the supported protocols
            are TCP, TLS, UDP, and TCP_UDP. You can’t specify the UDP or TCP_UDP protocol if dual-stack mode
            is enabled. You cannot specify a protocol for a Gateway Load Balancer. Defaults to None.
        port(int, optional): The port on which the load balancer is listening. You cannot specify a port for a Gateway Load
            Balancer. Defaults to None.
        ssl_policy(Text, optional): [HTTPS and TLS listeners] The security policy that defines which protocols and ciphers are
            supported. For more information, see Security policies in the Application Load Balancers Guide
            and Security policies in the Network Load Balancers Guide. Defaults to None.
        certificates(List, optional): [HTTPS and TLS listeners] The default certificate for the listener. You must provide exactly one
            certificate. Set CertificateArn to the certificate ARN but do not set IsDefault. Defaults to None.
        default_actions(List): The actions for the default rule.
        alpn_policy(List, optional): [TLS listeners] The name of the Application-Layer Protocol Negotiation (ALPN) policy. You can
            specify one policy name. The following are the possible values:    HTTP1Only     HTTP2Only
            HTTP2Optional     HTTP2Preferred     None    For more information, see ALPN policies in the
            Network Load Balancers Guide. Defaults to None.
        tags(List, optional): The tags to assign to the listener. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.elbv2.listener.present:
                - name: value
                - load_balancer_arn: value
                - default_actions: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)

    
    before = await hub.exec.boto3.client.elbv2.describe_listeners(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.elbv2.create_listener(
                ctx,
                
                
                **{"LoadBalancerArn": load_balancer_arn, "Protocol": protocol, "Port": port, "SslPolicy": ssl_policy, "Certificates": certificates, "DefaultActions": default_actions, "AlpnPolicy": alpn_policy, "Tags": tags}
            )
            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.elbv2.describe_listeners(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**
    
    Deletes the specified listener. Alternatively, your listener is deleted when you delete the load balancer to
    which it is attached.

    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.elbv2.listener.absent:
                - name: value
                - listener_arn: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.elbv2.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.elbv2.describe_listeners(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.elbv2.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.elbv2.describe_listeners(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

