This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.py
49 lines (42 loc) · 1.69 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import requests
import os
import time
def handler(event,context):
# For cookies and error handling
session = requests.Session()
session.hooks = {
'response': lambda r, *args, **kwargs: r.raise_for_status()
}
# Login
login_data = {
"login[email]": os.getenv("USERNAME"),
"login[password]": os.getenv("PASSWORD")
}
login_response = session.post("https://login.newrelic.com/login", data = login_data)
custom_headers = {
"X-Requested-With": "XMLHttpRequest",
"Accept": "application/json"
}
# Get users
users_response = session.get(f"https://user-management.service.newrelic.com/accounts/{os.getenv('ACCOUNT_ID')}/users", headers=custom_headers)
# Determine which users need to be switched back to basic
switch_to_basic = []
for user in users_response.json():
roles = [role['id'] for role in user['roles'] ]
if int(os.getenv("ROLE_ID")) in roles: # check if in the auto role
if user['user_tier_id'] == 0: #check if full role
if user['last_access_at'] + int(os.getenv("TIMEOUT")) < time.time(): # check if the last access + timeout is less than the current timestamp
switch_to_basic.append(user['user_id'])
# Update accounts
for user_id in switch_to_basic:
print(f"Switching {str(user_id)} back to basic")
update_response = session.put(f"https://rpm.newrelic.com/user_management/accounts/{os.getenv('ACCOUNT_ID')}/users/{user_id}",
headers=custom_headers,
json={
"account_view":{
"user_tier_id":1
}
}
)
if __name__ == "__main__":
handler({},{})