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

hub.exec.boto3.client.config.delete_organization_config_rule
hub.exec.boto3.client.config.describe_organization_config_rules
hub.exec.boto3.client.config.put_organization_config_rule
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, organization_config_rule_name: Text, organization_managed_rule_metadata: Dict = None, organization_custom_rule_metadata: Dict = None, excluded_accounts: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Adds or updates organization config rule for your entire organization evaluating whether your AWS resources
    comply with your desired configurations.  Only a master account and a delegated administrator can create or
    update an organization config rule. When calling this API with a delegated administrator, you must ensure AWS
    Organizations ListDelegatedAdministrator permissions are added.  This API enables organization service access
    through the EnableAWSServiceAccess action and creates a service linked role
    AWSServiceRoleForConfigMultiAccountSetup in the master or delegated administrator account of your organization.
    The service linked role is created only when the role does not exist in the caller account. AWS Config verifies
    the existence of role with GetRole action. To use this API with delegated administrator, register a delegated
    administrator by calling AWS Organization register-delegated-administrator for config-
    multiaccountsetup.amazonaws.com.  You can use this action to create both custom AWS Config rules and AWS managed
    Config rules. If you are adding a new custom AWS Config rule, you must first create AWS Lambda function in the
    master account or a delegated administrator that the rule invokes to evaluate your resources. When you use the
    PutOrganizationConfigRule action to add the rule to AWS Config, you must specify the Amazon Resource Name (ARN)
    that AWS Lambda assigns to the function. If you are adding an AWS managed Config rule, specify the rule's
    identifier for the RuleIdentifier key. The maximum number of organization config rules that AWS Config supports
    is 150 and 3 delegated administrator per organization.   Prerequisite: Ensure you call EnableAllFeatures API to
    enable all features in an organization. Specify either OrganizationCustomRuleMetadata or
    OrganizationManagedRuleMetadata.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        organization_config_rule_name(Text): The name that you assign to an organization config rule.
        organization_managed_rule_metadata(Dict, optional): An OrganizationManagedRuleMetadata object. Defaults to None.
        organization_custom_rule_metadata(Dict, optional): An OrganizationCustomRuleMetadata object. Defaults to None.
        excluded_accounts(List, optional): A comma-separated list of accounts that you want to exclude from an organization config rule. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.config.put_organization_config_rule(
                ctx,
                
                
                **{"OrganizationConfigRuleName": organization_config_rule_name, "OrganizationManagedRuleMetadata": organization_managed_rule_metadata, "OrganizationCustomRuleMetadata": organization_custom_rule_metadata, "ExcludedAccounts": excluded_accounts}
            )
            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.config.describe_organization_config_rules(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, organization_config_rule_name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified organization config rule and all of its evaluation results from all member accounts in
    that organization.  Only a master account and a delegated administrator account can delete an organization
    config rule. When calling this API with a delegated administrator, you must ensure AWS Organizations
    ListDelegatedAdministrator permissions are added. AWS Config sets the state of a rule to DELETE_IN_PROGRESS
    until the deletion is complete. You cannot update a rule while it is in this state.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        organization_config_rule_name(Text): The name of organization config rule that you want to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

