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

hub.exec.boto3.client.iam.create_policy_version
hub.exec.boto3.client.iam.delete_policy_version
hub.exec.boto3.client.iam.get_policy_version
hub.exec.boto3.client.iam.list_policy_versions
resource = hub.tool.boto3.resource.create(ctx, "iam", "PolicyVersion", name)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, set_as_default, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, policy_arn: Text, policy_document: Text, set_as_default: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a new version of the specified managed policy. To update a managed policy, you create a new policy
    version. A managed policy can have up to five versions. If the policy has five versions, you must delete an
    existing version using DeletePolicyVersion before you create a new version. Optionally, you can set the new
    version as the policy's default version. The default version is the version that is in effect for the IAM users,
    groups, and roles to which the policy is attached. For more information about managed policy versions, see
    Versioning for managed 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 to which you want to add a new version. For
            more information about ARNs, see Amazon Resource Names (ARNs) in the Amazon Web Services General
            Reference.
        policy_document(Text): The JSON policy document that you want to use as the content for this new version of the 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. 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).
        set_as_default(bool, optional): Specifies whether to set this version as the policy's default version. When this parameter is
            true, the new policy version becomes the operative version. That is, it becomes the version that
            is in effect for the IAM users, groups, and roles that the policy is attached to. For more
            information about managed policy versions, see Versioning for managed policies in the IAM User
            Guide. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.iam.policy_version.present:
                - name: value
                - policy_arn: value
                - policy_document: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iam.policy_version.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", "PolicyVersion", 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_version(
                ctx,
                
                
                **{"PolicyArn": policy_arn, "PolicyDocument": policy_document, "SetAsDefault": set_as_default}
            )
            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, version_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified version from the specified managed policy. You cannot delete the default version from a
    policy using this operation. To delete the default version from a policy, use DeletePolicy. To find out which
    version of a policy is marked as the default version, use ListPolicyVersions. For information about versions for
    managed policies, see Versioning for managed 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 from which you want to delete a version. For
            more information about ARNs, see Amazon Resource Names (ARNs) in the Amazon Web Services General
            Reference.
        version_id(Text): The policy version to delete. This parameter allows (through its regex pattern) a string of
            characters that consists of the lowercase letter 'v' followed by one or two digits, and
            optionally followed by a period '.' and a string of letters and digits. For more information
            about managed policy versions, see Versioning for managed policies in the IAM User Guide.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.iam.policy_version.absent:
                - name: value
                - policy_arn: value
                - version_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iam.policy_version.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", "PolicyVersion", 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_version(
                ctx,
                
                
                **{"PolicyArn": policy_arn, "VersionId": version_id}
            )
            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

