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

hub.exec.boto3.client.elasticache.create_global_replication_group
hub.exec.boto3.client.elasticache.delete_global_replication_group
hub.exec.boto3.client.elasticache.describe_global_replication_groups
hub.exec.boto3.client.elasticache.disassociate_global_replication_group
hub.exec.boto3.client.elasticache.failover_global_replication_group
hub.exec.boto3.client.elasticache.modify_global_replication_group
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, global_replication_group_id_suffix: Text, primary_replication_group_id: Text, global_replication_group_description: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Global Datastore for Redis offers fully managed, fast, reliable and secure cross-region replication. Using
    Global Datastore for Redis, you can create cross-region read replica clusters for ElastiCache for Redis to
    enable low-latency reads and disaster recovery across regions. For more information, see Replication Across
    Regions Using Global Datastore.    The GlobalReplicationGroupIdSuffix is the name of the Global datastore.   The
    PrimaryReplicationGroupId represents the name of the primary cluster that accepts writes and will replicate
    updates to the secondary cluster.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        global_replication_group_id_suffix(Text): The suffix name of a Global datastore. Amazon ElastiCache automatically applies a prefix to the
            Global datastore ID when it is created. Each Amazon Region has its own prefix. For instance, a
            Global datastore ID created in the US-West-1 region will begin with "dsdfu" along with the
            suffix name you provide. The suffix, combined with the auto-generated prefix, guarantees
            uniqueness of the Global datastore name across multiple regions.  For a full list of Amazon
            Regions and their respective Global datastore iD prefixes, see Using the Amazon CLI with Global
            datastores .
        global_replication_group_description(Text, optional): Provides details of the Global datastore. Defaults to None.
        primary_replication_group_id(Text): The name of the primary cluster that accepts writes and will replicate updates to the secondary
            cluster.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticache.create_global_replication_group(
                ctx,
                
                
                **{"GlobalReplicationGroupIdSuffix": global_replication_group_id_suffix, "GlobalReplicationGroupDescription": global_replication_group_description, "PrimaryReplicationGroupId": primary_replication_group_id}
            )
            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.elasticache.describe_global_replication_groups(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, global_replication_group_id: Text, retain_primary_replication_group: bool)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deleting a Global datastore is a two-step process:    First, you must DisassociateGlobalReplicationGroup to
    remove the secondary clusters in the Global datastore.   Once the Global datastore contains only the primary
    cluster, you can use the DeleteGlobalReplicationGroup API to delete the Global datastore while retainining the
    primary cluster using RetainPrimaryReplicationGroup=true.   Since the Global Datastore has only a primary
    cluster, you can delete the Global Datastore while retaining the primary by setting
    RetainPrimaryReplicationGroup=true. The primary cluster is never deleted when deleting a Global Datastore. It
    can only be deleted when it no longer is associated with any Global Datastore. When you receive a successful
    response from this operation, Amazon ElastiCache immediately begins deleting the selected resources; you cannot
    cancel or revert this operation.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        global_replication_group_id(Text): The name of the Global datastore.
        retain_primary_replication_group(bool): The primary replication group is retained as a standalone replication group.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticache.delete_global_replication_group(
                ctx,
                
                
                **{"GlobalReplicationGroupId": global_replication_group_id, "RetainPrimaryReplicationGroup": retain_primary_replication_group}
            )
            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.elasticache.describe_global_replication_groups(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

