Back to Yugabyte Db

Create Universe

managed/api-examples/python-simple/create-universe.ipynb

2026.1.0.0-b254.1 KB
Original Source

Setup

First, import the required packages.

Next, specify some important variables:

  • platform_address: The address of the Yugabyte Platform API
  • platform_api_key: The API key used to authenticate with the Platform API

Finally, open a HTTP connection to the Yugabyte Platform API.

python
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}"
}

Get Session Info

Make an API call to session endpoint to determine customer UUID.

python
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)

Get Provider UUID By Name

Make an API call to the provider endpoint to determine provider UUID and regions.

python
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))

Get Access Key

Make an API call to the access key endpoint to determine access key for provider.

python
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)

Get Instance Types Available

Make an API call to see the instance types available for the provider.

python
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'])

Define Universe

In this example, we will create a GCP universe. Define a Universe object with the desired configuration.

python
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': {},
            }
        },
    ],
}

Create Universe

Make API call to create new universe.

python
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))