Path: blob/main/L4assets/DSandMLOpsAssets/CLIandSDK/packages/cpdalllibs/cpdaaslib/support.py
1928 views
import requests1from cpdalllibs.cpdaaslib.constants import *23def getToken(key) :4"""Get the access token required to interface with CPDaaS"""5headers = {6'Accept': 'application/json',7'Content-type': 'application/x-www-form-urlencoded'8}9data = "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={}"10resp = requests.post(IAM_ENDPOINT + '/identity/token',11headers=headers, data=data.format(key))1213return(resp)1415def apikeyDetails(API_key, access_token) :16headers = {17'Authorization': 'Bearer ' + access_token,18'IAM-Apikey' : API_key19}20resp = requests.get(IAM_ENDPOINT + '/v1/apikeys/details', headers=headers)21return(resp)2223def accountConfig(headersAPI, account_id) :24endpoint = IAM_ENDPOINT + '/v1/accounts/{}/settings/identity?include_history=false'25resp = requests.get(endpoint.format(account_id), headers=headersAPI)26return(resp)2728def getServiceIDs(headersAPI, account_id) :29endpoint = IAM_ENDPOINT + '/v1/serviceids?pagesize=100&account_id={}'30resp = requests.get(endpoint.format(account_id), headers=headersAPI)31if resp.status_code > 204 :32print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))33return []34resp_json = resp.json()35total = resp_json['serviceids']36while 'next' in resp_json :37resp = requests.get(resp_json['next'], headers=headersAPI)38resp_json = resp.json()39total.extend(resp_json['serviceids'])40return(total)4142