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

hub.exec.boto3.client.glue.create_database
hub.exec.boto3.client.glue.delete_database
hub.exec.boto3.client.glue.get_databases
hub.exec.boto3.client.glue.update_database
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, database_input: Dict, catalog_id: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a new database in a Data Catalog.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        catalog_id(Text, optional): The ID of the Data Catalog in which to create the database. If none is provided, the Amazon Web
            Services account ID is used by default. Defaults to None.
        database_input(Dict): The metadata for the database.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.glue.create_database(
                ctx,
                
                
                **{"CatalogId": catalog_id, "DatabaseInput": database_input}
            )
            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.glue.get_databases(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, catalog_id: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Removes a specified database from a Data Catalog.  After completing this operation, you no longer have access to
    the tables (and all table versions and partitions that might belong to the tables) and the user-defined
    functions in the deleted database. Glue deletes these "orphaned" resources asynchronously in a timely manner, at
    the discretion of the service. To ensure the immediate deletion of all related resources, before calling
    DeleteDatabase, use DeleteTableVersion or BatchDeleteTableVersion, DeletePartition or BatchDeletePartition,
    DeleteUserDefinedFunction, and DeleteTable or BatchDeleteTable, to delete any resources that belong to the
    database.

    Args:
        name(Text): The name of the database to delete. For Hive compatibility, this must be all lowercase.
        catalog_id(Text, optional): The ID of the Data Catalog in which the database resides. If none is provided, the Amazon Web
            Services account ID is used by default. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

