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

hub.exec.boto3.client.greengrassv2.cancel_deployment
hub.exec.boto3.client.greengrassv2.create_deployment
hub.exec.boto3.client.greengrassv2.get_deployment
hub.exec.boto3.client.greengrassv2.list_deployments
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, target_arn: Text, deployment_name: Text = None, components: Dict = None, iot_job_configuration: Dict = None, deployment_policies: Dict = None, tags: Dict = None, client_token: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a continuous deployment for a target, which is a Greengrass core device or group of core devices. When
    you add a new core device to a group of core devices that has a deployment, IoT Greengrass deploys that group's
    deployment to the new device. You can define one deployment for each target. When you create a new deployment
    for a target that has an existing deployment, you replace the previous deployment. IoT Greengrass applies the
    new deployment to the target devices. Every deployment has a revision number that indicates how many deployment
    revisions you define for a target. Use this operation to create a new revision of an existing deployment. This
    operation returns the revision number of the new deployment when you create it. For more information, see the
    Create deployments in the IoT Greengrass V2 Developer Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        target_arn(Text): The ARN of the target IoT thing or thing group.
        deployment_name(Text, optional): The name of the deployment. Defaults to None.
        components(Dict, optional): The components to deploy. This is a dictionary, where each key is the name of a component, and
            each key's value is the version and configuration to deploy for that component. Defaults to None.
        iot_job_configuration(Dict, optional): The job configuration for the deployment configuration. The job configuration specifies the
            rollout, timeout, and stop configurations for the deployment configuration. Defaults to None.
        deployment_policies(Dict, optional): The deployment policies for the deployment. These policies define how the deployment updates
            components and handles failure. Defaults to None.
        tags(Dict, optional): A list of key-value pairs that contain metadata for the resource. For more information, see Tag
            your resources in the IoT Greengrass V2 Developer Guide. Defaults to None.
        client_token(Text, optional): A unique, case-sensitive identifier that you can provide to ensure that the request is
            idempotent. Idempotency means that the request is successfully processed only once, even if you
            send the request multiple times. When a request succeeds, and you specify the same client token
            for subsequent successful requests, the IoT Greengrass V2 service returns the successful
            response that it caches from the previous request. IoT Greengrass V2 caches successful responses
            for idempotent requests for up to 8 hours. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.greengrassv2.create_deployment(
                ctx,
                
                
                **{"targetArn": target_arn, "deploymentName": deployment_name, "components": components, "iotJobConfiguration": iot_job_configuration, "deploymentPolicies": deployment_policies, "tags": tags, "clientToken": client_token}
            )
            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.greengrassv2.get_deployment(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, deployment_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Cancels a deployment. This operation cancels the deployment for devices that haven't yet received it. If a
    device already received the deployment, this operation doesn't change anything for that device.

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

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

