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

hub.exec.boto3.client.organizations.create_organization
hub.exec.boto3.client.organizations.delete_organization
hub.exec.boto3.client.organizations.describe_organization
hub.exec.boto3.client.organizations.leave_organization
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, feature_set: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an AWS organization. The account whose user is calling the CreateOrganization operation automatically
    becomes the management account of the new organization. This operation must be called using credentials from the
    account that is to become the new organization's management account. The principal must also have the relevant
    IAM permissions. By default (or if you set the FeatureSet parameter to ALL), the new organization is created
    with all features enabled and service control policies automatically enabled in the root. If you instead choose
    to create the organization supporting only the consolidated billing features by setting the FeatureSet parameter
    to CONSOLIDATED_BILLING", no policy types are enabled by default, and you can't use organization policies

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        feature_set(Text, optional): Specifies the feature set supported by the new organization. Each feature set supports different
            levels of functionality.    CONSOLIDATED_BILLING: All member accounts have their bills
            consolidated to and paid by the management account. For more information, see Consolidated
            billing in the AWS Organizations User Guide.   The consolidated billing feature subset isn't
            available for organizations in the AWS GovCloud (US) Region.    ALL: In addition to all the
            features supported by the consolidated billing feature set, the management account can also
            apply any policy type to any member account in the organization. For more information, see All
            features in the AWS Organizations User Guide. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

async def absent(hub, ctx, name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the organization. You can delete an organization only by using credentials from the management account.
    The organization must be empty of member accounts.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

