Skip to content

Instantly share code, notes, and snippets.

@fprimex
Forked from skierkowski/tfe-push.sh
Last active February 17, 2018 16:34
Show Gist options
  • Save fprimex/4eb948741b56f870f16b67dee22cc92e to your computer and use it in GitHub Desktop.
Save fprimex/4eb948741b56f870f16b67dee22cc92e to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <path_to_content_directory> <organization>/<workspace>"
exit 0
fi
CONTENT_DIRECTORY=$1
ORG_NAME="$(cut -d'/' -f1 <<<"$2")"
WORKSPACE_NAME="$(cut -d'/' -f2 <<<"$2")"
echo "=====> Creating file for upload"
# Creates a tar.gz of the director with the configuration
UPLOAD_FILE_NAME="./content-$(date +%s).tar.gz"
tar -zcvf $UPLOAD_FILE_NAME $CONTENT_DIRECTORY
echo "=====> Looking up Workspace ID for '$WORKSPACE_NAME' in '$ORG_NAME' org"
# Gets the workspace ID given the organization name and workspace name
# This step can be skipped if you know the workspace ID
WORKSPACE_ID=($(curl \
--header "Authorization: Bearer $ATLAS_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
https://atlas.hashicorp.com/api/v2/organizations/$ORG_NAME/workspaces/$WORKSPACE_NAME \
| jq -r '.data.id'))
echo "=====> Creating a new configuration version for workspace $WORKSPACE_ID"
# Creates the JSON Payload used to create the new cnofiguration version
echo '{"data":{"type":"configuration-version"}}' > ./create_config_version.json
# Creates the configuration version and extractes teh upload-url
UPLOAD_URL=($(curl \
--header "Authorization: Bearer $ATLAS_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @create_config_version.json \
https://atlas.hashicorp.com/api/v2/workspaces/$WORKSPACE_ID/configuration-versions \
| jq -r '.data.links.self'))
echo "=====> Uploading content to upload URL"
curl \
--request PUT \
-F "data=@$UPLOAD_FILE_NAME" \
$UPLOAD_URL
echo "=====> Deleting temporary content file"
rm $UPLOAD_FILE_NAME
rm ./create_config_version.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment