Created
April 13, 2021 10:14
-
-
Save caiodelgadonew/7f8d3b5b6268629ea70040f5b18ba92e to your computer and use it in GitHub Desktop.
Terraform Enterprise/Cloud list workspaces by version
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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code lists all workspaces with the desired terraform version.