def wrapper(args): global token if args.as_user is None: username = config().get('auth', 'username') else: username = args.as_user if args.with_password is None: if args.as_user is None: password = config().get('auth', 'password') else: password = args.as_user else: password = args.with_password login_url = config().get('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 succeed(): global spinner if spinner: if config().has_option('set', 'unicorn_mode') and config().getboolean( 'set', 'unicorn_mode'): spinner.stop_and_persist(symbol='🦄'.encode('utf-8')) else: spinner.succeed() spinner = None
def add_token(args): io.start('Adding token %s for user %s' % (highlight(args.token), highlight(args.user))) user = get(config().get('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().get('api', 'rest1') + 'sys_sec_Token', data)
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().get('api', 'import'), file_path.resolve(), params) import_run_url = urljoin(config().get('api', 'host'), response.text) status, message = _poll_for_completion(import_run_url) if status == 'FAILED': raise McmdError(message)
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().get('api', 'member') % 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().get('api', 'rest1') + 'sys_md_Package', 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().get('api', 'rls'), data={ 'id': args.entity, 'rlsEnabled': True })
def _find_group(role): io.debug('Fetching groups') groups = get(config().get('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 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().get('api', 'rls'), data={ 'id': args.entity, 'rlsEnabled': False })
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().get('api', 'rest1') + 'sys_sec_User', { 'username': args.username, 'password_': password, 'changePassword': ch_pwd, 'Email': email, 'active': active, 'superuser': superuser })
def _import_from_url(args): file_url = args.from_url file_name = file_url.split("/")[-1] io.start('Importing from URL %s' % highlight(file_url)) params = {'action': _get_import_action(file_name), 'metadataAction': 'upsert'} if args.to_package: params['packageId'] = args.to_package params['url'] = file_url response = import_by_url(params) import_run_url = urljoin(config().get('api', 'host'), response.text) status, message = _poll_for_completion(import_run_url) if status == 'FAILED': raise McmdError(message)
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().get('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 user_exists(username): log.debug('Checking if user %s exists' % username) response = get(config().get('api', 'rest2') + 'sys_sec_User?q=username==' + username) return int(response.json()['total']) > 0
def _get_import_action(file_name): if '.owl' in file_name or '.obo' in file_name: return 'add' else: return config().get('set', 'import_action')
def add_group(args): io.start('Adding group %s' % highlight(args.name)) post(config().get('api', 'group'), { 'name': args.name.lower(), 'label': args.name })
def _get_molgenis_folders(): if not config().has_option('data', 'git_root') or not config().has_option('data', 'git_paths'): io.info('Molgenis git paths not configured. Edit the mcmd.properties file to include the test data.') return list() else: return config_string_to_paths(config().get('data', 'git_paths'))
def _get_quick_folders(): if not config().has_option('data', 'quick_folders'): return list() else: return config_string_to_paths(config().get('data', 'quick_folders'))
def role_exists(rolename): log.debug('Checking if role %s exists' % rolename) response = get(config().get('api', 'rest2') + 'sys_sec_Role?q=name==' + rolename.upper()) return int(response.json()['total']) > 0
def import_by_url(params): return _handle_request( lambda: requests.post(config().get('api', 'import_url'), headers=_get_default_headers(), params=params))
def resource_exists(resource_id, resource_type): log.debug('Checking if %s %s exists' % (resource_type.get_label(), resource_id)) response = get(config().get('api', 'rest2') + resource_type.get_entity_id() + '?q=id==' + resource_id) return int(response.json()['total']) > 0