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

hub.exec.boto3.client.guardduty.create_filter
hub.exec.boto3.client.guardduty.delete_filter
hub.exec.boto3.client.guardduty.get_filter
hub.exec.boto3.client.guardduty.list_filters
hub.exec.boto3.client.guardduty.update_filter
"""



from typing import *
import dict_tools.differ as differ


__virtualname__ = "filter"
async def present(hub, ctx, name: Text, detector_id: Text, finding_criteria: Dict, description: Text = None, action: Text = None, rank: int = None, tags: Dict = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a filter using the specified finding criteria.

    Args:
        name(Text): The name of the filter. Minimum length of 3. Maximum length of 64. Valid characters include
            alphanumeric characters, dot (.), underscore (_), and dash (-). Spaces are not allowed.
        detector_id(Text): The ID of the detector belonging to the GuardDuty account that you want to create a filter for.
        description(Text, optional): The description of the filter. Defaults to None.
        action(Text, optional): Specifies the action that is to be applied to the findings that match the filter. Defaults to None.
        rank(int, optional): Specifies the position of the filter in the list of current filters. Also specifies the order in
            which this filter is applied to the findings. Defaults to None.
        finding_criteria(Dict): Represents the criteria to be used in the filter for querying findings. You can only use the
            following attributes to query findings:   accountId   region   confidence   id
            resource.accessKeyDetails.accessKeyId   resource.accessKeyDetails.principalId
            resource.accessKeyDetails.userName   resource.accessKeyDetails.userType
            resource.instanceDetails.iamInstanceProfile.id   resource.instanceDetails.imageId
            resource.instanceDetails.instanceId   resource.instanceDetails.outpostArn
            resource.instanceDetails.networkInterfaces.ipv6Addresses
            resource.instanceDetails.networkInterfaces.privateIpAddresses.privateIpAddress
            resource.instanceDetails.networkInterfaces.publicDnsName
            resource.instanceDetails.networkInterfaces.publicIp
            resource.instanceDetails.networkInterfaces.securityGroups.groupId
            resource.instanceDetails.networkInterfaces.securityGroups.groupName
            resource.instanceDetails.networkInterfaces.subnetId
            resource.instanceDetails.networkInterfaces.vpcId   resource.instanceDetails.tags.key
            resource.instanceDetails.tags.value   resource.resourceType   service.action.actionType
            service.action.awsApiCallAction.api   service.action.awsApiCallAction.callerType
            service.action.awsApiCallAction.errorCode
            service.action.awsApiCallAction.remoteIpDetails.city.cityName
            service.action.awsApiCallAction.remoteIpDetails.country.countryName
            service.action.awsApiCallAction.remoteIpDetails.ipAddressV4
            service.action.awsApiCallAction.remoteIpDetails.organization.asn
            service.action.awsApiCallAction.remoteIpDetails.organization.asnOrg
            service.action.awsApiCallAction.serviceName   service.action.dnsRequestAction.domain
            service.action.networkConnectionAction.blocked
            service.action.networkConnectionAction.connectionDirection
            service.action.networkConnectionAction.localPortDetails.port
            service.action.networkConnectionAction.protocol
            service.action.networkConnectionAction.localIpDetails.ipAddressV4
            service.action.networkConnectionAction.remoteIpDetails.city.cityName
            service.action.networkConnectionAction.remoteIpDetails.country.countryName
            service.action.networkConnectionAction.remoteIpDetails.ipAddressV4
            service.action.networkConnectionAction.remoteIpDetails.organization.asn
            service.action.networkConnectionAction.remoteIpDetails.organization.asnOrg
            service.action.networkConnectionAction.remotePortDetails.port
            service.additionalInfo.threatListName   service.archived When this attribute is set to TRUE,
            only archived findings are listed. When it's set to FALSE, only unarchived findings are listed.
            When this attribute is not set, all existing findings are listed.   service.resourceRole
            severity   type   updatedAt Type: ISO 8601 string format: YYYY-MM-DDTHH:MM:SS.SSSZ or YYYY-MM-
            DDTHH:MM:SSZ depending on whether the value contains milliseconds.
        tags(Dict, optional): The tags to be added to a new filter resource. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.guardduty.filter_.present:
                - name: value
                - detector_id: value
                - finding_criteria: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.guardduty.filter.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.guardduty.get_filter(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.guardduty.create_filter(
                ctx,
                
                ClientToken=name,
                **{"Name": name, "DetectorId": detector_id, "Description": description, "Action": action, "Rank": rank, "FindingCriteria": finding_criteria, "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.guardduty.get_filter(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, detector_id: Text, filter_name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the filter specified by the filter name.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        detector_id(Text): The unique ID of the detector that the filter is associated with.
        filter_name(Text): The name of the filter that you want to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.guardduty.filter_.absent:
                - name: value
                - detector_id: value
                - filter_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.guardduty.filter.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.guardduty.get_filter(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.guardduty.delete_filter(
                ctx,
                
                
                **{"DetectorId": detector_id, "FilterName": filter_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.guardduty.get_filter(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

