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

hub.exec.boto3.client.sso_admin.create_account_assignment
hub.exec.boto3.client.sso_admin.delete_account_assignment
hub.exec.boto3.client.sso_admin.list_account_assignments
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, instance_arn: Text, target_id: Text, target_type: Text, permission_set_arn: Text, principal_type: Text, principal_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Assigns access to a principal for a specified Amazon Web Services account using a specified permission set.  The
    term principal here refers to a user or group that is defined in Amazon Web Services SSO.   As part of a
    successful CreateAccountAssignment call, the specified permission set will automatically be provisioned to the
    account in the form of an IAM policy. That policy is attached to the SSO-created IAM role. If the permission set
    is subsequently updated, the corresponding IAM policies attached to roles in your accounts will not be updated
    automatically. In this case, you must call  ProvisionPermissionSet  to make these updates.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        instance_arn(Text): The ARN of the SSO instance under which the operation will be executed. For more information
            about ARNs, see Amazon Resource Names (ARNs) and Amazon Web Services Service Namespaces in the
            Amazon Web Services General Reference.
        target_id(Text): TargetID is an Amazon Web Services account identifier, typically a 10-12 digit string (For
            example, 123456789012).
        target_type(Text): The entity type for which the assignment will be created.
        permission_set_arn(Text): The ARN of the permission set that the admin wants to grant the principal access to.
        principal_type(Text): The entity type for which the assignment will be created.
        principal_id(Text): An identifier for an object in Amazon Web Services SSO, such as a user or group. PrincipalIds
            are GUIDs (For example, f81d4fae-7dec-11d0-a765-00a0c91e6bf6). For more information about
            PrincipalIds in Amazon Web Services SSO, see the Amazon Web Services SSO Identity Store API
            Reference.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.sso_admin.account_assignment.present:
                - name: value
                - instance_arn: value
                - target_id: value
                - target_type: value
                - permission_set_arn: value
                - principal_type: value
                - principal_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.sso_admin.account_assignment.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.sso_admin.list_account_assignments(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.sso_admin.create_account_assignment(
                ctx,
                
                
                **{"InstanceArn": instance_arn, "TargetId": target_id, "TargetType": target_type, "PermissionSetArn": permission_set_arn, "PrincipalType": principal_type, "PrincipalId": principal_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.sso_admin.list_account_assignments(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, instance_arn: Text, target_id: Text, target_type: Text, permission_set_arn: Text, principal_type: Text, principal_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a principal's access from a specified Amazon Web Services account using a specified permission set.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        instance_arn(Text): The ARN of the SSO instance under which the operation will be executed. For more information
            about ARNs, see Amazon Resource Names (ARNs) and Amazon Web Services Service Namespaces in the
            Amazon Web Services General Reference.
        target_id(Text): TargetID is an Amazon Web Services account identifier, typically a 10-12 digit string (For
            example, 123456789012).
        target_type(Text): The entity type for which the assignment will be deleted.
        permission_set_arn(Text): The ARN of the permission set that will be used to remove access.
        principal_type(Text): The entity type for which the assignment will be deleted.
        principal_id(Text): An identifier for an object in Amazon Web Services SSO, such as a user or group. PrincipalIds
            are GUIDs (For example, f81d4fae-7dec-11d0-a765-00a0c91e6bf6). For more information about
            PrincipalIds in Amazon Web Services SSO, see the Amazon Web Services SSO Identity Store API
            Reference.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.sso_admin.account_assignment.absent:
                - name: value
                - instance_arn: value
                - target_id: value
                - target_type: value
                - permission_set_arn: value
                - principal_type: value
                - principal_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.sso_admin.account_assignment.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.sso_admin.list_account_assignments(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.sso_admin.delete_account_assignment(
                ctx,
                
                
                **{"InstanceArn": instance_arn, "TargetId": target_id, "TargetType": target_type, "PermissionSetArn": permission_set_arn, "PrincipalType": principal_type, "PrincipalId": principal_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.sso_admin.list_account_assignments(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

