def __get_custom_ou_for_computer()

in ad-joining/register-computer/main.py [0:0]


def __get_custom_ou_for_computer(ad_connection, gce_instance, instance_name, project_id):
    computer_ou = None
    # The service is configured to use custom OUs. Make sure the root OU is valid
    custom_ou_root = __read_required_setting("CUSTOM_OU_ROOT_DN")
    logging.debug("Service is configured to use custom OU root '%s'" % custom_ou_root)
    if __is_custom_ou_valid(ad_connection, custom_ou_root):
        # Locate the custom OU for the computer and make sure it is valid
        custom_target_ou = __get_computer_ou_from_metadata(gce_instance)
        if custom_target_ou and __is_custom_ou_valid(ad_connection, custom_target_ou):
            logging.debug("Found custom OU '%s' for compute instance '%s' in project '%s'" 
                % (custom_target_ou, instance_name, project_id))

            # Verify the OU provided for the computer is a descendant of the custom root OU
            if custom_target_ou.lower().endswith(custom_ou_root.lower()):
                computer_ou = custom_target_ou
            else:
                logging.error("The OU '%s' provided by the computer instance '%s' is not a descendant of the root OU '%s'" 
                    % (custom_target_ou, instance_name, custom_ou_root))
        else:
            logging.error("The OU '%s' provided by the computer instance '%s' is either missing or not valid" 
                % (custom_target_ou, instance_name))
    else:
        logging.error("Custom OU root '%s' is not valid" % custom_ou_root)
    
    return computer_ou