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

hub.exec.boto3.client.acm_pca.create_permission
hub.exec.boto3.client.acm_pca.delete_permission
hub.exec.boto3.client.acm_pca.list_permissions
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, certificate_authority_arn: Text, principal: Text, actions: List, source_account: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Grants one or more permissions on a private CA to the AWS Certificate Manager (ACM) service principal
    (acm.amazonaws.com). These permissions allow ACM to issue and renew ACM certificates that reside in the same AWS
    account as the CA. You can list current permissions with the ListPermissions action and revoke them with the
    DeletePermission action.  About Permissions    If the private CA and the certificates it issues reside in the
    same account, you can use CreatePermission to grant permissions for ACM to carry out automatic certificate
    renewals.   For automatic certificate renewal to succeed, the ACM service principal needs permissions to create,
    retrieve, and list certificates.   If the private CA and the ACM certificates reside in different accounts, then
    permissions cannot be used to enable automatic renewals. Instead, the ACM certificate owner must set up a
    resource-based policy to enable cross-account issuance and renewals. For more information, see Using a Resource
    Based Policy with ACM Private CA.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        certificate_authority_arn(Text): The Amazon Resource Name (ARN) of the CA that grants the permissions. You can find the ARN by
            calling the ListCertificateAuthorities action. This must have the following form:   arn:aws:acm-
            pca:region:account:certificate-authority/12345678-1234-1234-1234-123456789012 .
        principal(Text): The AWS service or identity that receives the permission. At this time, the only valid principal
            is acm.amazonaws.com.
        source_account(Text, optional): The ID of the calling account. Defaults to None.
        actions(List): The actions that the specified AWS service principal can use. These include IssueCertificate,
            GetCertificate, and ListPermissions.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.acm_pca.permission.present:
                - name: value
                - certificate_authority_arn: value
                - principal: value
                - actions: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.acm_pca.permission.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.acm_pca.list_permissions(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.acm_pca.create_permission(
                ctx,
                
                
                **{"CertificateAuthorityArn": certificate_authority_arn, "Principal": principal, "SourceAccount": source_account, "Actions": actions}
            )
            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.acm_pca.list_permissions(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, certificate_authority_arn: Text, principal: Text, source_account: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Revokes permissions on a private CA granted to the AWS Certificate Manager (ACM) service principal
    (acm.amazonaws.com).  These permissions allow ACM to issue and renew ACM certificates that reside in the same
    AWS account as the CA. If you revoke these permissions, ACM will no longer renew the affected certificates
    automatically. Permissions can be granted with the CreatePermission action and listed with the ListPermissions
    action.   About Permissions    If the private CA and the certificates it issues reside in the same account, you
    can use CreatePermission to grant permissions for ACM to carry out automatic certificate renewals.   For
    automatic certificate renewal to succeed, the ACM service principal needs permissions to create, retrieve, and
    list certificates.   If the private CA and the ACM certificates reside in different accounts, then permissions
    cannot be used to enable automatic renewals. Instead, the ACM certificate owner must set up a resource-based
    policy to enable cross-account issuance and renewals. For more information, see Using a Resource Based Policy
    with ACM Private CA.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        certificate_authority_arn(Text): The Amazon Resource Number (ARN) of the private CA that issued the permissions. You can find the
            CA's ARN by calling the ListCertificateAuthorities action. This must have the following form:
            arn:aws:acm-pca:region:account:certificate-authority/12345678-1234-1234-1234-123456789012 .
        principal(Text): The AWS service or identity that will have its CA permissions revoked. At this time, the only
            valid service principal is acm.amazonaws.com.
        source_account(Text, optional): The AWS account that calls this action. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.acm_pca.permission.absent:
                - name: value
                - certificate_authority_arn: value
                - principal: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.acm_pca.permission.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.acm_pca.list_permissions(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.acm_pca.delete_permission(
                ctx,
                
                
                **{"CertificateAuthorityArn": certificate_authority_arn, "Principal": principal, "SourceAccount": source_account}
            )
            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.acm_pca.list_permissions(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

