Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CloudPak-Outcomes
GitHub Repository: CloudPak-Outcomes/Outcomes-Projects
Path: blob/main/L4assets/DSandMLOpsAssets/CLIandSDK/packages/cpdalllibs/cpdaaslib/usermgmt.py
1928 views
1
import requests
2
3
from cpdalllibs.cpdaaslib.constants import *
4
5
def getUsers(headersAPI, account_id) :
6
'''Get all the users that are part of the account'''
7
all_users = []
8
resp = requests.get(USER_MGMT_ENDPOINT + '/v2/accounts/{}/users'.format(account_id), headers=headersAPI)
9
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
10
resp_json = resp.json()
11
resp_json0 = resp_json
12
#all_users = [ {'iam_id': name['iam_id'], 'user_id': name['user_id'], 'state': name['state']} for name in resp_json['resources']]
13
all_users = [{k: name[k] for k in ('iam_id', 'user_id', 'state', 'added_on')} for name in resp_json['resources']]
14
15
while ('next_url' in resp_json):
16
resp = requests.get(USER_MGMT_ENDPOINT + resp_json['next_url'], headers=headersAPI)
17
resp_json = resp.json()
18
all_users = all_users + [{k: name[k] for k in ('iam_id', 'user_id', 'state', 'added_on')} for name in resp_json['resources']]
19
return(all_users)
20
21
def inviteUser(headersAPI,account_id, data) :
22
"""Invite a user to an account"""
23
url = USER_MGMT_ENDPOINT + '/v2/accounts/{}/users'.format(account_id)
24
resp = requests.post(url, json=data, headers=headersAPI)
25
return(resp)
26
27
def removeUser(headersAPI, iam_id, account_id) :
28
"""Remove a user from the account"""
29
url = USER_MGMT_ENDPOINT + '/v2/accounts/{}/users/{}'.format(account_id,iam_id)
30
resp = requests.delete(url, headers=headersAPI)
31
return(resp)
32
33
def getAccessGroups(headersAPI, account_id) :
34
"""Get all access groups"""
35
limit = 100 # max value: 100, default 20
36
access_group_json = []
37
resp = requests.get(IAM_ENDPOINT +
38
'/v2/groups?account_id={}&limit={}'.format(account_id,limit),
39
headers=headersAPI)
40
if resp.status_code > 202 : # if error
41
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
42
else :
43
resp_json = resp.json()
44
access_group_json = resp_json['groups']
45
while 'next' in resp_json :
46
resp = requests.get(resp_json['next']['href'], headers=headersAPI)
47
resp_json = resp.json()
48
access_group_json.extend(resp_json['groups'])
49
return access_group_json
50
51
def getGroupMembers(headersAPI, group_id) :
52
"""Get an access group members"""
53
url = IAM_ENDPOINT + '/v2/groups/{}/members'.format(group_id)
54
limit = 100 # max value: 100, default 20
55
members_json = []
56
resp = requests.get(url, headers=headersAPI)
57
if resp.status_code > 202 : # if error
58
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
59
else :
60
resp_json = resp.json()
61
members_json = resp_json['members']
62
while 'next' in resp_json :
63
resp = requests.get(resp_json['next']['href'], headers=headersAPI)
64
resp_json = resp.json()
65
members_json.extend(resp_json['members'])
66
return members_json
67
68
def getGroupRules(headersAPI, group_id) :
69
"""Get the rules part of a group"""
70
url = IAM_ENDPOINT + '/v2/groups/{}/rules'.format(group_id)
71
resp = requests.get(url, headers=headersAPI)
72
return(resp)
73
74
75
def cre8Group(headersAPI, account_id, group_name, group_description="") :
76
"""Create an access group"""
77
data = {
78
"name": group_name,
79
"description": group_description
80
}
81
url = IAM_ENDPOINT + '/v2/groups?account_id={}'.format(account_id)
82
resp = requests.post(url, json=data, headers=headersAPI)
83
return(resp)
84
85
def deleteGroup(headersAPI, group_id) :
86
"""Remove an access group from the account"""
87
url = IAM_ENDPOINT + '/v2/groups/{}'.format(group_id)
88
resp = requests.delete(url, headers=headersAPI)
89
return(resp)
90
91
def getRoles(headersAPI) :
92
'''Get the roles for the account'''
93
resp = requests.get(IAM_ENDPOINT + '/v2/roles', headers=headersAPI)
94
if resp.status_code > 202 : # if error
95
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
96
return(resp.json())
97
98