Skip to content

Instantly share code, notes, and snippets.

@caiodelgadonew
Created April 13, 2021 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caiodelgadonew/7f8d3b5b6268629ea70040f5b18ba92e to your computer and use it in GitHub Desktop.
Save caiodelgadonew/7f8d3b5b6268629ea70040f5b18ba92e to your computer and use it in GitHub Desktop.
Terraform Enterprise/Cloud list workspaces by version
#!/bin/bash
# Please export the tfe/cloud token, url, organization and the Terraform version that you want to search
# export TF_TOKEN='<API_TOKEN>'
# export TF_URL='<SERVER_URL>'
# export TF_ORG='<ORGANIZATION>'
# export TF_VERSION='<VERSION_OF_TERRAFORM>'
function get_pages {
PAGES=$(curl -s\
--header "Authorization: Bearer $TF_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
$TF_URL/api/v2/organizations/$TF_ORG/workspaces\?page\[size\]\=100 | jq '.meta["pagination"]["total-pages"]' )
}
function list_tfe_versions {
for i in $(seq 1 $PAGES):
do
curl -s \
--header "Authorization: Bearer $TF_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
$TF_URL/api/v2/organizations/$TF_ORG/workspaces\?page\[number\]\=${i}\&page\[size\]\=100 | jq '.data[]["attributes"] | select(."terraform-version" == "'${TF_VERSION}'") | {workspace: .name, "tf-version": ."terraform-version"}'
done
}
if [[ -z $TF_TOKEN || -z $TF_URL || -z $TF_ORG || -z $TF_VERSION ]] ;
then
echo "One or more variables are undefined!"
echo "Please be sure to define TF_TOKEN, TF_URL, TF_ORG, TF_VERSION before running the script"
else
get_pages
list_tfe_versions
fi
@caiodelgadonew
Copy link
Author

This code lists all workspaces with the desired terraform version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment