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

hub.exec.boto3.client.wafv2.delete_permission_policy
hub.exec.boto3.client.wafv2.get_permission_policy
hub.exec.boto3.client.wafv2.put_permission_policy
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, resource_arn: Text, policy: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Attaches an IAM policy to the specified resource. Use this to share a rule group across accounts. You must be
    the owner of the rule group to perform this operation. This action is subject to the following restrictions:
    You can attach only one policy with each PutPermissionPolicy request.   The ARN in the request must be a valid
    WAF RuleGroup ARN and the rule group must exist in the same Region.   The user making the request must be the
    owner of the rule group.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        resource_arn(Text): The Amazon Resource Name (ARN) of the RuleGroup to which you want to attach the policy.
        policy(Text): The policy to attach to the specified rule group.  The policy specifications must conform to the
            following:   The policy must be composed using IAM Policy version 2012-10-17 or version
            2015-01-01.   The policy must include specifications for Effect, Action, and Principal.
            Effect must specify Allow.    Action must specify wafv2:CreateWebACL, wafv2:UpdateWebACL, and
            wafv2:PutFirewallManagerRuleGroups. WAF rejects any extra actions or wildcard actions in the
            policy.   The policy must not include a Resource parameter.   For more information, see IAM
            Policies.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.wafv2.permission_policy.present:
                - name: value
                - resource_arn: value
                - policy: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.wafv2.permission_policy.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.wafv2.get_permission_policy(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.wafv2.put_permission_policy(
                ctx,
                
                
                **{"ResourceArn": resource_arn, "Policy": policy}
            )
            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.wafv2.get_permission_policy(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, resource_arn: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Permanently deletes an IAM policy from the specified rule group. You must be the owner of the rule group to
    perform this operation.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        resource_arn(Text): The Amazon Resource Name (ARN) of the rule group from which you want to delete the policy. You
            must be the owner of the rule group to perform this operation.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

