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

hub.exec.boto3.client.ec2.create_key_pair
hub.exec.boto3.client.ec2.delete_key_pair
hub.exec.boto3.client.ec2.describe_key_pairs
hub.exec.boto3.client.ec2.import_key_pair
resource = hub.tool.boto3.resource.create(ctx, "ec2", "KeyPair", 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, key_name: Text, key_type: Text = None, tag_specifications: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an ED25519 or 2048-bit RSA key pair with the specified name. Amazon EC2 stores the public key and
    displays the private key for you to save to a file. The private key is returned as an unencrypted PEM encoded
    PKCS#1 private key. If a key with the specified name already exists, Amazon EC2 returns an error. The key pair
    returned to you is available only in the Amazon Web Services Region in which you create it. If you prefer, you
    can create your own key pair using a third-party tool and upload it to any Region using ImportKeyPair. You can
    have up to 5,000 key pairs per Amazon Web Services Region. For more information, see Amazon EC2 key pairs in the
    Amazon Elastic Compute Cloud User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        key_name(Text): A unique name for the key pair. Constraints: Up to 255 ASCII characters.
        key_type(Text, optional): The type of key pair. Note that ED25519 keys are not supported for Windows instances, EC2
            Instance Connect, and EC2 Serial Console. Default: rsa. Defaults to None.
        tag_specifications(List, optional): The tags to apply to the new key pair. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.key_pair.present:
                - name: value
                - key_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.key_pair.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", "KeyPair", 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_key_pair(
                ctx,
                DryRun=ctx.test,
                
                **{"KeyName": key_name, "KeyType": key_type, "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, key_name: Text = None, key_pair_id: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified key pair, by removing the public key from Amazon EC2.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        key_name(Text, optional): The name of the key pair. Defaults to None.
        key_pair_id(Text, optional): The ID of the key pair. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.ec2.key_pair.absent:
                - name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.key_pair.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", "KeyPair", 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_key_pair(
                ctx,
                DryRun=ctx.test,
                
                **{"KeyName": key_name, "KeyPairId": key_pair_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

