Skip to content

Instantly share code, notes, and snippets.

@ericreeves
Last active August 21, 2023 20:43
Show Gist options
  • Save ericreeves/8451833d00c42d0e271c5bd351c37afd to your computer and use it in GitHub Desktop.
Save ericreeves/8451833d00c42d0e271c5bd351c37afd to your computer and use it in GitHub Desktop.
Terraform Enterprise/Cloud - List All Workspaces
#!/bin/bash
#
# Terraform Enterprise/Cloud - List All Workspaces in All Organizations
#
#------------------------------------------
# DESCRIPTION
#------------------------------------------
# This script will list all workspaces in all organizations visible to the user for which the TFE_TOKEN was generated
#
#------------------------------------------
# CONFIGURATION
#------------------------------------------
# These variables can either be hard-coded in this script, or exported prior to execution of the script.
#
# TFE_TOKEN="YOUR_TFE_API_TOKEN"
# TFE_URL="https://app.terraform.io/api/v2"
# Ensure required configuration variables have values
if [ -z ${TFE_TOKEN} ]; then
echo "TFE_TOKEN is unset."
exit 1
fi
if [ -z ${TFE_URL} ]; then
echo "TFE_URL is unset."
exit 1
fi
# Function to list workspaces for an organization
list_workspaces() {
org_name="$1"
workspace_data=$(curl -s -H "Authorization: Bearer $TFE_TOKEN" "$TFE_URL/organizations/$org_name/workspaces")
workspaces=$(echo "$workspace_data" | jq -r '.data[] | .attributes.name')
echo "$workspaces"
}
# Get all organizations
org_data=$(curl -s -H "Authorization: Bearer $TFE_TOKEN" "$TFE_URL/organizations")
while IFS= read -r org_name; do
workspace_list=$(list_workspaces "$org_name")
for workspace_name in $workspace_list; do
echo "$org_name/$workspace_name"
done
done < <(echo "$org_data" | jq -r '.data[] | .attributes.name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment