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

hub.exec.boto3.client.codeartifact.delete_repository_permissions_policy
hub.exec.boto3.client.codeartifact.get_repository_permissions_policy
hub.exec.boto3.client.codeartifact.put_repository_permissions_policy
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, domain: Text, repository: Text, policy_document: Text, domain_owner: Text = None, policy_revision: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Sets the resource policy on a repository that specifies permissions to access it.   When you call
    PutRepositoryPermissionsPolicy, the resource policy on the repository is ignored when evaluting permissions.
    This ensures that the owner of a repository cannot lock themselves out of the repository, which would prevent
    them from being able to update the resource policy.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        domain(Text):  The name of the domain containing the repository to set the resource policy on.
        domain_owner(Text, optional):  The 12-digit account number of the AWS account that owns the domain. It does not include dashes
            or spaces. Defaults to None.
        repository(Text):  The name of the repository to set the resource policy on.
        policy_revision(Text, optional):  Sets the revision of the resource policy that specifies permissions to access the repository.
            This revision is used for optimistic locking, which prevents others from overwriting your
            changes to the repository's resource policy. Defaults to None.
        policy_document(Text):  A valid displayable JSON Aspen policy string to be set as the access control resource policy on
            the provided repository.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.codeartifact.repository_permissions_policy.present:
                - name: value
                - domain: value
                - repository: value
                - policy_document: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codeartifact.repository_permissions_policy.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.codeartifact.get_repository_permissions_policy(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.codeartifact.put_repository_permissions_policy(
                ctx,
                
                
                **{"domain": domain, "domainOwner": domain_owner, "repository": repository, "policyRevision": policy_revision, "policyDocument": policy_document}
            )
            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.codeartifact.get_repository_permissions_policy(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, domain: Text, repository: Text, domain_owner: Text = None, policy_revision: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Deletes the resource policy that is set on a repository. After a resource policy is deleted, the permissions
    allowed and denied by the deleted policy are removed. The effect of deleting a resource policy might not be
    immediate.    Use DeleteRepositoryPermissionsPolicy with caution. After a policy is deleted, AWS users, roles,
    and accounts lose permissions to perform the repository actions granted by the deleted policy.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        domain(Text):  The name of the domain that contains the repository associated with the resource policy to be
            deleted.
        domain_owner(Text, optional):  The 12-digit account number of the AWS account that owns the domain. It does not include dashes
            or spaces. Defaults to None.
        repository(Text):  The name of the repository that is associated with the resource policy to be deleted.
        policy_revision(Text, optional):  The revision of the repository's resource policy to be deleted. This revision is used for
            optimistic locking, which prevents others from accidentally overwriting your changes to the
            repository's resource policy. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.codeartifact.repository_permissions_policy.absent:
                - name: value
                - domain: value
                - repository: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codeartifact.repository_permissions_policy.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.codeartifact.get_repository_permissions_policy(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.codeartifact.delete_repository_permissions_policy(
                ctx,
                
                
                **{"domain": domain, "domainOwner": domain_owner, "repository": repository, "policyRevision": policy_revision}
            )
            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.codeartifact.get_repository_permissions_policy(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

