|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Make sure ATLAS_TOKEN environment variable is set |
| 4 | +# to owners team token for organization |
| 5 | + |
| 6 | +# Set PTFE address, organization, and workspace to create. You should edit these before running. |
| 7 | +address="roger-ptfe.hashidemos.io" |
| 8 | +organization="Solutions-Engineering" |
| 9 | +workspace="workspace-from-api" |
| 10 | + |
| 11 | +# You can change sleep duration if desired |
| 12 | +sleep_duration=15 |
| 13 | + |
| 14 | +# name of person to set name variable to |
| 15 | +name=$1 |
| 16 | + |
| 17 | +# Override soft-mandatory policy checks that fail |
| 18 | +# Set to "yes" or "no" |
| 19 | +# if not specified, then we set to "no" |
| 20 | +if [ ! -z $2 ]; then |
| 21 | + override=$2 |
| 22 | +else |
| 23 | + override="no" |
| 24 | +fi |
| 25 | + |
| 26 | +# build myconfig.tar.gz |
| 27 | +cd config |
| 28 | +tar -cvf myconfig.tar . |
| 29 | +gzip myconfig.tar |
| 30 | +mv myconfig.tar.gz ../. |
| 31 | +cd .. |
| 32 | + |
| 33 | +#Set name of workspace in workspace.json |
| 34 | +sed "s/placeholder/$workspace/" < workspace.template.json > workspace.json |
| 35 | + |
| 36 | +workspace_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @workspace.json "https://${address}/api/v2/organizations/${organization}/workspaces") |
| 37 | + |
| 38 | +# Parse workspace_id from workspace_result |
| 39 | +workspace_id=$(echo $workspace_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])") |
| 40 | + |
| 41 | +echo "Workspace ID: " $workspace_id |
| 42 | + |
| 43 | +# Create configuration versions |
| 44 | +configuration_version_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @configversion.json "https://${address}/api/v2/workspaces/${workspace_id}/configuration-versions") |
| 45 | + |
| 46 | +# Parse configuration_version_id and upload_url |
| 47 | +config_version_id=$(echo $configuration_version_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])") |
| 48 | +upload_url=$(echo $configuration_version_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['upload-url'])") |
| 49 | + |
| 50 | +echo "Config Version ID: " $config_version_id |
| 51 | +echo "Upload URL: " $upload_url |
| 52 | + |
| 53 | +# Upload configuration |
| 54 | +curl --request PUT -F '[email protected]' "$upload_url" |
| 55 | + |
| 56 | +# Add name variable |
| 57 | +sed -e "s/my-name/$name/" -e "s/my-organization/$organization/" -e "s/my-workspace/$workspace/" < variable.template.json > variable.json |
| 58 | + |
| 59 | +upload_variable_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @variable.json "https://${address}/api/v2/vars?filter%5Borganization%5D%5Busername%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}") |
| 60 | + |
| 61 | +# Do a run |
| 62 | +sed "s/workspace_id/$workspace_id/" < run.template.json > run.json |
| 63 | + |
| 64 | +run_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @run.json https://${address}/api/v2/runs) |
| 65 | + |
| 66 | +# Parse run run_result |
| 67 | +run_id=$(echo $run_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])") |
| 68 | +echo "Run ID: " $run_id |
| 69 | + |
| 70 | +# Check run run result |
| 71 | +continue=1 |
| 72 | +while [ $continue -ne 0 ]; do |
| 73 | + # Sleep a bit |
| 74 | + sleep $sleep_duration |
| 75 | + echo "Checking run status" |
| 76 | + |
| 77 | + # Check the status |
| 78 | + check_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id}) |
| 79 | + |
| 80 | + # Parse out the startus |
| 81 | + run_status=$(echo $check_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['status'])") |
| 82 | + echo "Run Status: " $run_status |
| 83 | + |
| 84 | + # If status is "policy_checked" or "policy_override", |
| 85 | + # then do Apply. If "errored", exit loop. |
| 86 | + # Anything else, continue loop |
| 87 | + if [[ "$run_status" == "policy_checked" ]] ; then |
| 88 | + continue=0 |
| 89 | + # Do the apply |
| 90 | + echo "Policies passed. Doing Apply" |
| 91 | + apply_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply) |
| 92 | + elif [[ "$run_status" == "policy_override" ]] && [[ "$override" == "yes" ]]; then |
| 93 | + continue=0 |
| 94 | + echo "Some policies failed, but will override" |
| 95 | + # Get the policy check ID |
| 96 | + echo "Getting policy check ID" |
| 97 | + policy_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" https://${address}/api/v2/runs/${run_id}/policy-checks) |
| 98 | + # Parse out the policy check ID |
| 99 | + policy_check_id=$(echo $policy_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data'][0]['id'])") |
| 100 | + echo "Policy Check ID: " $policy_check_id |
| 101 | + # Override policy |
| 102 | + echo "Overriding policy check" |
| 103 | + override_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST https://${address}/api/v2/policy-checks/${policy_check_id}/actions/override) |
| 104 | + # Do the apply |
| 105 | + echo "Doing Apply" |
| 106 | + apply_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply) |
| 107 | + elif [[ "$run_status" == "policy_override" ]] && [[ "$override" == "no" ]]; then |
| 108 | + echo "Some policies failed, but will not override. Check run in Terraform Enterprise UI." |
| 109 | + continue=0 |
| 110 | + elif [[ "$run_status" == "errored" ]]; then |
| 111 | + echo "Plan errored or hard-mandatory policy failed" |
| 112 | + continue=0 |
| 113 | + else |
| 114 | + sleep $sleep_duration |
| 115 | + fi |
| 116 | +done |
0 commit comments