ThoughtSpot Python CLI (trcli)

1.14.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to interact with the `trcli` command-line tool from a Python script using the `subprocess` module. It includes a login attempt and a sample command to list users, highlighting the importance of handling credentials securely (e.g., via environment variables) and capturing output.

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

view raw JSON →