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

hub.exec.boto3.client.iam.create_policy
hub.exec.boto3.client.iam.delete_policy
hub.exec.boto3.client.iam.get_policy
hub.exec.boto3.client.iam.list_policies
hub.exec.boto3.client.iam.tag_policy
hub.exec.boto3.client.iam.untag_policy
resource = hub.tool.boto3.resource.create(ctx, "iam", "Policy", name)
hub.tool.boto3.resource.exec(resource, attach_group, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, attach_role, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, attach_user, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, create_version, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, detach_group, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, detach_role, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, detach_user, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, policy_name: Text, policy_document: Text, path: Text = None, description: Text = None, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a new managed policy for your account. This operation creates a policy version with a version identifier
    of v1 and sets v1 as the policy's default version. For more information about policy versions, see Versioning
    for managed policies in the IAM User Guide. As a best practice, you can validate your IAM policies. To learn
    more, see Validating IAM policies in the IAM User Guide. For more information about managed policies in general,
    see Managed policies and inline policies in the IAM User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        policy_name(Text): The friendly name of the policy. IAM user, group, role, and policy names must be unique within
            the account. Names are not distinguished by case. For example, you cannot create resources named
            both "MyResource" and "myresource".
        path(Text, optional): The path for the policy. For more information about paths, see IAM identifiers in the IAM User
            Guide. This parameter is optional. If it is not included, it defaults to a slash (/). This
            parameter allows (through its regex pattern) a string of characters consisting of either a
            forward slash (/) by itself or a string that must begin and end with forward slashes. In
            addition, it can contain any ASCII character from the ! (\u0021) through the DEL character
            (\u007F), including most punctuation characters, digits, and upper and lowercased letters. Defaults to None.
        policy_document(Text): The JSON policy document that you want to use as the content for the new policy. You must
            provide policies in JSON format in IAM. However, for CloudFormation templates formatted in YAML,
            you can provide the policy in JSON or YAML format. CloudFormation always converts a YAML policy
            to JSON format before submitting it to IAM. The maximum length of the policy document that you
            can pass in this operation, including whitespace, is listed below. To view the maximum character
            counts of a managed policy with no whitespaces, see IAM and STS character quotas. To learn more
            about JSON policy grammar, see Grammar of the IAM JSON policy language in the IAM User Guide.
            The regex pattern used to validate this parameter is a string of characters consisting of the
            following:   Any printable ASCII character ranging from the space character (\u0020) through the
            end of the ASCII character range   The printable characters in the Basic Latin and Latin-1
            Supplement character set (through \u00FF)   The special characters tab (\u0009), line feed
            (\u000A), and carriage return (\u000D).
        description(Text, optional): A friendly description of the policy. Typically used to store information about the permissions
            defined in the policy. For example, "Grants access to production DynamoDB tables." The policy
            description is immutable. After a value is assigned, it cannot be changed. Defaults to None.
        tags(List, optional): A list of tags that you want to attach to the new IAM customer managed policy. Each tag consists
            of a key name and an associated value. For more information about tagging, see Tagging IAM
            resources in the IAM User Guide.  If any one of the tags is invalid or if you exceed the allowed
            maximum number of tags, then the entire request fails and the resource is not created. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    resource = hub.tool.boto3.resource.create(ctx, "iam", "Policy", name)
    before = await hub.tool.boto3.resource.describe(resource)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iam.create_policy(
                ctx,
                
                
                **{"PolicyName": policy_name, "Path": path, "PolicyDocument": policy_document, "Description": description, "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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, policy_arn: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified managed policy. Before you can delete a managed policy, you must first detach the policy
    from all users, groups, and roles that it is attached to. In addition, you must delete all the policy's
    versions. The following steps describe the process for deleting a managed policy:   Detach the policy from all
    users, groups, and roles that the policy is attached to, using DetachUserPolicy, DetachGroupPolicy, or
    DetachRolePolicy. To list all the users, groups, and roles that a policy is attached to, use
    ListEntitiesForPolicy.   Delete all versions of the policy using DeletePolicyVersion. To list the policy's
    versions, use ListPolicyVersions. You cannot use DeletePolicyVersion to delete the version that is marked as the
    default version. You delete the policy's default version in the next step of the process.   Delete the policy
    (this automatically deletes the policy's default version) using this operation.   For information about managed
    policies, see Managed policies and inline policies in the IAM User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        policy_arn(Text): The Amazon Resource Name (ARN) of the IAM policy you want to delete. For more information about
            ARNs, see Amazon Resource Names (ARNs) in the Amazon Web Services General Reference.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    resource = hub.tool.boto3.resource.create(ctx, "iam", "Policy", name)

    before = await hub.tool.boto3.resource.describe(resource)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.iam.delete_policy(
                ctx,
                
                
                **{"PolicyArn": policy_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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

