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

hub.exec.boto3.client.ec2.copy_image
hub.exec.boto3.client.ec2.create_image
hub.exec.boto3.client.ec2.deregister_image
hub.exec.boto3.client.ec2.describe_images
hub.exec.boto3.client.ec2.export_image
hub.exec.boto3.client.ec2.import_image
hub.exec.boto3.client.ec2.register_image
resource = hub.tool.boto3.resource.create(ctx, "ec2", "Image", name)
hub.tool.boto3.resource.exec(resource, create_tags, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, deregister, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, describe_attribute, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, modify_attribute, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, reset_attribute, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, instance_id: Text, block_device_mappings: List = None, description: Text = None, no_reboot: bool = None, tag_specifications: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an Amazon EBS-backed AMI from an Amazon EBS-backed instance that is either running or stopped. If you
    customized your instance with instance store volumes or EBS volumes in addition to the root device volume, the
    new AMI contains block device mapping information for those volumes. When you launch an instance from this new
    AMI, the instance automatically launches with those additional volumes. For more information, see Creating
    Amazon EBS-Backed Linux AMIs in the Amazon Elastic Compute Cloud User Guide.

    Args:
        name(Text): A name for the new image. Constraints: 3-128 alphanumeric characters, parentheses (()), square
            brackets ([]), spaces ( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs
            (@), or underscores(_).
        block_device_mappings(List, optional): The block device mappings. This parameter cannot be used to modify the encryption status of
            existing volumes or snapshots. To create an AMI with encrypted snapshots, use the CopyImage
            action. Defaults to None.
        description(Text, optional): A description for the new image. Defaults to None.
        instance_id(Text): The ID of the instance.
        no_reboot(bool, optional): By default, Amazon EC2 attempts to shut down and reboot the instance before creating the image.
            If the No Reboot option is set, Amazon EC2 doesn't shut down the instance before creating the
            image. When this option is used, file system integrity on the created image can't be guaranteed. Defaults to None.
        tag_specifications(List, optional): The tags to apply to the AMI and snapshots on creation. You can tag the AMI, the snapshots, or
            both.   To tag the AMI, the value for ResourceType must be image.   To tag the snapshots that
            are created of the root volume and of other EBS volumes that are attached to the instance, the
            value for ResourceType must be snapshot. The same tag is applied to all of the snapshots that
            are created.   If you specify other values for ResourceType, the request fails. To tag an AMI or
            snapshot after it has been created, see CreateTags. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.image.present:
                - name: value
                - instance_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.image.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", "Image", 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_image(
                ctx,
                DryRun=ctx.test,
                
                **{"Name": name, "BlockDeviceMappings": block_device_mappings, "Description": description, "InstanceId": instance_id, "NoReboot": no_reboot, "TagSpecifications": tag_specifications}
            )
            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, image_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deregisters the specified AMI. After you deregister an AMI, it can't be used to launch new instances; however,
    it doesn't affect any instances that you've already launched from the AMI. You'll continue to incur usage costs
    for those instances until you terminate them. When you deregister an Amazon EBS-backed AMI, it doesn't affect
    the snapshot that was created for the root volume of the instance during the AMI creation process. When you
    deregister an instance store-backed AMI, it doesn't affect the files that you uploaded to Amazon S3 when you
    created the AMI.

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

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.ec2.image.absent:
                - name: value
                - image_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.image.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", "Image", 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.deregister_image(
                ctx,
                DryRun=ctx.test,
                
                **{"ImageId": image_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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

