def add_token(args): io.start('Adding token %s for user %s' % (highlight(args.token), highlight(args.user))) user = get( config.api('rest2') + 'sys_sec_User?attrs=id&q=username==%s' % args.user) if user.json()['total'] == 0: raise McmdError('Unknown user %s' % args.user) user_id = user.json()['items'][0]['id'] data = {'User': user_id, 'token': args.token} post(config.api('rest1') + 'sys_sec_Token', data)
def wrapper(args): global token if args.as_user is None: username = config.username() else: username = args.as_user if args.with_password is None: if args.as_user is None: password = config.password() else: password = args.as_user else: password = args.with_password login_url = config.api('login') io.debug('Logging in as user %s' % username) response = post(login_url, data={ "username": username, "password": password }) token = response.json()['token'] func(args)
def resource_exists(resource_id, resource_type): log.debug('Checking if %s %s exists' % (resource_type.get_label(), resource_id)) response = get( config.api('rest2') + resource_type.get_entity_id() + '?q=id==' + resource_id) return int(response.json()['total']) > 0
def add_theme(args): """ add_theme adds a theme to the stylesheet table :param args: commandline arguments containing bootstrap3_theme and optionally bootstrap4_theme :return: None """ _validate_args(args) valid_types = {'text/css'} api = config.api('add_theme') paths = [] bs3_name = args.bootstrap3 bs4 = args.bootstrap4 paths = [bs3_name] names = ['bootstrap3-style'] if bs4: paths.append(bs4) names.append('bootstrap4-style') bs4_name = get_file_name_from_path(bs4) io.start( 'Adding bootstrap 3 theme {} and bootstrap 4 theme {} to bootstrap themes' .format(highlight(bs3_name), highlight(bs4_name))) else: io.start('Adding bootstrap 3 theme {} to bootstrap themes'.format( highlight(bs3_name))) if not args.from_path: paths = [_get_path_from_quick_folders(theme) for theme in paths] files = _prepare_files_for_upload(paths, names, valid_types) post_files(files, api)
def one_resource_exists(resources, resource_type): log.debug('Checking if one of [{}] exists in [{}]'.format( ','.join(resources), resource_type.get_label())) response = get( config.api('rest2') + resource_type.get_entity_id() + '?q=id=in=({})'.format(','.join(resources))) return int(response.json()['total']) > 0
def make(args): io.start('Making user %s a member of role %s' % (highlight(args.user), highlight(args.role.upper()))) group_name = _find_group(args.role) url = config.api('member').format(group_name) post(url, {'username': args.user, 'roleName': args.role.upper()})
def add_package(args): io.start('Adding package %s' % highlight(args.id)) data = {'id': args.id, 'label': args.id} if args.parent: data['parent'] = args.parent post(config.api('rest1') + 'sys_md_Package', data)
def enable_theme(args): """ enable_theme enables a bootstrap theme :param args: commandline arguments containing the id of the theme (without .css) :exception McmdError: when applying the theme fails :return None """ theme = args.theme.replace('.css', '').replace('.min', '') io.start('Applying theme {}'.format(highlight(theme))) # Resource can be bootstrap-name.min.css (if molgenis theme), or name.min.css (if uploaded .min.css), or # name.css (if uploaded as .css). if one_resource_exists([theme + '.min.css', theme + '.css', 'bootstrap-' + theme + '.min.css'], ResourceType.THEME): # Molgenis themes start with bootstrap- but this is stripped from the name in the theme manager try: post(config.api('set_theme'), theme) except: post(config.api('set_theme'), theme.split('bootstrap-')[1]) else: raise McmdError( 'Applying theme failed. No themes found containing {} in the name'.format(args.theme, 'sys_set_StyleSheet'))
def enable_rls(args): if not io.confirm( 'Are you sure you want to disable row level security on %s?' % args.entity): return io.start('Disabling row level security on entity type %s' % highlight(args.entity)) ensure_resource_exists(args.entity, ResourceType.ENTITY_TYPE) post(config.api('rls'), data={'id': args.entity, 'rlsEnabled': False})
def add_logo(args): """ add_logo uploads a logo to add to the left top of the menu :param args: commandline arguments containing path to logo :return: None """ io.start('Adding logo from path {}'.format(highlight(args.logo))) api = config.api('logo') valid_types = {'image/jpeg', 'image/png', 'image/gif'} logo = [args.logo] if not args.from_path: logo = [_get_path_from_quick_folders(args.logo)] files = _prepare_files_for_upload(logo, ['logo'], valid_types) post_files(files, api)
def _find_group(role): io.debug('Fetching groups') groups = get(config.api('rest2') + 'sys_sec_Group?attrs=name') role = lower_kebab(role) matches = { len(group['name']): group['name'] for group in groups.json()['items'] if role.startswith(group['name']) } if not matches: raise McmdError('No group found for role %s' % upper_snake(role)) return matches[max(matches, key=int)]
def _do_import(file_path, package): io.start('Importing %s' % (highlight(file_path.name))) params = { 'action': _get_import_action(file_path.name), 'metadataAction': 'upsert' } if package: params['packageId'] = package response = post_file(config.api('import'), file_path.resolve(), params) import_run_url = urljoin(config.get('host', 'selected'), response.text) status, message = _poll_for_completion(import_run_url) if status == 'FAILED': raise McmdError(message)
def add_user(args): io.start('Adding user %s' % highlight(args.username)) password = args.set_password if args.set_password else args.username email = args.with_email if args.with_email else args.username + '@molgenis.org' active = not args.is_inactive superuser = args.is_superuser ch_pwd = args.change_password post( config.api('rest1') + 'sys_sec_User', { 'username': args.username, 'password_': password, 'changePassword': ch_pwd, 'Email': email, 'active': active, 'superuser': superuser })
def grant(principal_type, principal_name, resource_type, identifier, permission): data = {'radio-' + identifier: permission} if principal_type == PrincipalType.USER: data['username'] = principal_name elif principal_type == PrincipalType.ROLE: data['rolename'] = principal_name.upper() else: raise McmdError('Unknown principal type: %s' % principal_type) url = config.api('perm') + resource_type.get_resource_name( ) + '/' + principal_type.value return _handle_request(lambda: requests.post( url, headers={ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'x-molgenis-token': token }, data=data))
def enable_rls(args): io.start('Enabling row level security on entity type %s' % highlight(args.entity)) ensure_resource_exists(args.entity, ResourceType.ENTITY_TYPE) post(config.api('rls'), data={'id': args.entity, 'rlsEnabled': True})
def _delete_row(entity, row): url = '{}{}'.format(config.api('rest2'), entity) delete_data(url, [row])
def _delete_all_data(entity): io.start('Deleting all data from entity {}'.format(highlight(entity))) url = '{}{}'.format(config.api('rest1'), entity) delete(url)
def import_by_url(params): return _handle_request( lambda: requests.post(config.api('import_url'), headers=_get_default_headers(), params=params))
def get_version(): response = get(urljoin(config.api('rest2'), 'version')) return response.json()['molgenisVersion']
def user_exists(username): log.debug('Checking if user %s exists' % username) response = get( config.api('rest2') + 'sys_sec_User?q=username==' + username) return int(response.json()['total']) > 0
def role_exists(rolename): log.debug('Checking if role %s exists' % rolename) response = get( config.api('rest2') + 'sys_sec_Role?q=name==' + rolename.upper()) return int(response.json()['total']) > 0
def add_group(args): io.start('Adding group %s' % highlight(args.name)) post(config.api('group'), {'name': args.name.lower(), 'label': args.name})