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

hub.exec.boto3.client.iot.create_thing
hub.exec.boto3.client.iot.delete_thing
hub.exec.boto3.client.iot.describe_thing
hub.exec.boto3.client.iot.list_things
hub.exec.boto3.client.iot.register_thing
hub.exec.boto3.client.iot.update_thing
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, thing_name: Text, thing_type_name: Text = None, attribute_payload: Dict = None, billing_group_name: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a thing record in the registry. If this call is made multiple times using the same thing name and
    configuration, the call will succeed. If this call is made with the same thing name but different configuration
    a ResourceAlreadyExistsException is thrown.  This is a control plane operation. See Authorization for
    information about authorizing control plane actions.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        thing_name(Text): The name of the thing to create. You can't change a thing's name after you create it. To change
            a thing's name, you must create a new thing, give it the new name, and then delete the old
            thing.
        thing_type_name(Text, optional): The name of the thing type associated with the new thing. Defaults to None.
        attribute_payload(Dict, optional): The attribute payload, which consists of up to three name/value pairs in a JSON document. For
            example:  {\"attributes\":{\"string1\":\"string2\"}}. Defaults to None.
        billing_group_name(Text, optional): The name of the billing group the thing will be added to. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.iot.thing.present:
                - name: value
                - thing_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iot.thing.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.describe_thing(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iot.create_thing(
                ctx,
                
                
                **{"thingName": thing_name, "thingTypeName": thing_type_name, "attributePayload": attribute_payload, "billingGroupName": billing_group_name}
            )
            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.describe_thing(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, thing_name: Text, expected_version: int = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified thing. Returns successfully with no error if the deletion is successful or you specify a
    thing that doesn't exist.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        thing_name(Text): The name of the thing to delete.
        expected_version(int, optional): The expected version of the thing record in the registry. If the version of the record in the
            registry does not match the expected version specified in the request, the DeleteThing request
            is rejected with a VersionConflictException. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

