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

hub.exec.boto3.client.organizations.deregister_delegated_administrator
hub.exec.boto3.client.organizations.list_delegated_administrators
hub.exec.boto3.client.organizations.register_delegated_administrator
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, account_id: Text, service_principal: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Enables the specified member account to administer the Organizations features of the specified AWS service. It
    grants read-only access to AWS Organizations service data. The account still requires IAM permissions to access
    and administer the AWS service. You can run this action only for AWS services that support this feature. For a
    current list of services that support it, see the column Supports Delegated Administrator in the table at AWS
    Services that you can use with AWS Organizations in the AWS Organizations User Guide.  This operation can be
    called only from the organization's management account.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        account_id(Text): The account ID number of the member account in the organization to register as a delegated
            administrator.
        service_principal(Text): The service principal of the AWS service for which you want to make the member account a
            delegated administrator.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

async def absent(hub, ctx, name: Text, account_id: Text, service_principal: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Removes the specified member AWS account as a delegated administrator for the specified AWS service.
    Deregistering a delegated administrator can have unintended impacts on the functionality of the enabled AWS
    service. See the documentation for the enabled service before you deregister a delegated administrator so that
    you understand any potential impacts.  You can run this action only for AWS services that support this feature.
    For a current list of services that support it, see the column Supports Delegated Administrator in the table at
    AWS Services that you can use with AWS Organizations in the AWS Organizations User Guide.  This operation can be
    called only from the organization's management account.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        account_id(Text): The account ID number of the member account in the organization that you want to deregister as a
            delegated administrator.
        service_principal(Text): The service principal name of an AWS service for which the account is a delegated administrator.
            Delegated administrator privileges are revoked for only the specified AWS service from the
            member account. If the specified service is the only service for which the member account is a
            delegated administrator, the operation also revokes Organizations read action permissions.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

