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