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

hub.exec.boto3.client.cloudhsmv2.create_cluster
hub.exec.boto3.client.cloudhsmv2.delete_cluster
hub.exec.boto3.client.cloudhsmv2.describe_clusters
hub.exec.boto3.client.cloudhsmv2.initialize_cluster
hub.exec.boto3.client.cloudhsmv2.modify_cluster
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, hsm_type: Text, subnet_ids: List, backup_retention_policy: Dict = None, source_backup_id: Text = None, tag_list: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a new AWS CloudHSM cluster.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        backup_retention_policy(Dict, optional): A policy that defines how the service retains backups. Defaults to None.
        hsm_type(Text): The type of HSM to use in the cluster. Currently the only allowed value is hsm1.medium.
        source_backup_id(Text, optional): The identifier (ID) of the cluster backup to restore. Use this value to restore the cluster from
            a backup instead of creating a new cluster. To find the backup ID, use DescribeBackups. Defaults to None.
        subnet_ids(List): The identifiers (IDs) of the subnets where you are creating the cluster. You must specify at
            least one subnet. If you specify multiple subnets, they must meet the following criteria:   All
            subnets must be in the same virtual private cloud (VPC).   You can specify only one subnet per
            Availability Zone.
        tag_list(List, optional): Tags to apply to the CloudHSM cluster during creation. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.cloudhsmv2.create_cluster(
                ctx,
                
                
                **{"BackupRetentionPolicy": backup_retention_policy, "HsmType": hsm_type, "SourceBackupId": source_backup_id, "SubnetIds": subnet_ids, "TagList": tag_list}
            )
            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.cloudhsmv2.describe_clusters(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, cluster_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified AWS CloudHSM cluster. Before you can delete a cluster, you must delete all HSMs in the
    cluster. To see if the cluster contains any HSMs, use DescribeClusters. To delete an HSM, use DeleteHsm.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        cluster_id(Text): The identifier (ID) of the cluster that you are deleting. To find the cluster ID, use
            DescribeClusters.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

