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

hub.exec.boto3.client.logs.delete_query_definition
hub.exec.boto3.client.logs.describe_query_definitions
hub.exec.boto3.client.logs.put_query_definition
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, query_string: Text, query_definition_id: Text = None, log_group_names: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates or updates a query definition for CloudWatch Logs Insights. For more information, see Analyzing Log Data
    with CloudWatch Logs Insights. To update a query definition, specify its queryDefinitionId in your request. The
    values of name, queryString, and logGroupNames are changed to the values that you specify in your update
    operation. No current values are retained from the current query definition. For example, if you update a
    current query definition that includes log groups, and you don't specify the logGroupNames parameter in your
    update operation, the query definition changes to contain no log groups. You must have the
    logs:PutQueryDefinition permission to be able to perform this operation.

    Args:
        name(Text): A name for the query definition. If you are saving a lot of query definitions, we recommend that
            you name them so that you can easily find the ones you want by using the first part of the name
            as a filter in the queryDefinitionNamePrefix parameter of DescribeQueryDefinitions.
        query_definition_id(Text, optional): If you are updating a query definition, use this parameter to specify the ID of the query
            definition that you want to update. You can use DescribeQueryDefinitions to retrieve the IDs of
            your saved query definitions. If you are creating a query definition, do not specify this
            parameter. CloudWatch generates a unique ID for the new query definition and include it in the
            response to this operation. Defaults to None.
        log_group_names(List, optional): Use this parameter to include specific log groups as part of your query definition. If you are
            updating a query definition and you omit this parameter, then the updated definition will
            contain no log groups. Defaults to None.
        query_string(Text): The query string to use for this definition. For more information, see CloudWatch Logs Insights
            Query Syntax.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.logs.put_query_definition(
                ctx,
                
                
                **{"Name": name, "queryDefinitionId": query_definition_id, "logGroupNames": log_group_names, "queryString": query_string}
            )
            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.logs.describe_query_definitions(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, query_definition_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a saved CloudWatch Logs Insights query definition. A query definition contains details about a saved
    CloudWatch Logs Insights query. Each DeleteQueryDefinition operation can delete one query definition. You must
    have the logs:DeleteQueryDefinition permission to be able to perform this operation.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        query_definition_id(Text): The ID of the query definition that you want to delete. You can use DescribeQueryDefinitions to
            retrieve the IDs of your saved query definitions.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

