|
| 1 | +import os |
| 2 | +import requests.exceptions |
| 3 | +import json |
| 4 | + |
| 5 | +from thoughtspot_rest_api_v1 import * |
| 6 | + |
| 7 | +# |
| 8 | +# Short script to show how to use the connection_create and connection_update commands |
| 9 | +# Documentation on exact properties / options: https://developers.thoughtspot.com/docs/?pageid=connections-api#connection-metadata |
| 10 | +# |
| 11 | + |
| 12 | +username = os.getenv('username') # or type in yourself |
| 13 | +password = os.getenv('password') # or type in yourself |
| 14 | +server = os.getenv('server') # or type in yourself |
| 15 | + |
| 16 | +ts: TSRestApiV1 = TSRestApiV1(server_url=server) |
| 17 | +try: |
| 18 | + ts.session_login(username=username, password=password) |
| 19 | +except requests.exceptions.HTTPError as e: |
| 20 | + print(e) |
| 21 | + print(e.response.content) |
| 22 | + |
| 23 | + |
| 24 | +# You should instead implement the secure retrieval of the password for the connection |
| 25 | +def securely_retrieve_connection_pw(): |
| 26 | + conn_pw = os.getenv('connection_password') |
| 27 | + return conn_pw |
| 28 | + |
| 29 | + |
| 30 | +# See the connection types possible if you need them. 'name' is what is used in the connection_type= of create/update |
| 31 | +conn_types = ts.connection_types() |
| 32 | +for conn_type in conn_types: |
| 33 | + print(conn_type['name'], conn_type['displayName']) |
| 34 | + |
| 35 | +# The actual properties of the connection are set using the connection metadata |
| 36 | +# https://developers.thoughtspot.com/docs/?pageid=connections-api#connection-metadata |
| 37 | +connection_metadata = { |
| 38 | + 'accountName': 'myAccountName', |
| 39 | + 'user': 'myUser', |
| 40 | + 'password': securely_retrieve_connection_pw(), |
| 41 | + 'role': 'yourRole', |
| 42 | + 'warehouse': 'SnowflakeWarehouse' |
| 43 | +} |
| 44 | +# Convert the dict into a JSON string |
| 45 | +conn_json = json.dumps(connection_metadata) |
| 46 | +# Create the connection. use_internal_endpoint=True for software versions prior to 7.1 |
| 47 | +response = ts.connection_create(connection_name='My Great Connection', connection_type="RDBMS_SNOWFLAKE", |
| 48 | + metadata_json=conn_json, description='Connection Descr', |
| 49 | + create_without_tables=True, use_internal_endpoint=False) |
| 50 | + |
| 51 | +new_connection_guid = response['header']['id'] |
| 52 | + |
| 53 | +# connection_update uses basically the same form, but you specify the GUID of the existing connection to update |
| 54 | +ts.connection_update(connection_guid=new_connection_guid, |
| 55 | + connection_name='My Even Greater Connection', connection_type="RDBMS_SNOWFLAKE", |
| 56 | + metadata_json=conn_json, description='Connection Descr 2', |
| 57 | + create_without_tables=True, use_internal_endpoint=False) |
0 commit comments