-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_api.py
executable file
·82 lines (70 loc) · 2.28 KB
/
setup_api.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Setup an AMIV API for use with the amiv-checkin tool."""
from sys import argv
import requests
if len(argv) < 5:
print("Usage: %s <API URL> <root password> <admin username> <admin password> <admin group name>" % argv[0])
exit(1)
API_URL_c = str(argv[1])
rootpw_c = str(argv[2])
auth_obj = requests.auth.HTTPBasicAuth(rootpw_c, "")
ADMIN_BN_c = str(argv[3])
ADMIN_PW_c = str(argv[4])
ADMIN_GROUP_c = str(argv[5])
def api_delete(obj):
delurl = API_URL_c + "/" + str(obj['_links']['self']['href'])
headers = {'If-Match': obj['_etag']}
r = requests.delete(delurl, headers=headers, auth=auth_obj)
if r.status_code not in [200, 202, 204]:
print('Error with API access: '+str(r.text))
exit(1)
# create your checkin admin user
data = {
'nethz': ADMIN_BN_c,
'password': ADMIN_PW_c,
'gender': 'male',
'firstname': 'Checkin',
'lastname': 'Admin',
'membership': 'regular',
'email': ADMIN_BN_c + '@amiv.ethz.ch'
}
r = requests.post(API_URL_c + '/users', json=data, auth=auth_obj)
if r.status_code is not 201:
print('Error with API access: '+str(r.text))
exit(1)
adminuser = r.json()
# create checkin admin group
data = {
"name": ADMIN_GROUP_c,
"requires_storage": False,
"allow_self_enrollment": False,
"moderator": str(adminuser['_id']),
"permissions": {
"events": "read",
"users": "read",
"eventsignups": "readwrite"
}
}
r = requests.post(API_URL_c + '/groups', json=data, auth=auth_obj)
if r.status_code is not 201:
print('Error with API access: '+str(r.text))
api_delete(adminuser)
exit(1)
admingroup = r.json()
# make adminuser member of admingroup
data = {
"group": admingroup['_id'],
"user": adminuser['_id']
}
r = requests.post(API_URL_c + '/groupmemberships', json=data, auth=auth_obj)
if r.status_code is not 201:
print('Error with API access: '+str(r.text))
api_delete(admingroup)
api_delete(adminuser)
exit(1)
# output to user
print("Generated Checkin Administrator user and group.")
print("Administrator username: '{:s}' password:'{:s}'".format(ADMIN_BN_c, ADMIN_PW_c))
print("Administrator group name: '{:s}' _id:'{:s}' <<-- copy this into config.py".format(ADMIN_GROUP_c, admingroup['_id']))
print("Setup succeeded.")