Path: blob/main/L4assets/DSandMLOpsAssets/CLIandSDK/packages/cpdalllibs/cpdaaslib/usermgmt.py
1928 views
import requests12from cpdalllibs.cpdaaslib.constants import *34def getUsers(headersAPI, account_id) :5'''Get all the users that are part of the account'''6all_users = []7resp = requests.get(USER_MGMT_ENDPOINT + '/v2/accounts/{}/users'.format(account_id), headers=headersAPI)8print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))9resp_json = resp.json()10resp_json0 = resp_json11#all_users = [ {'iam_id': name['iam_id'], 'user_id': name['user_id'], 'state': name['state']} for name in resp_json['resources']]12all_users = [{k: name[k] for k in ('iam_id', 'user_id', 'state', 'added_on')} for name in resp_json['resources']]1314while ('next_url' in resp_json):15resp = requests.get(USER_MGMT_ENDPOINT + resp_json['next_url'], headers=headersAPI)16resp_json = resp.json()17all_users = all_users + [{k: name[k] for k in ('iam_id', 'user_id', 'state', 'added_on')} for name in resp_json['resources']]18return(all_users)1920def inviteUser(headersAPI,account_id, data) :21"""Invite a user to an account"""22url = USER_MGMT_ENDPOINT + '/v2/accounts/{}/users'.format(account_id)23resp = requests.post(url, json=data, headers=headersAPI)24return(resp)2526def removeUser(headersAPI, iam_id, account_id) :27"""Remove a user from the account"""28url = USER_MGMT_ENDPOINT + '/v2/accounts/{}/users/{}'.format(account_id,iam_id)29resp = requests.delete(url, headers=headersAPI)30return(resp)3132def getAccessGroups(headersAPI, account_id) :33"""Get all access groups"""34limit = 100 # max value: 100, default 2035access_group_json = []36resp = requests.get(IAM_ENDPOINT +37'/v2/groups?account_id={}&limit={}'.format(account_id,limit),38headers=headersAPI)39if resp.status_code > 202 : # if error40print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))41else :42resp_json = resp.json()43access_group_json = resp_json['groups']44while 'next' in resp_json :45resp = requests.get(resp_json['next']['href'], headers=headersAPI)46resp_json = resp.json()47access_group_json.extend(resp_json['groups'])48return access_group_json4950def getGroupMembers(headersAPI, group_id) :51"""Get an access group members"""52url = IAM_ENDPOINT + '/v2/groups/{}/members'.format(group_id)53limit = 100 # max value: 100, default 2054members_json = []55resp = requests.get(url, headers=headersAPI)56if resp.status_code > 202 : # if error57print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))58else :59resp_json = resp.json()60members_json = resp_json['members']61while 'next' in resp_json :62resp = requests.get(resp_json['next']['href'], headers=headersAPI)63resp_json = resp.json()64members_json.extend(resp_json['members'])65return members_json6667def getGroupRules(headersAPI, group_id) :68"""Get the rules part of a group"""69url = IAM_ENDPOINT + '/v2/groups/{}/rules'.format(group_id)70resp = requests.get(url, headers=headersAPI)71return(resp)727374def cre8Group(headersAPI, account_id, group_name, group_description="") :75"""Create an access group"""76data = {77"name": group_name,78"description": group_description79}80url = IAM_ENDPOINT + '/v2/groups?account_id={}'.format(account_id)81resp = requests.post(url, json=data, headers=headersAPI)82return(resp)8384def deleteGroup(headersAPI, group_id) :85"""Remove an access group from the account"""86url = IAM_ENDPOINT + '/v2/groups/{}'.format(group_id)87resp = requests.delete(url, headers=headersAPI)88return(resp)8990def getRoles(headersAPI) :91'''Get the roles for the account'''92resp = requests.get(IAM_ENDPOINT + '/v2/roles', headers=headersAPI)93if resp.status_code > 202 : # if error94print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))95return(resp.json())969798