-
Notifications
You must be signed in to change notification settings - Fork 3
/
00_create_workspace.sh
executable file
·69 lines (53 loc) · 2.06 KB
/
00_create_workspace.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# Make sure tfc_token environment variable is set
# to owners team token for organization
# Set address if using private Terraform Enterprise server.
# Set organization and workspace to create.
# You should edit these before running.
tfc_token=`cat tfe_team_token`
address="app.terraform.io"
organization="<MY_ORG>"
workspace="<MY_WORKSPACE>"
########################
# 01) CREATE WORKSPACE #
########################
#Set name of workspace in workspace.json (create a payload.json)
sed -e "s/placeholder/$workspace/" < workspace.template.json > workspace.json
# Create workspace
workspace_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @workspace.json \
"https://${address}/api/v2/organizations/${organization}/workspaces"
)
workspace_id=$(
echo $workspace_result | jq -r ".data | select (.attributes.name == \"$workspace\") | .id "
)
echo "Workspace created. WorkspaceID: $workspace_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 02) Variables"
#####################################
# 02) ASSIGN VARIABLES TO WORKSPACE #
#####################################
# Add variables to workspace
while IFS=',' read -r key value category hcl sensitive
do
sed -e "s/my-organization/$organization/" \
-e "s/my-workspace/$workspace_id/" \
-e "s/my-key/$key/" \
-e "s/my-value/$value/" \
-e "s/my-category/$category/" \
-e "s/my-hcl/$hcl/" \
-e "s/my-sensitive/$sensitive/" < variable.template.json > variable.json
echo "Adding variable $key in category $category "
upload_variable_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--data @variable.json \
"https://${address}/api/v2/vars?filter%5Borganization%5D%5Bname%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}"
)
done < variables.csv
echo
read -n 1 -r -p "Press any key to continue with STEP 03) Config-Version"