-
Notifications
You must be signed in to change notification settings - Fork 3
/
rest_v3_auth.py
76 lines (58 loc) · 2.04 KB
/
rest_v3_auth.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
from const import *
import requests
import json
import sys
import pprint
# http://developer.openstack.org/api-ref/identity/v3/index.html?expanded=authenticate-detail
auth_request = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": USERNAME,
"domain": {
"id": "default"
},
"password": PASSWORD
}
}
},
}
}
auth_url = '{PROTO}://{HOST}:{IDENTITY_PORT}/v3/auth'.format(**CONNECTION)
def do_auth():
resp = requests.post(auth_url + '/tokens', json=auth_request)
if resp.status_code < 200 or resp.status_code >= 300:
print("ERROR: {0}".format(resp.text))
sys.exit()
return resp.json() # Auth token is in response['token']['user']['id']
def get_service_catalog(token):
auth_header = {'X-Auth-Token': token,
'Content-Type': 'application/json'}
resp = requests.get(auth_url + '/catalog', headers=auth_header)
if resp.status_code < 200 or resp.status_code >= 300:
print("ERROR: {0}".format(resp.text))
sys.exit()
return resp.json() # Auth token is in response['token']['user']['id']
def get_token_details(token):
auth_header = {'X-Auth-Token': token,
'X-Subject-Token': token,
'Content-Type': 'application/json'}
resp = requests.get(auth_url + '/tokens', headers=auth_header)
if resp.status_code < 200 or resp.status_code >= 300:
print("ERROR: {0}".format(resp.text))
sys.exit()
return resp.json() # Auth token is in response['token']['user']['id']
if __name__ == '__main__':
print('USER: {0}'.format(USERNAME))
auth_response = do_auth()
token = auth_response['token']['user']['id']
print(' TOKEN: {0}'.format(token))
print(get_token_details(token))
print('-SERVICE CATALOG-')
catalog = get_service_catalog(token)
for service in catalog['catalog']:
print(service)