managed/api-examples/python-simple/create-universe.ipynb
First, import the required packages.
Next, specify some important variables:
platform_address: The address of the Yugabyte Platform APIplatform_api_key: The API key used to authenticate with the Platform APIFinally, open a HTTP connection to the Yugabyte Platform API.
import os
import http.client
import json
from pprint import pprint
platform_address = os.getenv('API_BASE_URL', "portal.dev.yugabyte.com")
platform_api_key = os.getenv('YB_API_KEY')
conn = http.client.HTTPConnection(f"{platform_address}")
headers = {
'Content-Type': "application/json",
'X-AUTH-YW-API-TOKEN': f"{platform_api_key}"
}
Make an API call to session endpoint to determine customer UUID.
conn.request("GET", "/api/v1/session_info", headers=headers)
res = conn.getresponse()
data = res.read()
session_info = json.loads(data)
customer_uuid = session_info['customerUUID']
print('Customer UUID:\n%s' % customer_uuid)
Make an API call to the provider endpoint to determine provider UUID and regions.
provider_name = 'my-aws-provider' # my-gcp-rovider
url = f"/api/v1/customers/{customer_uuid}/providers"
conn.request("GET", url, headers=headers)
res = conn.getresponse()
provider_list = json.load(res)
for provider in provider_list:
if provider['name'] == provider_name and provider['active'] == True:
provider_uuid = provider['uuid']
region_list = [region['uuid'] for region in provider['regions']]
break
print('Provider UUID:\n%s' % provider_uuid)
print('Regions:\n[%s]' % ', '.join(region_list))
Make an API call to the access key endpoint to determine access key for provider.
url = f"/api/v1/customers/{customer_uuid}/providers/{provider_uuid}/access_keys"
conn.request("GET", url, headers=headers)
res = conn.getresponse()
access_key_list = json.load(res)
access_key_code = access_key_list[0]['idKey']['keyCode']
print('Access Key:\n%s' % access_key_code)
Make an API call to see the instance types available for the provider.
url = f"/api/v1/customers/{customer_uuid}/providers/{provider_uuid}/instance_types"
conn.request("GET", url, headers=headers)
res = conn.getresponse()
instance_type = None
for i in json.load(res):
print(i['instanceTypeCode'])
if i['instanceTypeCode'] in ['c5.large', 'n1-standard-1']: # desired aws or gcp type code
print('^^^^^^^ Found !!!!!!!!!!!')
instance_type = i
break
pprint(instance_type['instanceTypeDetails'])
In this example, we will create a GCP universe. Define a Universe object with the desired configuration.
new_universe = {
'clusters': [
{
'clusterType': 'PRIMARY',
'userIntent': {
'universeName': 'my-aws-universe', # or my-gcp-universe
'providerType': 'aws',
'provider': provider_uuid,
'regionList': region_list,
'numNodes': 3,
'replicationFactor': 3,
'instanceType': instance_type['instanceTypeCode'],
'deviceInfo': {
'numVolumes': 1,
'volumeSize': instance_type['instanceTypeDetails']['volumeDetailsList'][0]['volumeSizeGB'],
'storageType': 'GP2', # 'storageType': 'Persistent',
},
'assignPublicIP': True,
'useTimeSync': True,
'enableYSQL': True,
'enableYEDIS': False,
'enableNodeToNodeEncrypt': True,
'enableClientToNodeEncrypt': True,
'enableVolumeEncryption': False,
'ybSoftwareVersion': '2.7.3.0-b80',
'accessKeyCode': access_key_code,
'tserverGFlags': {},
'masterGFlags': {},
}
},
],
}
Make API call to create new universe.
url = f"/api/v1/customers/{customer_uuid}/universes/clusters"
conn.request("POST", url, json.dumps(new_universe), headers)
res = conn.getresponse()
pprint(json.load(res))