def add_new_user(first_name, last_name, email): """ Add a new user to the current account. """ action = 'management/accounts/' + str( apiutils.get_account_resource_id()) + '/users' json_content = { "user": { "email": str(email), "first_name": str(first_name), "last_name": str(last_name) } } body = json.dumps(json_content) headers = apiutils.generate_headers('owner', method='POST', action=action, body=body) try: response = requests.request('POST', _url('user'), json=json_content, headers=headers) handle_create_user_response(response) except requests.exceptions.RequestException as error: print error 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 = apiutils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if response_error(response): # Check response has no errors print 'Renaming team with id: %s failed, status code: %d' \ % (team_id, response.status_code) exit(1) elif response.status_code == 200: print "Team: '%s' renamed to: '%s'" % (team_id, team_name) except requests.exceptions.RequestException as error: print error exit(1)
def test_generate_headers_ro(mocked_ro_apikey): mocked_ro_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH headers = apiutils.generate_headers(api_key_type='ro') assert "x-api-key" in headers assert headers["x-api-key"] == misc_ex.TEST_APIKEY_WITH_VALID_LENGTH
def test_generate_header_rw(mocked_rw_apikey): mocked_rw_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH headers = apiutils.generate_headers(api_key_type='rw') assert 'x-api-key' in headers assert headers['x-api-key'] == misc_ex.TEST_APIKEY_WITH_VALID_LENGTH
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 = apiutils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if response_error(response): # Check response has no errors print 'Renaming team with id: %s failed, status code: %d' \ % (team_id, response.status_code) exit(1) elif response.status_code == 200: print "Team: '%s' renamed to: '%s'" % (team_id, team_name) except requests.exceptions.RequestException as error: print error exit(1)
def get_events(log_keys, time_from=None, time_to=None, date_from=None, date_to=None): """ Get events belonging to log_keys and within the time range provided. """ if date_from is not None and date_to is not None: from_ts = int( time.mktime(time.strptime(date_from, "%Y-%m-%d %H:%M:%S"))) * 1000 to_ts = int(time.mktime(time.strptime(date_to, "%Y-%m-%d %H:%M:%S"))) * 1000 else: from_ts = time_from * 1000 to_ts = time_to * 1000 leql = { "during": { "from": from_ts, "to": to_ts }, "statement": "where(/.*/)" } payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def post_query(log_keys, query_string, time_from=None, time_to=None, date_from=None, date_to=None): """ Post query to Logentries. """ if date_from is not None and date_to is not None: from_ts = int( time.mktime(time.strptime(date_from, "%Y-%m-%d %H:%M:%S"))) * 1000 to_ts = int(time.mktime(time.strptime(date_to, "%Y-%m-%d %H:%M:%S"))) * 1000 else: from_ts = time_from * 1000 to_ts = time_to * 1000 leql = { "during": { "from": from_ts, "to": to_ts }, "statement": query_string } payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def test_generate_header_owner(mocked_owner_apikey, mocked_owner_apikey_id): mocked_owner_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH mocked_owner_apikey_id.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH headers = apiutils.generate_headers(api_key_type='owner', body='', method="GET", action="action") assert 'Date' in headers assert 'authorization-api-key' in headers assert misc_ex.TEST_APIKEY_WITH_VALID_LENGTH in headers['authorization-api-key']
def add_user_to_team(team_id, user_key): """ Add user with the provided user_key to team with provided team_id. """ headers = apiutils.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 = apiutils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if 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) exit(1) elif response.status_code == 200: print "Added user with key: '%s' to team" % user_key except requests.exceptions.RequestException as error: print error exit(1) elif response_error(response): print 'Cannot find team. Adding user to team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) exit(1) except requests.exceptions.RequestException as error: print error exit(1)
def fetch_results(provided_url): """ Make the get request to the url and return the response. """ try: response = requests.get(provided_url, headers=apiutils.generate_headers('rw')) return response except requests.exceptions.RequestException as error: print error exit(1)
def delete_user_from_team(team_id, user_key): """ Delete a user from a team. """ headers = apiutils.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 = apiutils.generate_headers('rw') try: response = requests.put(url, json=params, headers=headers) if 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) 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: print error exit(1) elif response_error(response): print 'Cannot find team. Deleting user from team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) exit(1) except requests.exceptions.RequestException as error: print error exit(1)
def get_teams(): """ Get teams associated with the user. """ headers = apiutils.generate_headers('rw') try: response = requests.request('GET', _url(), data='', headers=headers) handle_get_teams_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def list_users(): """ List users that is in the current account. """ action = 'management/accounts/' + str(apiutils.get_account_resource_id()) + '/users' try: response = requests.request('GET', _url('user'), headers=apiutils.generate_headers( 'owner', 'GET', action, '')) handle_userlist_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def test_generate_header_owner(mocked_owner_apikey, mocked_owner_apikey_id): mocked_owner_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH mocked_owner_apikey_id.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH headers = apiutils.generate_headers(api_key_type='owner', body='', method="GET", action="action") assert 'Date' in headers assert 'authorization-api-key' in headers assert misc_ex.TEST_APIKEY_WITH_VALID_LENGTH in headers[ 'authorization-api-key']
def add_user_to_team(team_id, user_id): """ Add user with the provided user_id to team with provided team_id. """ headers = apiutils.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_id # we want to add here {'id': user_id} ] } } headers = apiutils.generate_headers('rw') try: response = requests.patch(url, json=params, headers=headers) if response_error(response): # Check response has no errors print 'Adding user to team with id: %s failed, status code: %d' \ % (team_id, response.status_code) exit(1) elif response.status_code == 200: print "Added user with id: '%s' to team" % user_id except requests.exceptions.RequestException as error: print error exit(1) elif response_error(response): print 'Cannot find team. Adding user to team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) exit(1) except requests.exceptions.RequestException as error: print error exit(1)
def get_team(team_id): """ Get a specific team. """ headers = apiutils.generate_headers('rw') params = {'teamid': team_id} try: response = requests.get(_url() + "/" + team_id, params=params, headers=headers) handle_get_teams_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def add_existing_user(user_id): """ Add a user that already exist to the current account. """ url = _url('user') + '/' + str(user_id) action = url.split("com/")[1] headers = apiutils.generate_headers('owner', method='POST', action=action, body='') try: response = requests.request('POST', url, data='', headers=headers) handle_create_user_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def delete_user_from_team(team_id, user_id): """ Delete a user from a team. """ headers = apiutils.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_id] } } headers = apiutils.generate_headers('rw') try: response = requests.put(url, json=params, headers=headers) if response_error(response): # Check response has no errors print 'Deleting user from team with id: %s failed, status code: %d' \ % (team_id, response.status_code) exit(1) elif response.status_code == 200: print "Deleted user with id: '%s' from team: %s" % (user_id, team_id) except requests.exceptions.RequestException as error: print error exit(1) elif response_error(response): print 'Cannot find team. Deleting user from team %s failed, ' \ 'status code: %d' % (team_id, response.status_code) exit(1) except requests.exceptions.RequestException as error: print error exit(1)
def list_users(): """ List users that is in the current account. """ action = 'management/accounts/' + str( apiutils.get_account_resource_id()) + '/users' try: response = requests.request('GET', _url('user'), headers=apiutils.generate_headers( 'owner', 'GET', action, '')) handle_userlist_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def get_recent_events(log_keys, last_x_seconds=200, expand=False): """ Get recent events belonging to provided log_keys in the last_x_seconds. """ to_ts = int(time.time()) * 1000 from_ts = (int(time.time()) - last_x_seconds) * 1000 leql = {"during": {"from": from_ts, "to": to_ts}, "statement": "where(/.*/)"} payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response, expand) except requests.exceptions.RequestException as error: print error exit(1)
def delete_team(team_id): """ Delete a team with the provided team ID. """ url = _url() + '/' + team_id headers = apiutils.generate_headers('rw') try: response = requests.delete(url, headers=headers) if response_error(response): # Check response has no errors print 'Delete team failed, status code: %d' % response.status_code exit(1) elif response.status_code == 204: print 'Deleted team with id: %s' % team_id except requests.exceptions.RequestException as error: print error exit(1)
def get_recent_events(log_keys, last_x_seconds=200): """ Get recent events belonging to provided log_keys in the last_x_seconds. """ to_ts = int(time.time()) * 1000 from_ts = (int(time.time()) - last_x_seconds) * 1000 leql = {"during": {"from": from_ts, "to": to_ts}, "statement": "where(/.*/)"} payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def delete_user(user_id): """ Delete a user from the current account. """ url = _url('user') + '/' + str(user_id) action = url.split("com/")[1] headers = apiutils.generate_headers('owner', method='DELETE', action=action, body='') try: response = requests.request('DELETE', url, data='', headers=headers) if response_error(response) is True: # Check response has no errors print 'Delete user failed, status code: ' + str(response.status_code) exit(1) elif response.status_code == 204: print 'Deleted user' except requests.exceptions.RequestException as error: print error exit(1)
def create_team(name): """ Add a new user to the current account. """ params = {'team': {'name': str(name), 'users': []}} headers = apiutils.generate_headers('rw') try: response = requests.post(_url(), json=params, headers=headers) if response_error(response): print 'Creating team failed, status code: %d' % response.status_code exit(1) elif response.status_code == 201: print 'Team created with name: %s' % name except requests.exceptions.RequestException as error: print error exit(1)
def add_new_user(first_name, last_name, email): """ Add a new user to the current account. """ action = 'management/accounts/' + str(apiutils.get_account_resource_id()) + '/users' body = { "email": str(email), "first_name": str(first_name), "last_name": str(last_name) } body = json.dumps(body, separators=(',', ':')) headers = apiutils.generate_headers('owner', method='POST', action=action, body=body) try: response = requests.request('POST', _url('user'), data=body, headers=headers) handle_create_user_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def get_events(log_keys, time_from=None, time_to=None, date_from=None, date_to=None): """ Get events belonging to log_keys and within the time range provided. """ if date_from is not None and date_to is not None: from_ts = int(time.mktime(time.strptime(date_from, "%Y-%m-%d %H:%M:%S"))) * 1000 to_ts = int(time.mktime(time.strptime(date_to, "%Y-%m-%d %H:%M:%S"))) * 1000 else: from_ts = time_from * 1000 to_ts = time_to * 1000 leql = {"during": {"from": from_ts, "to": to_ts}, "statement": "where(/.*/)"} payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def post_query(log_keys, query_string, time_from=None, time_to=None, date_from=None, date_to=None): """ Post query to Logentries. """ if date_from is not None and date_to is not None: from_ts = int(time.mktime(time.strptime(date_from, "%Y-%m-%d %H:%M:%S"))) * 1000 to_ts = int(time.mktime(time.strptime(date_to, "%Y-%m-%d %H:%M:%S"))) * 1000 else: from_ts = time_from * 1000 to_ts = time_to * 1000 leql = {"during": {"from": from_ts, "to": to_ts}, "statement": query_string} payload = {"logs": log_keys, "leql": leql} try: response = requests.post(_url('logs'), headers=apiutils.generate_headers('rw'), json=payload) handle_response(response) except requests.exceptions.RequestException as error: print error exit(1)
def delete_user(user_id): """ Delete a user from the current account. """ url = _url('user') + '/' + str(user_id) action = url.split("com/")[1] headers = apiutils.generate_headers('owner', method='DELETE', action=action, body='') try: response = requests.request('DELETE', url, data='', headers=headers) if response_error(response) is True: # Check response has no errors print 'Delete user failed, status code: ' + str( response.status_code) exit(1) elif response.status_code == 204: print 'Deleted user' except requests.exceptions.RequestException as error: print error exit(1)
def create_team(name): """ Add a new user to the current account. """ params = { 'team': { 'name': str(name), 'users': [] } } headers = apiutils.generate_headers('rw') try: response = requests.post(_url(), json=params, headers=headers) if response_error(response): print 'Creating team failed, status code: %d' % response.status_code exit(1) elif response.status_code == 201: print 'Team created with name: %s' % name except requests.exceptions.RequestException as error: print error exit(1)
def test_generate_headers_user_agent(mocked_ro_apikey): mocked_ro_apikey.return_value = misc_ex.TEST_APIKEY_WITH_VALID_LENGTH headers = apiutils.generate_headers(api_key_type='ro') assert "User-Agent" in headers assert headers['User-Agent'] == 'lecli'