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

hub.exec.boto3.client.iot.cancel_job
hub.exec.boto3.client.iot.create_job
hub.exec.boto3.client.iot.delete_job
hub.exec.boto3.client.iot.describe_job
hub.exec.boto3.client.iot.list_jobs
hub.exec.boto3.client.iot.update_job
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, job_id: Text, targets: List, document_source: Text = None, document: Text = None, description: Text = None, presigned_url_config: Dict = None, target_selection: Text = None, job_executions_rollout_config: Dict = None, abort_config: Dict = None, timeout_config: Dict = None, tags: List = None, namespace_id: Text = None, job_template_arn: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a job.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        job_id(Text): A job identifier which must be unique for your AWS account. We recommend using a UUID. Alpha-
            numeric characters, "-" and "_" are valid for use here.
        targets(List): A list of things and thing groups to which the job should be sent.
        document_source(Text, optional): An S3 link to the job document. Required if you don't specify a value for document.  If the job
            document resides in an S3 bucket, you must use a placeholder link when specifying the document.
            The placeholder link is of the following form:  ${aws:iot:s3-presigned-
            url:https://s3.amazonaws.com/bucket/key}  where bucket is your bucket name and key is the object
            in the bucket to which you are linking. Defaults to None.
        document(Text, optional): The job document. Required if you don't specify a value for documentSource. Defaults to None.
        description(Text, optional): A short text description of the job. Defaults to None.
        presigned_url_config(Dict, optional): Configuration information for pre-signed S3 URLs. Defaults to None.
        target_selection(Text, optional): Specifies whether the job will continue to run (CONTINUOUS), or will be complete after all those
            things specified as targets have completed the job (SNAPSHOT). If continuous, the job may also
            be run on a thing when a change is detected in a target. For example, a job will run on a thing
            when the thing is added to a target group, even after the job was completed by all things
            originally in the group. Defaults to None.
        job_executions_rollout_config(Dict, optional): Allows you to create a staged rollout of the job. Defaults to None.
        abort_config(Dict, optional): Allows you to create criteria to abort a job. Defaults to None.
        timeout_config(Dict, optional): Specifies the amount of time each device has to finish its execution of the job. The timer is
            started when the job execution status is set to IN_PROGRESS. If the job execution status is not
            set to another terminal state before the time expires, it will be automatically set to
            TIMED_OUT. Defaults to None.
        tags(List, optional): Metadata which can be used to manage the job. Defaults to None.
        namespace_id(Text, optional): The namespace used to indicate that a job is a customer-managed job. When you specify a value
            for this parameter, AWS IoT Core sends jobs notifications to MQTT topics that contain the value
            in the following format.  $aws/things/THING_NAME/jobs/JOB_ID/notify-namespace-NAMESPACE_ID/
            The namespaceId feature is in public preview. Defaults to None.
        job_template_arn(Text, optional): The ARN of the job template used to create the job. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iot.create_job(
                ctx,
                
                
                **{"jobId": job_id, "targets": targets, "documentSource": document_source, "document": document, "description": description, "presignedUrlConfig": presigned_url_config, "targetSelection": target_selection, "jobExecutionsRolloutConfig": job_executions_rollout_config, "abortConfig": abort_config, "timeoutConfig": timeout_config, "tags": tags, "namespaceId": namespace_id, "jobTemplateArn": job_template_arn}
            )
            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_job(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, job_id: Text, force: bool = None, namespace_id: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a job and its related job executions. Deleting a job may take time, depending on the number of job
    executions created for the job and various other factors. While the job is being deleted, the status of the job
    will be shown as "DELETION_IN_PROGRESS". Attempting to delete or cancel a job whose status is already
    "DELETION_IN_PROGRESS" will result in an error. Only 10 jobs may have status "DELETION_IN_PROGRESS" at the same
    time, or a LimitExceededException will occur.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        job_id(Text): The ID of the job to be deleted. After a job deletion is completed, you may reuse this jobId
            when you create a new job. However, this is not recommended, and you must ensure that your
            devices are not using the jobId to refer to the deleted job.
        force(bool, optional): (Optional) When true, you can delete a job which is "IN_PROGRESS". Otherwise, you can only
            delete a job which is in a terminal state ("COMPLETED" or "CANCELED") or an exception will
            occur. The default is false.  Deleting a job which is "IN_PROGRESS", will cause a device which
            is executing the job to be unable to access job information or update the job execution status.
            Use caution and ensure that each device executing a job which is deleted is able to recover to a
            valid state. Defaults to None.
        namespace_id(Text, optional): The namespace used to indicate that a job is a customer-managed job. When you specify a value
            for this parameter, AWS IoT Core sends jobs notifications to MQTT topics that contain the value
            in the following format.  $aws/things/THING_NAME/jobs/JOB_ID/notify-namespace-NAMESPACE_ID/
            The namespaceId feature is in public preview. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

