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

hub.exec.boto3.client.securityhub.create_members
hub.exec.boto3.client.securityhub.delete_members
hub.exec.boto3.client.securityhub.disassociate_members
hub.exec.boto3.client.securityhub.get_members
hub.exec.boto3.client.securityhub.invite_members
hub.exec.boto3.client.securityhub.list_members
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, account_details: List)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a member association in Security Hub between the specified accounts and the account used to make the
    request, which is the administrator account. If you are integrated with Organizations, then the administrator
    account is designated by the organization management account.  CreateMembers is always used to add accounts that
    are not organization members. For accounts that are managed using Organizations, CreateMembers is only used in
    the following cases:   Security Hub is not configured to automatically add new organization accounts.   The
    account was disassociated or deleted in Security Hub.   This action can only be used by an account that has
    Security Hub enabled. To enable Security Hub, you can use the EnableSecurityHub operation. For accounts that are
    not organization members, you create the account association and then send an invitation to the member account.
    To send the invitation, you use the InviteMembers operation. If the account owner accepts the invitation, the
    account becomes a member account in Security Hub. Accounts that are managed using Organizations do not receive
    an invitation. They automatically become a member account in Security Hub, and Security Hub is automatically
    enabled for those accounts. Note that Security Hub cannot be enabled automatically for the organization
    management account. The organization management account must enable Security Hub before the administrator
    account enables it as a member account. A permissions policy is added that permits the administrator account to
    view the findings generated in the member account. When Security Hub is enabled in a member account, the member
    account findings are also visible to the administrator account.  To remove the association between the
    administrator and member accounts, use the DisassociateFromMasterAccount or DisassociateMembers operation.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        account_details(List): The list of accounts to associate with the Security Hub administrator account. For each account,
            the list includes the account ID and optionally the email address.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.securityhub.create_members(
                ctx,
                
                
                **{"AccountDetails": account_details}
            )
            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.securityhub.get_members(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, account_ids: List)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified member accounts from Security Hub. Can be used to delete member accounts that belong to an
    organization as well as member accounts that were invited manually.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        account_ids(List): The list of account IDs for the member accounts to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

