def get_dashdb_credentials(region, instance_guid, access_token): """ Get credentials call 'ml-service' for a Dashdb instance given instance ID. Returns: -------- Tuple This tuple has three elements: 1st element: host of this dashdb instance 2nd element: username of this dashdb instance 3rd element: password of this dashdb instance Raises: ------ UnknownCloudFoundryError """ #firstly, try to get all service keys in that region. #if a service key called 'ml-service' exists, #we just return that service key instead of creating a new one all_credentials_res = url(regions[region] + '/v2/service_keys', 'GET', access_token) if all_credentials_res.status_code != 200: raise e.UnknownCloudFoundryError(msg='Retrieving all Dashdb credentials in region '+region \ +' failed. Reason: '+str(all_credentials_res.json())) else: all_credentials = all_credentials_res.json()['resources'] for credential in all_credentials: if credential['entity']['name'] == 'ml-service': #if a service key call 'ml-service' exsits #,since it could be credentials for other instance we used before, #we have to delete it. guid = credential['metadata']['guid'] try_delete = url(regions[region] + '/v2/service_keys/' + guid, 'DELETE', access_token) if try_delete.status_code != 204: raise e.UnknownCloudFoundryError( msg='Deleting exsiting Dashdb credentials' + ' failed. Reason: ' + str(try_delete.json())) #if the service key does not exist, create it credentials_res = url(regions[region] + '/v2/service_keys', 'POST', access_token=access_token, json={ 'service_instance_guid': instance_guid, 'name': 'ml-service' }) if credentials_res.status_code != 201: raise e.UnknownCloudFoundryError(msg='Creating Dashdb credentials in region '+region \ +' failed. Reason: '+str(credentials_res.json())) else: credentials = credentials_res.json()['entity']['credentials'] return (credentials['host'], credentials['username'], credentials['password'])
def get_dashdb_instances_in_region(region, token): """ Get user's exsiting Dashdb service instances in a specific region Returns: ------- list A list of Dashdb instances. Each item of the list is a tuple. It has two elements: first element: name of Dashdb instance second element: id of the Dashdb instance Raises: ------ UnknownCloudFoundryError """ #firstly, we have to get all Dashdb service plans in that region dashdb_service_plan_url = dashdb_service_plans_url[region] dashdb_service_plan_raw = url(regions[region] + dashdb_service_plan_url, access_token=token) if dashdb_service_plan_raw.status_code != 200: raise e.UnknownCloudFoundryError(msg='Retrieving Dashdb service plans in region '+region \ +' failed. Reason: '+str(dashdb_service_plan_raw.json())) else: dashdb_services_plan_guids = [] #this list stores all Dashdb #service plans in that region for item in dashdb_service_plan_raw.json()['resources']: guid = item['metadata']['guid'] dashdb_services_plan_guids.append(guid) dashdb_instances = [] #this list stores all Dashdb #instances in that region for guid in dashdb_services_plan_guids: dashdb_instance_details_res = url(regions[region]+'/v2/service_plans/'+\ guid+'/service_instances', access_token=token) if dashdb_instance_details_res.status_code != 200: raise e.UnknownCloudFoundryError(msg='Retrieving Dashdb service instances in region '+region \ +' failed. Reason: '+str(dashdb_instance_details_res.json())) else: dashdb_instance_details = dashdb_instance_details_res.json( )['resources'] guid_name = [] #this list stores Dashdb #instances for a specific service plan for each in dashdb_instance_details: guid_name.append( (each['entity']['name'], each['metadata']['guid'])) dashdb_instances += guid_name return dashdb_instances
def create_service(region, name, service_plan, space_guid, access_token): """ Create a service instance in a Bluemix space. Returns: -------- str ID of the service created Raises: ------ UnknownCloudFoundryError """ api = regions[region] + '/v2' create = url(api + '/service_instances?accepts_incomplete=true', method='POST', access_token=access_token, json={ 'name': name, 'service_plan_guid': service_plan, 'space_guid': space_guid }) if create.status_code != 201: raise e.UnknownCloudFoundryError(msg='Create service ' + name + ' failed. Reason: ' + str(create.json())) else: return create.json()['metadata']['guid']
def bind_service(region, service_guid, app_guid, access_token): """ Bind a service instance with an app. Returns: -------- dict This dict contains credentials to access the service instance Raises: ------ UnknownCloudFoundryError """ api = regions[region] + '/v2' bind = url(api + '/service_bindings', method='POST', access_token=access_token, json={ 'service_instance_guid': service_guid, 'app_guid': app_guid }) if bind.status_code != 201: raise e.UnknownCloudFoundryError(msg='Bind service failed. Reason: ' + str(bind.json())) else: return bind.json()['entity']['credentials']
def create_app(region, space_guid, app_name, access_token): """ Create an app in a Bluemix space. Returns: -------- str ID of the app created Raises: ------ UnknownCloudFoundryError """ api = regions[region] + '/v2' create_res = url(api + '/apps', 'POST', access_token=access_token, json={ 'name': app_name, 'space_guid': space_guid }) if create_res.status_code != 201: raise e.UnknownCloudFoundryError(msg='Create app '+app_name+ \ ' failed. Reason: '+str(create_res.json())) else: return create_res.json()['metadata']['guid']
def get_spaces(region, org_guid, access_token): """ Get account's spaces in specific organization. Params: ------- region: str Region cloud be 'US', 'United Kingdom', 'Sydney' org_guid: str ID of the organization where we want to retrieve spaces access_token: str The token used to authenticate HTTP request Returns: ------ list A list of spaces. Each item of the list is a dict. It has two keys: name: name of the space guid: id of the space Raises: ------ UnknownCloudFoundryError """ api = regions[region] + '/v2' req_spaces = url(api + '/organizations/' + org_guid + '/spaces', 'GET', access_token) if req_spaces.status_code != 200: raise e.UnknownCloudFoundryError(msg='Get spaces in region '+region \ +' failed. Reason: '+str(req_spaces.json())) else: req_spaces = req_spaces.json()['resources'] spaces = [] for item in req_spaces: name_guid = { 'name': item['entity']['name'], 'guid': item['metadata']['guid'] } spaces.append(name_guid) return spaces