Skip to content

Instantly share code, notes, and snippets.

@ericreeves
Last active August 21, 2023 20:43
Show Gist options
  • Select an option

  • Save ericreeves/8451833d00c42d0e271c5bd351c37afd to your computer and use it in GitHub Desktop.

Select an option

Save ericreeves/8451833d00c42d0e271c5bd351c37afd to your computer and use it in GitHub Desktop.

Revisions

  1. ericreeves revised this gist Aug 21, 2023. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions tfe_list_all_workspaces.sh
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,17 @@
    # 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"
  2. ericreeves created this gist Aug 21, 2023.
    37 changes: 37 additions & 0 deletions tfe_list_all_workspaces.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/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"

    # 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')