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

hub.exec.boto3.client.ecs.deregister_container_instance
hub.exec.boto3.client.ecs.describe_container_instances
hub.exec.boto3.client.ecs.list_container_instances
hub.exec.boto3.client.ecs.register_container_instance
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, cluster: Text = None, instance_identity_document: Text = None, instance_identity_document_signature: Text = None, total_resources: List = None, version_info: Dict = None, container_instance_arn: Text = None, attributes: List = None, platform_devices: List = None, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     This action is only used by the Amazon ECS agent, and it is not intended for use outside of the agent.
    Registers an EC2 instance into the specified cluster. This instance becomes available to place containers on.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        cluster(Text, optional): The short name or full Amazon Resource Name (ARN) of the cluster with which to register your
            container instance. If you do not specify a cluster, the default cluster is assumed. Defaults to None.
        instance_identity_document(Text, optional): The instance identity document for the EC2 instance to register. This document can be found by
            running the following command from the instance: curl
            http://169.254.169.254/latest/dynamic/instance-identity/document/. Defaults to None.
        instance_identity_document_signature(Text, optional): The instance identity document signature for the EC2 instance to register. This signature can be
            found by running the following command from the instance: curl
            http://169.254.169.254/latest/dynamic/instance-identity/signature/. Defaults to None.
        total_resources(List, optional): The resources available on the instance. Defaults to None.
        version_info(Dict, optional): The version information for the Amazon ECS container agent and Docker daemon running on the
            container instance. Defaults to None.
        container_instance_arn(Text, optional): The ARN of the container instance (if it was previously registered). Defaults to None.
        attributes(List, optional): The container instance attributes that this container instance supports. Defaults to None.
        platform_devices(List, optional): The devices that are available on the container instance. The only supported device type is a
            GPU. Defaults to None.
        tags(List, optional): The metadata that you apply to the container instance to help you categorize and organize them.
            Each tag consists of a key and an optional value, both of which you define. The following basic
            restrictions apply to tags:   Maximum number of tags per resource - 50   For each resource, each
            tag key must be unique, and each tag key can have only one value.   Maximum key length - 128
            Unicode characters in UTF-8   Maximum value length - 256 Unicode characters in UTF-8   If your
            tagging schema is used across multiple services and resources, remember that other services may
            have restrictions on allowed characters. Generally allowed characters are: letters, numbers, and
            spaces representable in UTF-8, and the following characters: + - = . _ : / @.   Tag keys and
            values are case-sensitive.   Do not use aws:, AWS:, or any upper or lowercase combination of
            such as a prefix for either keys or values as it is reserved for Amazon Web Services use. You
            cannot edit or delete tag keys or values with this prefix. Tags with this prefix do not count
            against your tags per resource limit. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.ecs.register_container_instance(
                ctx,
                
                
                **{"cluster": cluster, "instanceIdentityDocument": instance_identity_document, "instanceIdentityDocumentSignature": instance_identity_document_signature, "totalResources": total_resources, "versionInfo": version_info, "containerInstanceArn": container_instance_arn, "attributes": attributes, "platformDevices": platform_devices, "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.ecs.describe_container_instances(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, container_instance: Text, cluster: Text = None, force: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deregisters an Amazon ECS container instance from the specified cluster. This instance is no longer available to
    run tasks. If you intend to use the container instance for some other purpose after deregistration, you should
    stop all of the tasks running on the container instance before deregistration. That prevents any orphaned tasks
    from consuming resources. Deregistering a container instance removes the instance from a cluster, but it does
    not terminate the EC2 instance. If you are finished using the instance, be sure to terminate it in the Amazon
    EC2 console to stop billing.  If you terminate a running container instance, Amazon ECS automatically
    deregisters the instance from your cluster (stopped container instances or instances with disconnected agents
    are not automatically deregistered when terminated).

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        cluster(Text, optional): The short name or full Amazon Resource Name (ARN) of the cluster that hosts the container
            instance to deregister. If you do not specify a cluster, the default cluster is assumed. Defaults to None.
        container_instance(Text): The container instance ID or full ARN of the container instance to deregister. The ARN contains
            the arn:aws:ecs namespace, followed by the Region of the container instance, the Amazon Web
            Services account ID of the container instance owner, the container-instance namespace, and then
            the container instance ID. For example, arn:aws:ecs:region:aws_account_id:container-
            instance/container_instance_ID.
        force(bool, optional): Forces the deregistration of the container instance. If you have tasks running on the container
            instance when you deregister it with the force option, these tasks remain running until you
            terminate the instance or the tasks stop through some other means, but they are orphaned (no
            longer monitored or accounted for by Amazon ECS). If an orphaned task on your container instance
            is part of an Amazon ECS service, then the service scheduler starts another copy of that task,
            on a different container instance if possible.  Any containers in orphaned service tasks that
            are registered with a Classic Load Balancer or an Application Load Balancer target group are
            deregistered. They begin connection draining according to the settings on the load balancer or
            target group. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.ecs.deregister_container_instance(
                ctx,
                
                
                **{"cluster": cluster, "containerInstance": container_instance, "force": force}
            )
            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.ecs.describe_container_instances(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

