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/support.py
1928 views
1
import requests
2
from cpdalllibs.cpdaaslib.constants import *
3
4
def getToken(key) :
5
"""Get the access token required to interface with CPDaaS"""
6
headers = {
7
'Accept': 'application/json',
8
'Content-type': 'application/x-www-form-urlencoded'
9
}
10
data = "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={}"
11
resp = requests.post(IAM_ENDPOINT + '/identity/token',
12
headers=headers, data=data.format(key))
13
14
return(resp)
15
16
def apikeyDetails(API_key, access_token) :
17
headers = {
18
'Authorization': 'Bearer ' + access_token,
19
'IAM-Apikey' : API_key
20
}
21
resp = requests.get(IAM_ENDPOINT + '/v1/apikeys/details', headers=headers)
22
return(resp)
23
24
def accountConfig(headersAPI, account_id) :
25
endpoint = IAM_ENDPOINT + '/v1/accounts/{}/settings/identity?include_history=false'
26
resp = requests.get(endpoint.format(account_id), headers=headersAPI)
27
return(resp)
28
29
def getServiceIDs(headersAPI, account_id) :
30
endpoint = IAM_ENDPOINT + '/v1/serviceids?pagesize=100&account_id={}'
31
resp = requests.get(endpoint.format(account_id), headers=headersAPI)
32
if resp.status_code > 204 :
33
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
34
return []
35
resp_json = resp.json()
36
total = resp_json['serviceids']
37
while 'next' in resp_json :
38
resp = requests.get(resp_json['next'], headers=headersAPI)
39
resp_json = resp.json()
40
total.extend(resp_json['serviceids'])
41
return(total)
42