ThoughtSpot Python CLI (trcli)
The ThoughtSpot Python CLI (trcli) is a utility that enables users to perform administrative tasks, such as managing users, groups, objects, and configurations, on a ThoughtSpot cluster programmatically. It is actively developed by ThoughtSpot, with version 1.14.0 being the latest stable release. Releases typically align with ThoughtSpot platform updates or are issued as needed for bug fixes and new features.
Warnings
- breaking Minimum Python version requirement increased to 3.9.
- breaking Authentication logic updated, potentially requiring re-login or new token generation.
- gotcha Credentials and ThoughtSpot URL are often required and can be passed via environment variables, CLI arguments, or configuration files.
- gotcha Default output format is human-readable, but programmatic interaction often requires JSON output.
Install
-
pip install trcli
Imports
- trcli CLI functionality
from trcli.cli import main # Not a public API for general use
import subprocess import os ts_url = os.environ.get('TS_URL', 'https://your_thoughtspot_instance.com') ts_username = os.environ.get('TS_USERNAME', 'admin') ts_password = os.environ.get('TS_PASSWORD', 'password') # Example: Get trcli version try: result = subprocess.run( ['trcli', 'version'], capture_output=True, text=True, check=True ) print(result.stdout) except subprocess.CalledProcessError as e: print(f"Error running trcli: {e.stderr}") # Example: Login (requires environment variables or --url/--username/--password) try: # Using environment variables is common, ensure they are set # os.environ['TS_URL'] = ts_url # os.environ['TS_USERNAME'] = ts_username # os.environ['TS_PASSWORD'] = ts_password # Note: For security, avoid hardcoding credentials. Use env vars or config. result = subprocess.run( ['trcli', 'login', '--url', ts_url, '--username', ts_username, '--password', ts_password], capture_output=True, text=True, check=True ) print("Login successful:", result.stdout) except subprocess.CalledProcessError as e: print(f"Login failed: {e.stderr}")
Quickstart
import subprocess
import os
# Set ThoughtSpot URL and credentials using environment variables for security
# Or pass them directly as arguments (less secure for passwords)
# Example environment variables (replace with your actual values):
# export TS_URL='https://your-thoughtspot-instance.com'
# export TS_USERNAME='your_username'
# export TS_PASSWORD='your_password'
# You can use os.environ.get for testing or local setup
ts_url = os.environ.get('TS_URL', 'https://localhost') # Use a placeholder or actual URL
ts_username = os.environ.get('TS_USERNAME', 'user') # Use a placeholder or actual username
ts_password = os.environ.get('TS_PASSWORD', 'pass') # Use a placeholder or actual password
print(f"Attempting to use trcli with URL: {ts_url}")
try:
# First, try to login to obtain a session token
# For programmatic use, consider using a service principal or persistent token
login_cmd = [
'trcli', 'login',
'--url', ts_url,
'--username', ts_username,
'--password', ts_password
]
print(f"Running login command: {' '.join(login_cmd[:4])} ...") # Censor password
login_result = subprocess.run(login_cmd, capture_output=True, text=True, check=True)
print("Login successful:")
print(login_result.stdout)
# Now, run a command, e.g., list users, outputting as JSON
list_users_cmd = ['trcli', 'user', 'list', '-o', 'json']
print(f"Running command: {' '.join(list_users_cmd)}")
users_result = subprocess.run(list_users_cmd, capture_output=True, text=True, check=True)
print("Users list (JSON):")
print(users_result.stdout)
except FileNotFoundError:
print("Error: 'trcli' command not found. Ensure trcli is installed and in your PATH.")
except subprocess.CalledProcessError as e:
print(f"Error executing trcli command:\nSTDOUT: {e.stdout}\nSTDERR: {e.stderr}")
except Exception as e:
print(f"An unexpected error occurred: {e}")