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

hub.exec.boto3.client.workmail.delete_access_control_rule
hub.exec.boto3.client.workmail.list_access_control_rules
hub.exec.boto3.client.workmail.put_access_control_rule
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, effect: Text, description: Text, organization_id: Text, ip_ranges: List = None, not_ip_ranges: List = None, actions: List = None, not_actions: List = None, user_ids: List = None, not_user_ids: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Adds a new access control rule for the specified organization. The rule allows or denies access to the
    organization for the specified IPv4 addresses, access protocol actions, and user IDs. Adding a new rule with the
    same name as an existing rule replaces the older rule.

    Args:
        name(Text): The rule name.
        effect(Text): The rule effect.
        description(Text): The rule description.
        ip_ranges(List, optional): IPv4 CIDR ranges to include in the rule. Defaults to None.
        not_ip_ranges(List, optional): IPv4 CIDR ranges to exclude from the rule. Defaults to None.
        actions(List, optional): Access protocol actions to include in the rule. Valid values include ActiveSync, AutoDiscover,
            EWS, IMAP, SMTP, WindowsOutlook, and WebMail. Defaults to None.
        not_actions(List, optional): Access protocol actions to exclude from the rule. Valid values include ActiveSync, AutoDiscover,
            EWS, IMAP, SMTP, WindowsOutlook, and WebMail. Defaults to None.
        user_ids(List, optional): User IDs to include in the rule. Defaults to None.
        not_user_ids(List, optional): User IDs to exclude from the rule. Defaults to None.
        organization_id(Text): The identifier of the organization.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.workmail.access_control_rule.present:
                - name: value
                - effect: value
                - description: value
                - organization_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.workmail.access_control_rule.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.workmail.list_access_control_rules(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.workmail.put_access_control_rule(
                ctx,
                
                
                **{"Name": name, "Effect": effect, "Description": description, "IpRanges": ip_ranges, "NotIpRanges": not_ip_ranges, "Actions": actions, "NotActions": not_actions, "UserIds": user_ids, "NotUserIds": not_user_ids, "OrganizationId": organization_id}
            )
            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.workmail.list_access_control_rules(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, organization_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes an access control rule for the specified WorkMail organization.

    Args:
        name(Text): The name of the access control rule.
        organization_id(Text): The identifier for the organization.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

