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

hub.exec.boto3.client.apprunner.associate_custom_domain
hub.exec.boto3.client.apprunner.describe_custom_domains
hub.exec.boto3.client.apprunner.disassociate_custom_domain
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, service_arn: Text, domain_name: Text, enable_www_subdomain: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Associate your own domain name with the AWS App Runner subdomain URL of your App Runner service. After you call
    AssociateCustomDomain and receive a successful response, use the information in the CustomDomain record that's
    returned to add CNAME records to your Domain Name System (DNS). For each mapped domain name, add a mapping to
    the target App Runner subdomain and one or more certificate validation records. App Runner then performs DNS
    validation to verify that you own or control the domain name that you associated. App Runner tracks domain
    validity in a certificate stored in AWS Certificate Manager (ACM).

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        service_arn(Text): The Amazon Resource Name (ARN) of the App Runner service that you want to associate a custom
            domain name with.
        domain_name(Text): A custom domain endpoint to associate. Specify a root domain (for example, example.com), a
            subdomain (for example, login.example.com or admin.login.example.com), or a wildcard (for
            example, *.example.com).
        enable_www_subdomain(bool, optional): Set to true to associate the subdomain www.DomainName  with the App Runner service in addition
            to the base domain. Default: true. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.apprunner.custom_domain.present:
                - name: value
                - service_arn: value
                - domain_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.apprunner.custom_domain.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.apprunner.describe_custom_domains(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.apprunner.associate_custom_domain(
                ctx,
                
                
                **{"ServiceArn": service_arn, "DomainName": domain_name, "EnableWWWSubdomain": enable_www_subdomain}
            )
            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.apprunner.describe_custom_domains(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, service_arn: Text, domain_name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Disassociate a custom domain name from an AWS App Runner service. Certificates tracking domain validity are
    associated with a custom domain and are stored in AWS Certificate Manager (ACM). These certificates aren't
    deleted as part of this action. App Runner delays certificate deletion for 30 days after a domain is
    disassociated from your service.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        service_arn(Text): The Amazon Resource Name (ARN) of the App Runner service that you want to disassociate a custom
            domain name from.
        domain_name(Text): The domain name that you want to disassociate from the App Runner service.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.apprunner.custom_domain.absent:
                - name: value
                - service_arn: value
                - domain_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.apprunner.custom_domain.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.apprunner.describe_custom_domains(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.apprunner.disassociate_custom_domain(
                ctx,
                
                
                **{"ServiceArn": service_arn, "DomainName": domain_name}
            )
            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.apprunner.describe_custom_domains(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

