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

hub.exec.boto3.client.organizations.create_organizational_unit
hub.exec.boto3.client.organizations.delete_organizational_unit
hub.exec.boto3.client.organizations.describe_organizational_unit
hub.exec.boto3.client.organizations.update_organizational_unit
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, parent_id: Text, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an organizational unit (OU) within a root or parent OU. An OU is a container for accounts that enables
    you to organize your accounts to apply policies according to your business requirements. The number of levels
    deep that you can nest OUs is dependent upon the policy types enabled for that root. For service control
    policies, the limit is five. For more information about OUs, see Managing Organizational Units in the AWS
    Organizations User Guide.  If the request includes tags, then the requester must have the
    organizations:TagResource permission. This operation can be called only from the organization's management
    account.

    Args:
        name(Text): The friendly name to assign to the new OU.
        parent_id(Text): The unique identifier (ID) of the parent root or OU that you want to create the new OU in. The
            regex pattern for a parent ID string requires one of the following:    Root - A string that
            begins with "r-" followed by from 4 to 32 lowercase letters or digits.    Organizational unit
            (OU) - A string that begins with "ou-" followed by from 4 to 32 lowercase letters or digits (the
            ID of the root that the OU is in). This string is followed by a second "-" dash and from 8 to 32
            additional lowercase letters or digits.
        tags(List, optional): A list of tags that you want to attach to the newly created OU. For each tag in the list, you
            must specify both a tag key and a value. You can set the value to an empty string, but you can't
            set it to null. For more information about tagging, see Tagging AWS Organizations resources in
            the AWS Organizations User Guide.  If any one of the tags is invalid or if you exceed the
            allowed number of tags for an OU, then the entire request fails and the OU is not created. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

async def absent(hub, ctx, name: Text, organizational_unit_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes an organizational unit (OU) from a root or another OU. You must first remove all accounts and child OUs
    from the OU that you want to delete. 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.
        organizational_unit_id(Text): The unique identifier (ID) of the organizational unit that you want to delete. You can get the
            ID from the ListOrganizationalUnitsForParent operation. The regex pattern for an organizational
            unit ID string requires "ou-" followed by from 4 to 32 lowercase letters or digits (the ID of
            the root that contains the OU). This string is followed by a second "-" dash and from 8 to 32
            additional lowercase letters or digits.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

