Last active
June 26, 2024 04:34
-
-
Save quangnhut123/0f34acd137a3d6c799fef2e580143809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TOKEN="TFC_TOKEN" | |
ORG_NAME="ORG_NAME" | |
PAGE_SIZE=20 | |
# Function to fetch workspaces for a specific page number | |
function fetch_workspaces_page { | |
local page="$1" | |
curl -s -H "Authorization: Bearer $TOKEN" \ | |
-H "Content-Type: application/vnd.api+json" \ | |
"https://app.terraform.io/api/v2/organizations/$ORG_NAME/workspaces?page%5Bnumber%5D=$page&page%5Bsize%5D=$PAGE_SIZE" | |
} | |
# Step 1: Fetch the first page of workspaces | |
workspaces=$(fetch_workspaces_page 1) | |
total_pages=$(echo "$workspaces" | jq -r '.meta.pagination["total-pages"]') | |
# Step 2: Iterate through each page and get Terraform version for each workspace | |
for ((page=1; page<=total_pages; page++)); do | |
workspaces=$(fetch_workspaces_page $page) | |
workspace_ids=$(echo "$workspaces" | jq -r '.data[].id') | |
for workspace_id in $workspace_ids; do | |
workspace_details=$(curl -s -H "Authorization: Bearer $TOKEN" \ | |
-H "Content-Type: application/vnd.api+json" \ | |
"https://app.terraform.io/api/v2/workspaces/$workspace_id") | |
terraform_version=$(echo "$workspace_details" | jq -r '.data.attributes."terraform-version"') | |
workspace_name=$(echo "$workspace_details" | jq -r '.data.attributes.name') | |
echo "Workspace: $workspace_name, Terraform Version: $terraform_version" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment