|
| 1 | +<!-- search-meta |
| 2 | +tags: [REST-API, user-management, multi-org, org-admin, Python, automation] |
| 3 | +apis: [users/{user_identifier}/update, org_identifiers, auth/token/full, orgs/search, users/search, searchdata, REST-API-v2] |
| 4 | +questions: |
| 5 | + - How do I remove inactive users from a specific ThoughtSpot org without deleting them from the cluster? |
| 6 | + - How do I automate org membership cleanup based on user inactivity? |
| 7 | + - How do I remove a user from one org while keeping them in their other orgs? |
| 8 | + - How do I determine per-org user activity in ThoughtSpot using the BI Server usage data? |
| 9 | +--> |
| 10 | + |
| 11 | +# ts-org-inactive-users-cleanup.py |
| 12 | + |
| 13 | +Removes users from the specified ThoughtSpot org after N days of inactivity in |
| 14 | +that org. Users stay on the cluster and in their other orgs — only |
| 15 | +membership in that one org is removed. Run once per org. |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- Python 3.9+, `pip3 install -r requirements.txt` |
| 20 | +- `TS: BI Server` usage-stats reporting enabled on your cluster (what the |
| 21 | + inactivity check is based on). |
| 22 | +- An account with **"Can administer Org"** rights on the org you're |
| 23 | + processing, via one of: |
| 24 | + - **Trusted Auth `secret_key`** — can be org-scoped on some deployments |
| 25 | + — if you'll run this against several orgs, you may need a different |
| 26 | + key per org. |
| 27 | + - **Username + password** — simpler across several orgs, since one |
| 28 | + account's password works for every org it administers. |
| 29 | + |
| 30 | +## Install |
| 31 | + |
| 32 | +```bash |
| 33 | +python3 -m venv .venv |
| 34 | +source .venv/bin/activate |
| 35 | +pip3 install -r requirements.txt |
| 36 | +``` |
| 37 | + |
| 38 | +Every run below follows the same three steps, in this order: |
| 39 | +**list orgs → dry run → live run**. Always review the dry-run CSV before |
| 40 | +going live. Pick one of the two approaches below for supplying |
| 41 | +credentials/target org — don't mix them for the same run. |
| 42 | + |
| 43 | +## Approach A: `.env` file (recommended) |
| 44 | + |
| 45 | +Keeps `secret_key`/password out of your shell history and out of `ps` |
| 46 | +output, which passing them as CLI flags would not. |
| 47 | + |
| 48 | +```bash |
| 49 | +cp .env.example .env |
| 50 | +``` |
| 51 | + |
| 52 | +Open `.env` and fill in `TS_CLUSTER_URL`, **one** of the two auth methods |
| 53 | +above, and `TS_ORG_ID` (find it with step 1 below if you don't have it |
| 54 | +yet). `.env` is gitignored — never commit your real one. |
| 55 | + |
| 56 | +```bash |
| 57 | +# 1. Find your org ID (skip if you already know it) |
| 58 | +python3 ts-org-inactive-users-cleanup.py --list-orgs |
| 59 | + |
| 60 | +# 2. Dry run — no changes made, writes ts_org_cleanup_audit_<org_id>.csv |
| 61 | +python3 ts-org-inactive-users-cleanup.py --dry-run |
| 62 | + |
| 63 | +# 3. Live run — same command, minus --dry-run. No confirmation prompt |
| 64 | +# (deliberate, so this can run unattended on a schedule/cron). |
| 65 | +python3 ts-org-inactive-users-cleanup.py |
| 66 | +``` |
| 67 | + |
| 68 | +## Approach B: CLI flags (no `.env` file) |
| 69 | + |
| 70 | +Every value normally read from `.env` has a matching flag instead. Useful |
| 71 | +for a one-off run, or when credentials come from another secrets store |
| 72 | +that injects them as arguments. |
| 73 | + |
| 74 | +```bash |
| 75 | +# 1. Find your org ID (skip if you already know it) |
| 76 | +python3 ts-org-inactive-users-cleanup.py \ |
| 77 | + --cluster-url https://your-cluster.thoughtspot.cloud \ |
| 78 | + --username <admin_user> --password '<admin_password>' \ |
| 79 | + --list-orgs |
| 80 | + |
| 81 | +# 2. Dry run — no changes made, writes ts_org_cleanup_audit_111.csv |
| 82 | +python3 ts-org-inactive-users-cleanup.py \ |
| 83 | + --cluster-url https://your-cluster.thoughtspot.cloud \ |
| 84 | + --username <admin_user> --password '<admin_password>' \ |
| 85 | + --org-id 111 --dry-run |
| 86 | + |
| 87 | +# 3. Live run — same command, minus --dry-run. |
| 88 | +python3 ts-org-inactive-users-cleanup.py \ |
| 89 | + --cluster-url https://your-cluster.thoughtspot.cloud \ |
| 90 | + --username <admin_user> --password '<admin_password>' \ |
| 91 | + --org-id 111 |
| 92 | +``` |
| 93 | + |
| 94 | +(Swap `--username`/`--password` for `--secret-key '<key>' --admin-username <user>` |
| 95 | +if you're using Trusted Auth instead of a password.) |
| 96 | + |
| 97 | +Mixing is allowed but resolves by precedence — see **Optional flags** |
| 98 | +below: a real exported env var or an explicit CLI flag always takes |
| 99 | +priority over `.env`. |
| 100 | + |
| 101 | +## Multiple orgs? |
| 102 | + |
| 103 | +Run the script again with a different `--org-id` (this overrides whatever |
| 104 | +`TS_ORG_ID` is set in `.env`) — each run is independent, and its audit |
| 105 | +CSV/log file is named after that org's ID |
| 106 | +(`ts_org_cleanup_audit_<org_id>.csv`), so runs never overwrite each other. |
| 107 | + |
| 108 | +## Optional flags |
| 109 | + |
| 110 | +| Flag | `.env` equivalent | What it does | |
| 111 | +|---|---|---| |
| 112 | +| `--cluster-url <url>` | `TS_CLUSTER_URL` | ThoughtSpot cluster base URL | |
| 113 | +| `--secret-key '<key>'` | `TS_SECRET_KEY` | Trusted Auth key (Method 1) | |
| 114 | +| `--admin-username <user>` | `TS_ADMIN_USERNAME` | Username to mint tokens as (used with `--secret-key`) | |
| 115 | +| `--username <user>` | `TS_USERNAME` | Admin username for password auth (Method 2) | |
| 116 | +| `--password '<password>'` | `TS_PASSWORD` | Admin password for password auth (Method 2) | |
| 117 | +| `--org-id 111` | `TS_ORG_ID` | The org to process | |
| 118 | +| `--inactivity-days 30` | — | Inactivity threshold (default: 10 days) | |
| 119 | +| `--list-orgs` | — | List every org's id + name, then exit | |
| 120 | + |
| 121 | +## Notes |
| 122 | + |
| 123 | +- A live run (no `--dry-run`) removes users immediately with **no |
| 124 | + confirmation prompt** — this is intentional, so the script can be |
| 125 | + scheduled (cron/etc.) without a `yes` prompt blocking unattended runs. |
| 126 | + Always dry-run first. |
| 127 | +- Never removes `tsadmin`, `system`, or `thoughtspot` accounts. |
| 128 | +- The startup banner states exactly which auth method and source (CLI |
| 129 | + flag, a real env var, or `.env` file) is active — check it if a run |
| 130 | + doesn't behave as expected. A stale `TS_SECRET_KEY` in your shell or |
| 131 | + left in `.env` from an earlier setup silently overrides CLI flags |
| 132 | + otherwise. |
0 commit comments