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

hub.exec.boto3.client.organizations.accept_handshake
hub.exec.boto3.client.organizations.cancel_handshake
hub.exec.boto3.client.organizations.decline_handshake
hub.exec.boto3.client.organizations.describe_handshake
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, handshake_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Sends a response to the originator of a handshake agreeing to the action proposed by the handshake request. This
    operation can be called only by the following principals when they also have the relevant IAM permissions:
    Invitation to join or Approve all features request handshakes: only a principal from the member account. The
    user who calls the API for an invitation to join must have the organizations:AcceptHandshake permission. If you
    enabled all features in the organization, the user must also have the iam:CreateServiceLinkedRole permission so
    that AWS Organizations can create the required service-linked role named AWSServiceRoleForOrganizations. For
    more information, see AWS Organizations and Service-Linked Roles in the AWS Organizations User Guide.    Enable
    all features final confirmation handshake: only a principal from the management account. For more information
    about invitations, see Inviting an AWS Account to Join Your Organization in the AWS Organizations User Guide.
    For more information about requests to enable all features in the organization, see Enabling All Features in
    Your Organization in the AWS Organizations User Guide.    After you accept a handshake, it continues to appear
    in the results of relevant APIs for only 30 days. After that, it's deleted.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        handshake_id(Text): The unique identifier (ID) of the handshake that you want to accept. The regex pattern for
            handshake ID string requires "h-" followed by from 8 to 32 lowercase letters or digits.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

async def absent(hub, ctx, name: Text, handshake_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Cancels a handshake. Canceling a handshake sets the handshake state to CANCELED. This operation can be called
    only from the account that originated the handshake. The recipient of the handshake can't cancel it, but can use
    DeclineHandshake instead. After a handshake is canceled, the recipient can no longer respond to that handshake.
    After you cancel a handshake, it continues to appear in the results of relevant APIs for only 30 days. After
    that, it's deleted.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        handshake_id(Text): The unique identifier (ID) of the handshake that you want to cancel. You can get the ID from the
            ListHandshakesForOrganization operation. The regex pattern for handshake ID string requires "h-"
            followed by from 8 to 32 lowercase letters or digits.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

