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

hub.exec.boto3.client.ec2.create_tags
hub.exec.boto3.client.ec2.delete_tags
hub.exec.boto3.client.ec2.describe_tags
resource = hub.tool.boto3.resource.create(ctx, "ec2", "Tag", name)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, resources: List, tags: List)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Adds or overwrites only the specified tags for the specified Amazon EC2 resource or resources. When you specify
    an existing tag key, the value is overwritten with the new value. Each resource can have a maximum of 50 tags.
    Each tag consists of a key and optional value. Tag keys must be unique per resource. For more information about
    tags, see Tagging Your Resources in the Amazon Elastic Compute Cloud User Guide. For more information about
    creating IAM policies that control users' access to resources based on tags, see Supported Resource-Level
    Permissions for Amazon EC2 API Actions in the Amazon Elastic Compute Cloud User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        resources(List): The IDs of the resources, separated by spaces. Constraints: Up to 1000 resource IDs. We
            recommend breaking up this request into smaller batches.
        tags(List): The tags. The value parameter is required, but if you don't want the tag to have a value,
            specify the parameter with no value, and we set the value to an empty string.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.tag.present:
                - name: value
                - resources: value
                - tags: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.tag.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "ec2", "Tag", name)
    before = await hub.tool.boto3.resource.describe(resource)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.ec2.create_tags(
                ctx,
                DryRun=ctx.test,
                
                **{"Resources": resources, "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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, resources: List, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified set of tags from the specified set of resources. To list the current tags, use
    DescribeTags. For more information about tags, see Tagging Your Resources in the Amazon Elastic Compute Cloud
    User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        resources(List): The IDs of the resources, separated by spaces. Constraints: Up to 1000 resource IDs. We
            recommend breaking up this request into smaller batches.
        tags(List, optional): The tags to delete. Specify a tag key and an optional tag value to delete specific tags. If you
            specify a tag key without a tag value, we delete any tag with this key regardless of its value.
            If you specify a tag key with an empty string as the tag value, we delete the tag only if its
            value is an empty string. If you omit this parameter, we delete all user-defined tags for the
            specified resources. We do not delete Amazon Web Services-generated tags (tags that have the
            aws: prefix). Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.ec2.tag.absent:
                - name: value
                - resources: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.tag.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "ec2", "Tag", name)

    before = await hub.tool.boto3.resource.describe(resource)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.ec2.delete_tags(
                ctx,
                DryRun=ctx.test,
                
                **{"Resources": resources, "Tags": tags}
            )
            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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

