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

hub.exec.boto3.client.glue.create_table
hub.exec.boto3.client.glue.delete_table
hub.exec.boto3.client.glue.get_tables
hub.exec.boto3.client.glue.search_tables
hub.exec.boto3.client.glue.update_table
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, database_name: Text, table_input: Dict, catalog_id: Text = None, partition_indexes: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a new table definition in the 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 Table. If none is supplied, the Amazon Web
            Services account ID is used by default. Defaults to None.
        database_name(Text): The catalog database in which to create the new table. For Hive compatibility, this name is
            entirely lowercase.
        table_input(Dict): The TableInput object that defines the metadata table to create in the catalog.
        partition_indexes(List, optional): A list of partition indexes, PartitionIndex structures, to create in the table. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.glue.create_table(
                ctx,
                
                
                **{"CatalogId": catalog_id, "DatabaseName": database_name, "TableInput": table_input, "PartitionIndexes": partition_indexes}
            )
            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_tables(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, database_name: Text, catalog_id: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Removes a table definition from the Data Catalog.  After completing this operation, you no longer have access to
    the table versions and partitions that belong to the deleted table. 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 DeleteTable, use DeleteTableVersion or BatchDeleteTableVersion, and
    DeletePartition or BatchDeletePartition, to delete any resources that belong to the table.

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

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

