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

hub.exec.boto3.client.opsworkscm.create_backup
hub.exec.boto3.client.opsworkscm.delete_backup
hub.exec.boto3.client.opsworkscm.describe_backups
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, server_name: Text, description: Text = None, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Creates an application-level backup of a server. While the server is in the BACKING_UP state, the server cannot
    be changed, and no additional backup can be created.   Backups can be created for servers in RUNNING, HEALTHY,
    and UNHEALTHY states. By default, you can create a maximum of 50 manual backups.   This operation is
    asynchronous.   A LimitExceededException is thrown when the maximum number of manual backups is reached. An
    InvalidStateException is thrown when the server is not in any of the following states: RUNNING, HEALTHY, or
    UNHEALTHY. A ResourceNotFoundException is thrown when the server is not found. A ValidationException is thrown
    when parameters of the request are not valid.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        server_name(Text): The name of the server that you want to back up.
        description(Text, optional):  A user-defined description of the backup. Defaults to None.
        tags(List, optional): A map that contains tag keys and tag values to attach to an AWS OpsWorks-CM server backup.   The
            key cannot be empty.   The key can be a maximum of 127 characters, and can contain only Unicode
            letters, numbers, or separators, or the following special characters: + - = . _ : /    The value
            can be a maximum 255 characters, and contain only Unicode letters, numbers, or separators, or
            the following special characters: + - = . _ : /    Leading and trailing white spaces are trimmed
            from both the key and value.   A maximum of 50 user-applied tags is allowed for tag-supported
            AWS OpsWorks-CM resources. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.opsworkscm.backup.present:
                - name: value
                - server_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.opsworkscm.backup.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.opsworkscm.describe_backups(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.opsworkscm.create_backup(
                ctx,
                
                
                **{"ServerName": server_name, "Description": description, "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.exec.boto3.client.opsworkscm.describe_backups(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, backup_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Deletes a backup. You can delete both manual and automated backups. This operation is asynchronous.   An
    InvalidStateException is thrown when a backup deletion is already in progress. A ResourceNotFoundException is
    thrown when the backup does not exist. A ValidationException is thrown when parameters of the request are not
    valid.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        backup_id(Text): The ID of the backup to delete. Run the DescribeBackups command to get a list of backup IDs.
            Backup IDs are in the format ServerName-yyyyMMddHHmmssSSS.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

