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/services.py
1928 views
1
import requests
2
from requests.structures import CaseInsensitiveDict
3
from cpdalllibs.cpdaaslib.constants import *
4
import json
5
6
def listGlobalCatalog(headersAPI, limit) :
7
'''List some entries from the global catalog'''
8
endpoint = GLOBAL_CATALOG_ENDPOINT + \
9
'?limit={}&languages=en-us&q=tag:watson+kind:service'.format(limit)
10
resp = requests.get(endpoint, headers=headersAPI)
11
return resp
12
13
def getServiceInstances(headersAPI) :
14
'''Get all the services part of this account'''
15
resp = requests.get(RESOURCE_ENDPOINT + '/v2/resource_instances', headers=headersAPI)
16
if resp.status_code > 202 :
17
print("Status code: {}, reason: {}\n".format(resp.status_code,resp.reason))
18
return resp.json()
19
20
def getAccessGroups(headersAPI, account_id) :
21
limit = 100 # max value: 100
22
access_group_json = []
23
resp = requests.get(IAM_ENDPOINT +
24
'/v2/groups?account_id={}&limit={}'.format(account_id,limit),
25
headers=headersAPI)
26
if resp.status_code > 202 : # if error
27
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
28
else :
29
resp_json = resp.json()
30
access_group_json = resp_json['groups']
31
while 'next' in resp_json :
32
resp = requests.get(resp_json['next']['href'], headers=headersAPI)
33
resp_json = resp.json()
34
access_group_json.extend(resp_json['groups'])
35
return access_group_json
36
37
def getServiceIds(headersAPI, account_id) :
38
pagesize=100 # max value, default 20
39
service_ids = []
40
resp = requests.get(IAM_ENDPOINT + '/v1/serviceids?account_id={}&pagesize={}'.format(account_id,pagesize),
41
headers=headersAPI)
42
resp_json = resp.json()
43
if resp.status_code > 202 : # if error
44
print("Status code: {}, reason: {}".format(resp.status_code,resp.reason))
45
print(json.dumps(resp_json, indent=2, sort_keys=True))
46
else :
47
resp_json = resp.json()
48
service_ids = resp_json['serviceids']
49
while 'next' in resp_json :
50
resp = requests.get(resp_json['next'], headers=headersAPI)
51
resp_json = resp.json()
52
service_ids.extend(resp_json['serviceids'])
53
return(service_ids)
54
55
def addDVUser(email,DV_header):
56
'''Add user to data virtualization'''
57
payload = {"dvRole": "DV_WORKER", "iam": True, "ibmid": "[email protected]" }
58
payload['ibmid'] = email
59
resp = requests.post(DV_ENDPOINT + '/dbapi/v4/users', json=payload, headers=DV_header)
60
print("{}: Status code: {}, reason: {}".format(email,resp.status_code,resp.reason))
61
if resp.status_code > 204 :
62
print(resp.text)
63
return(resp)
64
65
def addOpenscaleUser(iam_id, account_id, os_guid, headersAPI) :
66
''' Add a user to openscale'''
67
data = {
68
"type": "access",
69
"subjects": [{
70
"attributes": [{
71
"name": "iam_id",
72
"value": iam_id
73
}]
74
}],
75
"roles": [{"role_id": "crn:v1:bluemix:public:iam::::role:Viewer"}],
76
"resources": [{
77
"attributes": [{
78
"name": "accountId",
79
"value": account_id,
80
"operator": "stringEquals"
81
},
82
{
83
"name": "serviceName",
84
"value": "aiopenscale"
85
},
86
{
87
"name": "serviceInstance",
88
"value": os_guid,
89
"operator": "stringEquals"
90
}]
91
}]
92
}
93
resp = requests.post(IAM_ENDPOINT + '/v1/policies', json=data, headers=headersAPI)
94
#print("{}: Status code: {}, reason: {}".format(iam_id,resp.status_code,resp.reason))
95
return(resp)
96