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

hub.exec.boto3.client.iot.attach_policy
hub.exec.boto3.client.iot.create_policy
hub.exec.boto3.client.iot.delete_policy
hub.exec.boto3.client.iot.detach_policy
hub.exec.boto3.client.iot.get_policy
hub.exec.boto3.client.iot.list_policies
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, policy_name: Text, policy_document: Text, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an AWS IoT policy. The created policy is the default version for the policy. This operation creates a
    policy version with a version identifier of 1 and sets 1 as the policy's default version.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        policy_name(Text): The policy name.
        policy_document(Text): The JSON document that describes the policy. policyDocument must have a minimum length of 1,
            with a maximum length of 2048, excluding whitespace.
        tags(List, optional): Metadata which can be used to manage the policy.  For URI Request parameters use format:
            ...key1=value1&key2=value2... For the CLI command-line parameter use format: &&tags
            "key1=value1&key2=value2..." For the cli-input-json file use format: "tags":
            "key1=value1&key2=value2...". Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.iot.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.iot.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.iot.get_policy(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iot.create_policy(
                ctx,
                
                
                **{"policyName": policy_name, "policyDocument": policy_document, "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.exec.boto3.client.iot.get_policy(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, policy_name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified policy. A policy cannot be deleted if it has non-default versions or it is attached to any
    certificate. To delete a policy, use the DeletePolicyVersion API to delete all non-default versions of the
    policy; use the DetachPrincipalPolicy API to detach the policy from any certificate; and then use the
    DeletePolicy API to delete the policy. When a policy is deleted using DeletePolicy, its default version is
    deleted with it.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        policy_name(Text): The name of the policy to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.iot.policy.absent:
                - name: value
                - policy_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iot.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.iot.get_policy(name)

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

