def delete_user_from_team(team_id, user_key): """ Delete a user from a team. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.request('GET', _url((team_id,))[1], params=params, headers=headers) if response.status_code == 200: params = { 'team': { 'name': response.json()['team']['name'], 'users': [user for user in response.json()['team']['users'] if user['id'] != user_key] } } headers = api_utils.generate_headers('rw') try: response = requests.put(_url((team_id,))[1], json=params, headers=headers) if response_utils.response_error(response): # Check response has no errors click.echo('Deleting user from team with key: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo("Deleted user with key: '%s' from team: %s" % (user_key, team_id)) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1) elif response_utils.response_error(response): click.echo('Cannot find team. Deleting user from team %s failed.' % team_id, err=True) sys.exit(1) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def add_user_to_team(team_id, user_key): """ Add user with the provided user_key to team with provided team_id. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.get(_url((team_id, ))[1], params=params, headers=headers) if response.status_code == 200: params = { 'team': { 'name': response.json()['team']['name'], 'users': [ # we are doing a patch request here so it's safe to include the user_key # we want to add here { 'id': user_key } ] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(_url((team_id, ))[1], json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors click.echo('Adding user to team with key: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo('Added user with key: %s to team.' % user_key) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1) elif response_utils.response_error(response): click.echo('Cannot find team. Adding user to team %s failed.' % team_id, err=True) sys.exit(1) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def update(api_key_id, active): """ Enable or disable an api key with given ID """ action, url = _url((api_key_id,)) payload = { "apikey": { "active": active } } headers = api_utils.generate_headers('owner', method='PATCH', body=json.dumps(payload), action=action) try: response = requests.patch(url, json=payload, headers=headers) if response_utils.response_error(response): sys.stderr.write('Failed to %s api key with id: %s \n' % ('enable' if active else 'disable', api_key_id)) sys.exit(1) elif response.status_code == 200: sys.stdout.write('%s api key with id: %s\n' % ('Enabled' if active else 'Disabled', api_key_id)) handle_api_key_response(response) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def handle_response(response, progress_bar): """ Handle response. Exit if it has any errors, continue if status code is 202, print response if status code is 200. """ if response_utils.response_error( response) is True: # Check response has no errors exit(1) elif response.status_code == 200: progress = response.json().get('progress') if progress: progress_bar.update(progress) else: progress_bar.update(100) progress_bar.render_finish() print_response(response) if 'links' in response.json(): next_url = response.json()['links'][0]['href'] next_response = fetch_results(next_url) handle_response(next_response, progress_bar) return return elif response.status_code == 202: continue_request(response, progress_bar) return
def create_log(logname, params): """Add a new log to the current account. If a JSON object is given, use that as the request parameters. Otherwise, use the name provided """ if params is not None: request_params = params else: request_params = { 'log': { 'name': logname } } headers = api_utils.generate_headers('rw') try: response = requests.post(_url(), json=request_params, headers=headers) if response_utils.response_error(response): sys.stderr.write('Create log failed, status code: %d' % response.status_code) sys.exit(1) elif response.status_code == 201: api_utils.pretty_print_string_as_json(response.text) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def handle_create_user_response(response): """ Handle create user response. If it has any errors, print help. """ if response_utils.response_error( response) is True: # Check response has no errors if response.status_code >= 400: sys.stderr.write('Failed to add user - User may have already been ' 'added this account or have a Logentries account') sys.stderr.write( 'To add a new user: lecli adduser -f John -l Smyth -e [email protected]' ) sys.stderr.write( 'To add an existing user using their User Key: ' 'lecli adduser -u 12345678-aaaa-bbbb-1234-1234cb123456') sys.exit(1) if response.status_code == 200: user = response.json()['user'] print 'Added user to account:\nName: %s %s \nLogin: %s \nEmail: %s \nUser Key: %s' % \ (user['first_name'], user['last_name'], user['login_name'], user['email'], user['id']) if response.status_code == 201: user = response.json()['user'] print 'Added user to account:\nName: %s %s \nLogin: %s \nEmail: %s \nUser Key: %s' % \ (user['first_name'], user['last_name'], user['login_name'], user['email'], user['id']) if response.status_code == 403: sys.stderr.write("User you attempted to add is the account owner")
def rename_team(team_id, team_name): """ Rename team with the provided team_id. """ params = { 'team': { 'name': team_name, # as this is a patch request, it won't modify users in the team. # what we want is to update the name of the team only. 'users': [ {'id': ''} ] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(_url((team_id,))[1], json=params, headers=headers) if response_utils.response_error(response): # Check response has no errors click.echo('Renaming team with id: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo("Team: '%s' renamed to: '%s'" % (team_id, team_name)) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def add_log(logset_id, log_id): """ Add a log to the logset """ params = { "logset":{ "logs_info": [{ "id": str(log_id) }] } } headers = api_utils.generate_headers('ro') try: response = requests.get(_url((logset_id,))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Add log %s to logset %s failed\n' % (log_id, logset_id)) sys.exit(1) elif response.status_code == 200: existing_logset = response.json() replace_logset(logset_id, api_utils.combine_objects(existing_logset, params)) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def rename_team(team_id, team_name): """ Rename team with the provided team_id. """ url = _url() + '/' + team_id params = { 'team': { 'name': team_name, # as this is a patch request, it won't modify users in the team. # what we want is to update the name of the team only. 'users': [{ 'id': '' }] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors print 'Renaming team with id: %s failed, status code: %d' \ % (team_id, response.status_code) sys.exit(1) elif response.status_code == 200: print "Team: '%s' renamed to: '%s'" % (team_id, team_name) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def rename_team(team_id, team_name): """ Rename team with the provided team_id. """ params = { 'team': { 'name': team_name, # as this is a patch request, it won't modify users in the team. # what we want is to update the name of the team only. 'users': [{ 'id': '' }] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(_url((team_id, ))[1], json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors click.echo('Renaming team with id: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo("Team: '%s' renamed to: '%s'" % (team_id, team_name)) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def handle_create_user_response(response): """ Handle create user response. If it has any errors, print help. """ if response_utils.response_error(response) is True: # Check response has no errors if response.status_code >= 400: sys.stderr.write('Failed to add user - User may have already been ' 'added this account or have a Logentries account') sys.stderr.write('To add a new user: lecli adduser -f John -l Smyth -e [email protected]') sys.stderr.write('To add an existing user using their User Key: ' 'lecli adduser -u 12345678-aaaa-bbbb-1234-1234cb123456') sys.exit(1) if response.status_code == 200: user = response.json()['user'] print 'Added user to account:\nName: %s %s \nLogin: %s \nEmail: %s \nUser Key: %s' % \ (user['first_name'], user['last_name'], user['login_name'], user['email'], user['id']) if response.status_code == 201: user = response.json()['user'] print 'Added user to account:\nName: %s %s \nLogin: %s \nEmail: %s \nUser Key: %s' % \ (user['first_name'], user['last_name'], user['login_name'], user['email'], user['id']) if response.status_code == 403: sys.stderr.write("User you attempted to add is the account owner")
def delete_user_from_team(team_id, user_key): """ Delete a user from a team. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.request('GET', _url((team_id, ))[1], params=params, headers=headers) if response.status_code == 200: params = { 'team': { 'name': response.json()['team']['name'], 'users': [ user for user in response.json()['team']['users'] if user['id'] != user_key ] } } headers = api_utils.generate_headers('rw') try: response = requests.put(_url((team_id, ))[1], json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors click.echo('Deleting user from team with key: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo("Deleted user with key: '%s' from team: %s" % (user_key, team_id)) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1) elif response_utils.response_error(response): click.echo('Cannot find team. Deleting user from team %s failed.' % team_id, err=True) sys.exit(1) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def handle_api_key_response(response): """ Handle get api key response """ if response_utils.response_error(response): sys.exit(1) elif response.status_code in [200, 201]: api_utils.pretty_print_string_as_json(response.text)
def add_user_to_team(team_id, user_key): """ Add user with the provided user_key to team with provided team_id. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.request('GET', _url() + '/' + team_id, params=params, headers=headers) if response.status_code == 200: url = _url() + '/' + team_id params = { 'team': { 'name': response.json()['team']['name'], 'users': [ # we are doing a patch request here so it's safe to include the user_key # we want to add here { 'id': user_key } ] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors print 'Adding user to team with key: %s failed, status code: %d' \ % (team_id, response.status_code) sys.exit(1) elif response.status_code == 200: print "Added user with key: '%s' to team" % user_key except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1) elif response_utils.response_error(response): print 'Cannot find team. Adding user to team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) sys.exit(1) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def handle_get_log_response(response): """ Handle get log response """ if response_utils.response_error(response): sys.exit(1) elif response.status_code == 200: api_utils.pretty_print_string_as_json(response.text)
def handle_userlist_response(response): """ Handle userlist response. Exit if it has any errors, print if status code is 200. """ if response_utils.response_error(response) is True: # Check response has no errors sys.stderr.write(response.text) sys.exit(1) elif response.status_code == 200: print_users(response)
def delete_user_from_team(team_id, user_key): """ Delete a user from a team. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.request('GET', _url() + '/' + team_id, params=params, headers=headers) if response.status_code == 200: url = _url() + '/' + team_id params = { 'team': { 'name': response.json()['team']['name'], 'users': [ user for user in response.json()['team']['users'] if user['id'] != user_key ] } } headers = api_utils.generate_headers('rw') try: response = requests.put(url, json=params, headers=headers) if response_utils.response_error( response): # Check response has no errors print 'Deleting user from team with key: %s failed, status code: %d' \ % (team_id, response.status_code) sys.exit(1) elif response.status_code == 200: print "Deleted user with key: '%s' from team: %s" % ( user_key, team_id) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1) elif response_utils.response_error(response): print 'Cannot find team. Deleting user from team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) sys.exit(1) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def handle_response(response, error_message, success_code, success_message=None): """Handle logset responses""" if response_utils.response_error(response): sys.stderr.write(error_message) sys.exit(1) elif response.status_code == success_code: if success_message: sys.stdout.write(success_message) else: api_utils.pretty_print_string_as_json(response.text)
def handle_get_teams_response(response): """ Handle get teams response. """ if response_utils.response_error(response): sys.exit(1) elif response.status_code == 200: if response.json().get('teams'): print_teams(response.json()['teams']) elif response.json().get('team'): print_team(response.json()['team'])
def update_saved_query(query_id, name=None, statement=None, from_ts=None, to_ts=None, time_range=None, logs=None): """ Update a saved query with the given parameters. :param query_id: id of the saved query to be updated :param name: new name of the saved query :param statement: new leql statement of the saved query :param from_ts: new 'from' timestamp of the saved query :param to_ts: new 'to' timestamp of the saved query :param time_range: new time range of the saved query :param logs: colon(:) separated list of logs of the saved query """ headers = api_utils.generate_headers('rw') params = { 'saved_query': { } } if name: params['saved_query']['name'] = name if logs: params['saved_query']['logs'] = logs.split(':') if any([statement, from_ts, to_ts, time_range]): leql = {} if statement: leql['statement'] = statement if any([from_ts, to_ts, time_range]): during = {} if from_ts: during.update({'from': from_ts, 'to': None, 'time_range': None}) if to_ts: during.update({'to': to_ts, 'time_range': None}) if time_range: during.update({'time_range': time_range, 'from': None, 'to': None}) leql['during'] = during params['saved_query']['leql'] = leql try: response = requests.patch(_url() + "/" + query_id, json=params, headers=headers) if response_utils.response_error(response): sys.stderr.write('Updating saved query failed, status code: %d' % response.status_code) _pretty_print_saved_query_error(response) elif response.status_code == 200: click.echo('Saved query with id %s updated.' % query_id) _pretty_print_saved_query(response.json()['saved_query']) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def update_saved_query(query_id, name=None, statement=None, from_ts=None, to_ts=None, time_range=None, logs=None): """ Update a saved query with the given parameters. :param query_id: id of the saved query to be updated :param name: new name of the saved query :param statement: new leql statement of the saved query :param from_ts: new 'from' timestamp of the saved query :param to_ts: new 'to' timestamp of the saved query :param time_range: new time range of the saved query :param logs: colon(:) separated list of logs of the saved query """ headers = api_utils.generate_headers('rw') params = { 'saved_query': { } } if name: params['saved_query']['name'] = name if logs: params['saved_query']['logs'] = logs.split(':') if any([statement, from_ts, to_ts, time_range]): leql = {} if statement: leql['statement'] = statement if any([from_ts, to_ts, time_range]): during = {} if from_ts: during.update({'from': from_ts, 'to': None, 'time_range': None}) if to_ts: during.update({'to': to_ts, 'time_range': None}) if time_range: during.update({'time_range': time_range, 'from': None, 'to': None}) leql['during'] = during params['saved_query']['leql'] = leql try: response = requests.patch(_url((query_id,))[1], json=params, headers=headers) if response_utils.response_error(response): sys.stderr.write('Updating saved query failed.\n') _pretty_print_saved_query_error(response) elif response.status_code == 200: click.echo('Saved query with id %s updated.' % query_id) _pretty_print_saved_query(response.json()['saved_query']) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def delete_saved_query(query_id): """ Delete a specific saved query :param query_id: uuid of saved query to be deleted """ headers = api_utils.generate_headers('rw') try: response = requests.delete(_url((query_id, ))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete saved query failed.\n') elif response.status_code == 204: click.echo('Deleted saved query with id: %s' % query_id) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def add_user_to_team(team_id, user_key): """ Add user with the provided user_key to team with provided team_id. """ headers = api_utils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.get(_url((team_id,))[1], params=params, headers=headers) if response.status_code == 200: params = { 'team': { 'name': response.json()['team']['name'], 'users': [ # we are doing a patch request here so it's safe to include the user_key # we want to add here {'id': user_key} ] } } headers = api_utils.generate_headers('rw') try: response = requests.patch(_url((team_id,))[1], json=params, headers=headers) if response_utils.response_error(response): # Check response has no errors click.echo('Adding user to team with key: %s failed.' % team_id, err=True) sys.exit(1) elif response.status_code == 200: click.echo('Added user with key: %s to team.' % user_key) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1) elif response_utils.response_error(response): click.echo('Cannot find team. Adding user to team %s failed.' % team_id, err=True) sys.exit(1) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def delete_saved_query(query_id): """ Delete a specific saved query :param query_id: uuid of saved query to be deleted """ headers = api_utils.generate_headers('rw') try: response = requests.delete(_url((query_id,))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete saved query failed.\n') elif response.status_code == 204: click.echo('Deleted saved query with id: %s' % query_id) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def delete_log(log_id): """ Delete a log with the provided log ID """ headers = api_utils.generate_headers('rw') try: response = requests.delete(_url(('logs', log_id))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete log failed.') sys.exit(1) elif response.status_code == 204: sys.stdout.write('Deleted log with id: %s \n' % log_id) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_team(team_id): """ Delete a team with the provided team ID. """ headers = api_utils.generate_headers('rw') try: response = requests.delete(_url((team_id,))[1], headers=headers) if response_utils.response_error(response): # Check response has no errors click.echo('Delete team failed.', err=True) sys.exit(1) elif response.status_code == 204: click.echo('Deleted team with id: %s.' % team_id) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def delete_user(user_key): """ Delete a user from the current account. """ action, url = _url(('users', user_key)) headers = api_utils.generate_headers('owner', method='DELETE', action=action, body='') try: response = requests.request('DELETE', url, data='', headers=headers) if response_utils.response_error(response) is True: # Check response has no errors sys.stderr.write('Delete user failed, status code: %s' % response.status_code) sys.exit(1) elif response.status_code == 204: print('Deleted user') except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def rename_logset(logset_id, logset_name): """ Rename a given logset """ headers = api_utils.generate_headers('ro') try: response = requests.get(_url((logset_id, ))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Rename logset failed.\n') sys.exit(1) elif response.status_code == 200: params = response.json() params['logset']['name'] = logset_name replace_logset(logset_id, params) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_team(team_id): """ Delete a team with the provided team ID. """ headers = api_utils.generate_headers('rw') try: response = requests.delete(_url((team_id, ))[1], headers=headers) if response_utils.response_error( response): # Check response has no errors click.echo('Delete team failed.', err=True) sys.exit(1) elif response.status_code == 204: click.echo('Deleted team with id: %s.' % team_id) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def replace_log(log_id, params): """ Replace the given log with the details provided """ headers = api_utils.generate_headers('rw') try: response = requests.put(_url(('logs', log_id))[1], json=params, headers=headers) if response_utils.response_error(response): sys.stderr.write('Update log failed.\n') sys.exit(1) elif response.status_code == 200: sys.stdout.write('Log: %s updated to:\n' % log_id) api_utils.pretty_print_string_as_json(response.text) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def handle_tail(response, poll_interval, poll_iteration=1000): """ handle tailing loop """ for _ in range(poll_iteration): if response_utils.response_error(response): sys.exit(1) elif response.status_code == 200: print_response(response) # fetch results from the next link if 'links' in response.json(): next_url = response.json()['links'][0]['href'] time.sleep(poll_interval) response = fetch_results(next_url) else: click.echo('No continue link found in the received response.', err=True)
def delete(api_key_id): """ Delete an api key with the provided ID """ action, url = _url((api_key_id,)) headers = api_utils.generate_headers('owner', method='DELETE', body='', action=action) try: response = requests.delete(url, headers=headers) if response_utils.response_error(response): sys.stderr.write('Deleting api key failed.') sys.exit(1) elif response.status_code == 204: sys.stdout.write('Deleted api key with id: %s \n' % api_key_id) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def get_usage(start, end): """ Get usage information for the account between start and end dates. """ headers = api_utils.generate_headers('rw') params = {'from': start, 'to': end} try: response = requests.get(_url()[1], params=params, headers=headers) if response_utils.response_error(response): sys.stderr.write("Getting account usage failed. Status code %s" % response.status_code) sys.exit(1) else: _handle_get_usage_response(response) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_user(user_key): """ Delete a user from the current account. """ action, url = _url(('users', user_key)) headers = api_utils.generate_headers('owner', method='DELETE', action=action, body='') try: response = requests.request('DELETE', url, data='', headers=headers) if response_utils.response_error(response) is True: # Check response has no errors sys.stderr.write('Delete user failed, status code: %s' % response.status_code) sys.exit(1) elif response.status_code == 204: print 'Deleted user' except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_log(log_id): """ Delete a log with the provided log ID """ url = "/".join([_url(), log_id]) headers = api_utils.generate_headers('rw') try: response = requests.delete(url, headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete log failed, status code: %d' % response.status_code) sys.exit(1) elif response.status_code == 204: sys.stdout.write('Deleted log with id: %s \n' % log_id) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_team(team_id): """ Delete a team with the provided team ID. """ url = _url() + '/' + team_id headers = api_utils.generate_headers('rw') try: response = requests.delete(url, headers=headers) if response_utils.response_error( response): # Check response has no errors print 'Delete team failed, status code: %d' % response.status_code sys.exit(1) elif response.status_code == 204: print 'Deleted team with id: %s' % team_id except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def create_team(name): """ Add a new user to the current account. """ params = {'team': {'name': str(name), 'users': []}} headers = api_utils.generate_headers('rw') try: response = requests.post(_url(), json=params, headers=headers) if response_utils.response_error(response): print 'Creating team failed, status code: %d' % response.status_code sys.exit(1) elif response.status_code == 201: print 'Team created with name: %s' % name except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_log(logset_id, log_id): """ Delete a log from the logset """ headers = api_utils.generate_headers('ro') try: response = requests.get(_url((logset_id,))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete log %s from logset %s failed\n' % (log_id, logset_id)) sys.exit(1) elif response.status_code == 200: existing_logset = response.json() params = extract_log_from_logset(existing_logset, log_id) replace_logset(logset_id, params) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def delete_log(logset_id, log_id): """ Delete a log from the logset """ headers = api_utils.generate_headers('ro') try: response = requests.get(_url((logset_id, ))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Delete log %s from logset %s failed\n' % (log_id, logset_id)) sys.exit(1) elif response.status_code == 200: existing_logset = response.json() params = extract_log_from_logset(existing_logset, log_id) replace_logset(logset_id, params) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def create_team(name): """ Add a new user to the current account. """ params = {'team': {'name': str(name), 'users': []}} headers = api_utils.generate_headers('rw') try: response = requests.post(_url()[1], json=params, headers=headers) if response_utils.response_error(response): click.echo('Creating team failed.', err=True) sys.exit(1) elif response.status_code == 201: click.echo('Team created with name: %s' % name) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)
def rename_logset(logset_id, logset_name): """ Rename a given logset """ headers = api_utils.generate_headers('ro') try: response = requests.get(_url((logset_id,))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Rename logset failed.\n') sys.exit(1) elif response.status_code == 200: params = response.json() params['logset']['name'] = logset_name replace_logset(logset_id, params) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def create(payload): """ Create an api key with the provided ID """ action, url = _url() headers = api_utils.generate_headers('owner', method='POST', body=json.dumps(payload), action=action) try: response = requests.post(url, headers=headers, json=payload) if response_utils.response_error(response): sys.stderr.write('Create api key failed.') sys.exit(1) elif response.status_code == 201: handle_api_key_response(response) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1)
def get_saved_query(query_id=None): """ If query id is provided, get this specific saved query or get them all :param query_id: uuid of saved query to be retrieved(optional) """ endpoint_url = _url()[1] if query_id: endpoint_url = _url((query_id,))[1] headers = api_utils.generate_headers('rw') try: response = requests.get(endpoint_url, headers=headers) if response_utils.response_error(response): if query_id: sys.stderr.write("Unable to retrieve saved query with id %s" % query_id) else: sys.stderr.write("Unable to retrieve saved queries.") elif response.status_code == 200: _handle_saved_query_response(response) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def handle_response(response, progress_bar): """ Handle response. Exit if it has any errors, continue if status code is 202, print response if status code is 200. """ if response_utils.response_error(response) is True: # Check response has no errors sys.exit(1) elif response.status_code == 200: progress = response.json().get('progress') if progress: progress_bar.update(progress) else: progress_bar.update(100) progress_bar.render_finish() print_response(response) if 'links' in response.json(): next_url = response.json()['links'][0]['href'] next_response = fetch_results(next_url) handle_response(next_response, progress_bar) elif response.status_code == 202: continue_request(response, progress_bar)
def create_saved_query(name, statement, from_ts=None, to_ts=None, time_range=None, logs=None): """ Create a new saved query with the provided fields. :param name: name of the saved query (mandatory) :param statement: leql statement of the query (mandatory) :param from_ts: 'from' timestamp of query - unix epoch timestamp (optional) :param to_ts: 'to' timestamp of query - unix epoch timestamp (optional) :param time_range: time range of query - cannot be defimed with 'from' and/or 'to' fields( optional) :param logs: list of logs of the saved query, colon(:) separated uuids. """ headers = api_utils.generate_headers('rw') params = { 'saved_query': { 'name': name, 'leql': { 'statement': statement, 'during': { 'from': from_ts, 'to': to_ts, 'time_range': time_range } }, 'logs': logs.split(':') if logs else [] } } try: response = requests.post(_url()[1], json=params, headers=headers) if response_utils.response_error(response): sys.stderr.write('Creating saved query failed.\n') _pretty_print_saved_query_error(response) elif response.status_code == 201: click.echo('Saved query created with name: %s' % name) _pretty_print_saved_query(response.json()['saved_query']) except requests.exceptions.RequestException as error: click.echo(error) sys.exit(1)
def get_log_keys_from_logset(logset_id): """Helper method to get list of log IDs in a logset""" headers = api_utils.generate_headers('ro') log_ids = [] try: response = requests.get(_url((logset_id,))[1], headers=headers) if response_utils.response_error(response): sys.stderr.write('Attempt to access logset %s failed\n' % logset_id) sys.exit(1) elif response.status_code == 200: existing_logset = response.json() if 'logs_info' in existing_logset['logset']: for log in existing_logset['logset']['logs_info']: log_ids.append(log['id']) except requests.exceptions.RequestException as error: sys.stderr.write(error) sys.exit(1) if log_ids is not []: return log_ids else: sys.stderr.write("No logs found in logset %s " % logset_id) sys.exit(1)
def create_team(name): """ Add a new user to the current account. """ params = { 'team': { 'name': str(name), 'users': [] } } headers = api_utils.generate_headers('rw') try: response = requests.post(_url()[1], json=params, headers=headers) if response_utils.response_error(response): click.echo('Creating team failed.', err=True) sys.exit(1) elif response.status_code == 201: click.echo('Team created with name: %s' % name) except requests.exceptions.RequestException as error: click.echo(error, err=True) sys.exit(1)