def sync_users(api_token, input_filepath): """ Load local User data, get system User data, compare and add users to system """ sc_client = sp.SafetyCulture(api_token) logger = import_grs.configure_logger() # Validating the CSV input first dataValidator = open(input_filepath, 'r') reader = csv.reader(dataValidator) header = next(reader) if header != ['email', 'lastname', 'firstname', 'groups']: logger.info('Header Missing') return for data in reader: if len(data) != 4: logger.info('Invalid row length: %s' % data) return data[0] = str(data[0]) data[1] = str(data[1]) data[2] = str(data[2]) data[3] = str(data[3]) all_group_details = json.loads(sc_client.get_all_groups_in_org().content) server_users = export_users.get_all_users_and_groups(api_token) process_desired_state(server_users, input_filepath) process_server_state(server_users, input_filepath) execute_actions(all_group_details, sc_client)
def main(): """ Load local response_set data, get remote response_set data, compare and reconcile """ parser = argparse.ArgumentParser() parser.add_argument('-f', '--file', required=True) parser.add_argument('-t', '--token', required=True) args = parser.parse_args() file_path = args.file api_token = args.token logger = configure_logger() sc_client = sp.SafetyCulture(api_token) if file_path is not None: local_response_sets = read_workbook(logger, file_path) if local_response_sets is not None: remote_response_sets = sc_client.get_response_sets() remote_rs_names = [x['name'] for x in remote_response_sets] for response_set_name in local_response_sets: if response_set_name in remote_rs_names: handle_matching_rs(logger, local_response_sets, remote_response_sets, response_set_name, sc_client) else: name = response_set_name responses = local_response_sets[response_set_name] sc_client.create_response_set(name, responses)
def configure(logger, path_to_config_file, export_formats, docker_enabled): """ instantiate and configure logger, load config settings from file, instantiate SafetyCulture SDK :param logger: the logger :param path_to_config_file: path to config file :param export_formats: desired export formats :return: instance of SafetyCulture SDK object, config settings """ config_settings = load_config_settings(logger, path_to_config_file, docker_enabled) config_settings[EXPORT_FORMATS] = export_formats if config_settings[PROXY_HTTP] is not None and config_settings[ PROXY_HTTPS] is not None: proxy_settings = { "http": config_settings[PROXY_HTTP], "https": config_settings[PROXY_HTTPS] } else: proxy_settings = None sc_client = sp.SafetyCulture( config_settings[API_TOKEN], proxy_settings=proxy_settings, certificate_settings=config_settings[SSL_CERT], ssl_verify=config_settings[SSL_VERIFY]) if config_settings[EXPORT_PATH] is not None: if config_settings[CONFIG_NAME] is not None: create_directory_if_not_exists( logger, os.path.join(config_settings[EXPORT_PATH])) else: logger.error( "You must set the config_name in your config file before continuing." ) sys.exit() else: logger.info('No export path was found in ' + path_to_config_file + ', defaulting to /exports') config_settings[EXPORT_PATH] = os.path.join(os.getcwd(), 'exports') if config_settings[CONFIG_NAME] is not None: create_directory_if_not_exists( logger, os.path.join(config_settings[EXPORT_PATH])) else: logger.error( "You must set the config_name in your config file before continuing." ) sys.exit() return sc_client, config_settings
def get_all_users_and_groups(api_token): """ Exports a dictionary of all active users from iAuditor organisation and their associated groups :return: A sorted dictionary of all active users and their associated groups """ sc_client = sp.SafetyCulture(api_token) org_id = sc_client.get_my_org() groups_list = [] user_map = {} users_of_org = json.loads(sc_client.get_users_of_group(org_id)) for user in users_of_org['users']: if user['status'] != 'active': continue email = user['email'] user_map[email] = { 'groups': [], 'firstname': user['firstname'], 'lastname': user['lastname'], 'user_id': user['user_id'] } all_group_details = json.loads(sc_client.get_all_groups_in_org().content) groups_list = [g['id'] for g in all_group_details['groups']] for group_id in groups_list: users_in_group = json.loads(sc_client.get_users_of_group(group_id)) groups = all_group_details['groups'] target_group = _.find(groups, {'id': group_id}) group_name = target_group['name'] for user in users_in_group['users']: if user['status'] != 'active': continue email = user['email'] user_map[email]['user_id'] = user['user_id'] if email in user_map: if group_name not in user_map[email]['groups']: user_map[email]['groups'].append(str(group_name)) user_map[email]['groups'].append(str(group_id)) else: user_map[email]['groups'] = [group_name] user_map[email]['groups'] = [group_name] sorted_user_map = OrderedDict(sorted(user_map.items(), key=lambda t: t[0])) return sorted_user_map
def configure(logger, path_to_config_file, export_formats): """ instantiate and configure logger, load config settings from file, instantiate SafetyCulture SDK :param logger: the logger :param path_to_config_file: path to config file :param export_formats: desired export formats :return: instance of SafetyCulture SDK object, config settings """ config_settings = load_config_settings(logger, path_to_config_file) config_settings[EXPORT_FORMATS] = export_formats sc_client = sp.SafetyCulture(config_settings[API_TOKEN]) if config_settings[EXPORT_PATH] is not None: create_directory_if_not_exists(logger, config_settings[EXPORT_PATH]) else: logger.info('Invalid export path was found in ' + path_to_config_file + ', defaulting to /exports') config_settings[EXPORT_PATH] = os.path.join(os.getcwd(), 'exports') create_directory_if_not_exists(logger, config_settings[EXPORT_PATH]) return sc_client, config_settings