Skip to content

Commit 4686afd

Browse files
terechcGitHub Enterprise
authored andcommitted
Issue 1157: Automated Template Test Script (#1234)
* template test script basics * arg comment * status loops * log whole error on failure * log for job failed case
1 parent 3d56dff commit 4686afd

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

template-test.sh

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/bin/sh
2+
3+
usage() {
4+
# Display Help
5+
echo
6+
echo "Syntax: $ template-test.sh [-h] [-a API KEY]"
7+
echo "Options:"
8+
echo " a IBM Cloud Platform API Key (REQUIRED)."
9+
echo " s SSH Public Key (REQUIRED)"
10+
echo " r Region to deploy resources in. Default value = 'us-south'."
11+
echo " g Resource group to deploy resources in. Default value = 'default'."
12+
echo " h Print help."
13+
echo
14+
}
15+
16+
# set up defaults and get arguments
17+
API_KEY="NOT-SET"
18+
SSH_KEY="NOT-SET"
19+
REGION="us-south"
20+
RESOURCE_GROUP="default"
21+
WORKSPACENAME="craig-template-test"
22+
TEMPLATE="quick-start-power"
23+
24+
# define arguments for getopts to look for (a,s,r,g)
25+
while getopts ":ha:s:r:g:" opt; do
26+
case $opt in
27+
h) # if -h print usage
28+
usage
29+
exit 0
30+
;;
31+
a) API_KEY=$OPTARG ;;
32+
s) SSH_KEY=$OPTARG ;;
33+
r) REGION=$OPTARG ;;
34+
g) RESOURCE_GROUP=$OPTARG ;;
35+
\?) # this case is for when an unknown argument is passed (e.g. -c)
36+
echo "Invalid option: -$OPTARG"
37+
exit 1
38+
;;
39+
esac
40+
done
41+
42+
if [ $API_KEY == "NOT-SET" ]; then
43+
echo "Please provide your IBM Cloud API Key."
44+
exit 1
45+
fi
46+
47+
if [ "$SSH_KEY" == "NOT-SET" ]; then
48+
echo "Please provide your public SSH Key"
49+
exit 1
50+
fi
51+
52+
# IAM token needed for API Calls
53+
TOKEN=$(echo $(curl -s -k -X POST \
54+
--header "Content-Type: application/x-www-form-urlencoded" \
55+
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
56+
--data-urlencode "apikey=$API_KEY" \
57+
"https://iam.cloud.ibm.com/identity/token") | jq -r .access_token)
58+
59+
# Store tar file for template in 'template-test.tar'
60+
curl -s -X GET \
61+
--url http://localhost:8080/api/craig/template-tar/$TEMPLATE \
62+
-o 'template-test.tar'
63+
64+
# Create workspace and store workspace info
65+
WORKSPACE_DATA=$(curl -s --request POST \
66+
--url https://schematics.cloud.ibm.com/v1/workspaces \
67+
-H "Authorization: Bearer $TOKEN" \
68+
-d '{"name":"'$WORKSPACENAME'", "resource_group": "'$RESOURCE_GROUP'","type": ["terraform_v1.3"],
69+
"location": "'$REGION'", "description": "Automated CRAIG testing workspace",
70+
"tags": ["craig"], "template_data": [{ "type": "terraform_v1.3"}]}')
71+
WORKSPACE_ID=$(echo "$WORKSPACE_DATA" | jq -r .id)
72+
TEMPLATE_ID=$(echo "$WORKSPACE_DATA" | jq -r .template_data[0].id)
73+
74+
# Upload template-test.tar file to newly created workspace
75+
curl -s --request PUT \
76+
--url https://schematics.cloud.ibm.com/v1/workspaces/$WORKSPACE_ID/template_data/$TEMPLATE_ID/template_repo_upload \
77+
-H "Authorization: Bearer $TOKEN" \
78+
-H "Content-Type: multipart/form-data" \
79+
80+
81+
# Wait for tar file to upload before continuing
82+
while true; do
83+
WORKSPACE=$(curl -s --request GET --url https://schematics.cloud.ibm.com/v1/workspaces/$WORKSPACE_ID \
84+
-H "Authorization: Bearer $TOKEN")
85+
WORKSPACE_STATUS=$(echo "$WORKSPACE" | jq -r '.status')
86+
if [ "$WORKSPACE_STATUS" == "INACTIVE" ]; then
87+
echo "Tar file succesfully uploaded"
88+
break
89+
elif [ "$WORKSPACE_STATUS" == "FAILED" ]; then
90+
echo "Error: Tar file failed to upload"
91+
echo $WORKSPACE
92+
exit 1
93+
else
94+
echo "Waiting tar file to finish uploading..."
95+
sleep 10
96+
fi
97+
done
98+
99+
# Clean up local tar file
100+
rm template-test.tar
101+
102+
# Set vars in workspace
103+
UPDATE_VARS=$(curl -s --request PUT \
104+
--url "https://schematics.cloud.ibm.com/v1/workspaces/$WORKSPACE_ID/template_data/$TEMPLATE_ID/values" \
105+
-d '{"variablestore": [{"name": "ibmcloud_api_key", "secure": true, "use_default": false, "value": "'$API_KEY'"},
106+
{"name": "vsi_ssh_key_public_key", "secure": false, "use_default": false, "value": "'"$SSH_KEY"'" }]}' \
107+
-H "Content-Type: application/json" \
108+
-H "Authorization: Bearer $TOKEN")
109+
110+
# Begin generate plan job
111+
JOB_ID=$(curl -s --request POST \
112+
--url "https://schematics.cloud.ibm.com/v1/workspaces/$WORKSPACE_ID/plan" \
113+
-H "Authorization: Bearer $TOKEN" | jq -r .activityid)
114+
115+
# Check status of generate plan job
116+
while true; do
117+
JOB=$(curl -s --request GET \
118+
--url "https://schematics.cloud.ibm.com/v2/jobs/$JOB_ID" \
119+
-H "Authorization: Bearer $TOKEN")
120+
JOB_STATUS=$(echo "$JOB" | jq -r '.status.workspace_job_status.status_code')
121+
if [ "$JOB_STATUS" == "job_failed" ]; then
122+
echo "Error: Generate plan failed"
123+
echo $JOB
124+
exit 1
125+
elif [ "$JOB_STATUS" == "job_finished" ]; then
126+
echo "Plan successfully generated"
127+
break
128+
elif [ "$JOB_STATUS" == "job_pending" ] || [ "$JOB_STATUS" == "job_in_progress" ]; then
129+
echo "Generate plan pending...."
130+
sleep 20
131+
else
132+
echo "Error: Job cancelled"
133+
echo $JOB
134+
exit 1
135+
fi
136+
done

0 commit comments

Comments
 (0)