-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsgwsapi.py
More file actions
214 lines (137 loc) · 6.39 KB
/
sgwsapi.py
File metadata and controls
214 lines (137 loc) · 6.39 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import requests
import urllib3
import json
from global_secrets import api_get_url,api_default_group
#Disable warning if not using certificate:
verify=False
if verify !=True :
urllib3.disable_warnings()
def _url(path):
url=api_get_url()
return url + path
#Operations on authorization
#Authenticate:
def get_auth_token(username, password):
auth_json={
"username": username,
"password": password ,
"cookie": "true",
"csrfToken": "false"
}
return requests.post(_url('/api/v3/authorize'), json=auth_json, verify=verify)
def get_tenant_token (username, password, accountId):
auth_json={
"accountId": accountId,
"username": username,
"password": password ,
"cookie": "true",
"csrfToken": "false"
}
return requests.post(_url('/api/v3/authorize'), json=auth_json, verify=verify)
#Operations at tenant level:
#Get tenants accounts
def get_tenants_accounts(authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get (_url('/api/v3/grid/accounts?limit=250'), headers=headers , verify=verify)
def get_tenant_by_name (authtoken, tenant_name):
#Returns the tenant id that match the name
query=get_tenants_accounts(authtoken)
for items in query.json()['data']:
if items['name']==tenant_name:
return items['id']
#Gets the storage usage information for the Storage Tenant Account
def get_storage_usage_in_tenant(tenant_id, authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get (_url('/api/v3/grid/accounts/{}/usage'.format(tenant_id)), headers=headers , verify=verify)
#Creates a new Storage Tenant Account
def create_new_tenant(authtoken, account_name,quota,root_password):
headers={'Authorization': 'Bearer ' + authtoken }
data_json= "{ \"name\":\""+ account_name +"\",\"capabilities\": [\"management\",\"s3\" ],\"policy\": {\"useAccountIdentitySource\": false,\"allowPlatformServices\": false,\"quotaObjectBytes\":"+ str(quota) +"},\"password\": \""+ root_password +"\",\"grantRootAccessToGroup\":\""+ api_default_group()+"\"}"
data=json.loads(data_json)
return requests.post(_url('/api/v3/grid/accounts'), json=data, headers=headers, verify=verify)
#Operations on alarms:
def get_alarms(authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get(_url('/api/v3/grid/alarms'), headers=headers, verify=verify)
def get_health(authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get(_url('/api/v3/grid/health'), headers=headers, verify=verify)
def get_health_topology(authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get(_url('/api/v3/grid/health/topology'), headers=headers, verify=verify)
#Operations on Users
#Lists Grid Administrator Users
def get_admin_users(authtoken):
headers={'Authorization': 'Bearer ' + authtoken }
return requests.get(_url('/api/v3/grid/users'), headers=headers, verify=verify)
#operations on tenants... needs a X-Csrf-Token
def get_usage(tenant_authtoken):
headers={'Authorization': 'Bearer ' + tenant_authtoken }
return requests.get(_url('/api/v3/org/usage'), headers=headers, verify=verify)
#Inside a Tenant
def create_new_tenant_user_group(tenant_authtoken, group_name, bucket_name):
#/org/groups Creates a new Tenant User Group only with access to a specific bucket.
#This group can operate over the bucket but not over the tenant, and can generate S3 keys.
headers={'Authorization': 'Bearer ' + tenant_authtoken }
body={
"displayName": group_name,
"policies": {
"management": {
"manageAllContainers": False,
"manageEndpoints": False,
"manageOwnS3Credentials": True,
"rootAccess": False },
"s3": {
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::"+bucket_name,
"arn:aws:s3:::"+bucket_name+"/*"
],
}
]
}
},
"uniqueName": "federated-group/"+group_name
}
data=body
#For debug:
print (json.dumps(data, indent=1))
return requests.post(_url('/api/v3/org/groups'), json=data, headers=headers, verify=verify)
def create_new_tenant_user_group_noS3access(tenant_authtoken, group_name, bucket_name):
#/org/groups Creates a new Tenant User Group only with access to a specific bucket.
#This group can operate over the bucket but not over the tenant, and can generate S3 keys.
headers={'Authorization': 'Bearer ' + tenant_authtoken }
body={
"displayName": group_name,
"policies": {
"management": {
"manageAllContainers": False,
"manageEndpoints": False,
"manageOwnS3Credentials": True,
"rootAccess": False }
},
"uniqueName": "federated-group/"+group_name
}
data=body
#For debug:
print (json.dumps(data, indent=1))
return requests.post(_url('/api/v3/org/groups'), json=data, headers=headers, verify=verify)
def create_new_bucket(tenant_authtoken,bucket_name, region):
#/org/containers
#Create a bucket for an S3 tenant account
headers={'Authorization': 'Bearer ' + tenant_authtoken }
data={
"name": bucket_name,
"region": region,
}
#For debug:
print (json.dumps(data, indent=1))
return requests.post(_url('/api/v3/org/containers'), json=data, headers=headers, verify=verify)
#/org/containers/{bucketName}/last-access-time
#Determines if LAT is enable on a bucket
def get_last_access_time(tenant_authtoken,bucket_name):
headers={'Authorization': 'Bearer ' + tenant_authtoken }
return requests.get (_url('/api/v3/org/containers/{}/last-access-time'.format(bucket_name)), headers=headers , verify=verify)