-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwf_utils
More file actions
executable file
·214 lines (197 loc) · 6.63 KB
/
wf_utils
File metadata and controls
executable file
·214 lines (197 loc) · 6.63 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#
# This file contains functions that aid with the scripts located in this
# repo. It should be "sourced" into the script in order to use the functions
# defined.
#
################################################################################
################################################################################
# This function is used to obtain a bearer token (a.k.a. "access token") for the
# BlueXP API and, by extension, Workload Factory API. It requires a
# refresh token that can be obtained from this webpage:
#
# https://services.cloud.netapp.com/refresh-token
#
# It will store the bearer token in a file named ".blueXP_token" in the
# account's home directory. If the script is called again, it will check to
# see if the modification time of the file is less than 24 hours old and
# if it is, it will just return the contents of the file. If it is older than
# 24 hours, it will create a new token, output it to standard out and
# store it in the file.
################################################################################
get_token() {
tokenFile="$HOME/.blueXP_token"
if [ -z "$REFRESH_TOKEN" ]; then
echo "Error: The REFRESH_TOKEN environment variable has not been set." >&2
exit 1
fi
#
# Ensure all the required commands are available.
for cmd in curl; do
if ! command -v $cmd &> /dev/null; then
echo "Error: $cmd is required but not installed." >&2
exit 1
fi
done
#
# According to the documentation tokens are only good for 24 hours.
# Subtract 5 minutes to be safe.
let token_valid=60*60*24-60*5
createToken=false
if [ -r $tokenFile ]; then
let diff="$(date +%s) - $(date -r $tokenFile +%s)"
if [ $diff -gt $token_valid ]; then
createToken=true
fi
else
createToken=true
fi
if [ $createToken == "true" ]; then
token=$(curl -s -X POST 'https://netapp-cloud-account.auth0.com/oauth/token' \
-H 'Content-Type: application/json' \
--data-raw '{
"grant_type": "refresh_token",
"refresh_token": "'$REFRESH_TOKEN'",
"client_id": "Mu0V1ywgYteI6w1MbD15fKfVIUrNXGWC"
}' | sed -ne 's/."access_token":"\([^"]*\).*/\1/p')
if [ -z "$token" ]; then
echo "Error: Unable to obtain a bearer token. Error message:" >&2
cat $tmpfile >&2
echo "" >&2
exit 1
fi
echo "$token" > $tokenFile
fi
cat $tokenFile
}
################################################################################
# This function runs 'curl' checking for the status code and handling errors.
# It takes the following positional parameters:
# 1. HTTP method (GET, POST, PUT or DELETE)
# 2. Bearer token
# 3. URL to call
# 4. Output file to write the response to
# 5. Error output file to write errors to
# 6. Data to send (for POST or PUT requests)
# 7. Accept header (optional, defaults to "application/json, text/plain, */*")
# 8 -> ??? optional headers to add.
################################################################################
run_curl () {
method="$(echo $1 | tr 'a-z' 'A-Z')"
token="$2"
url="$3"
output="$4"
errorOutput="$5"
data="$6"
accept="$7"
if [ -z "$accept" ]; then
accept="application/json, text/plain, */*"
if [ $# -lt 6 ]; then
shift $#
else
shift 6
fi
else
shift 7
fi
declare -a extraHeaders
while [ $# -gt 0 ]; do
extraHeader="$1"
if [ -n "$extraHeader" ]; then
extraHeaders+=(-H "$extraHeader")
fi
shift
done
if [[ -z "$method" || -z "$token" || -z "$url" || -z "$output" || -z "$errorOutput" ]]; then
echo "Error: Missing required parameters for run_curl function." >&2
exit 1
fi
#
# Since older versions of curl don't support the %{stderr} variable, using the -o option
# to redirect output to the $output file and getting the http_code from standard out.
case "$method" in
GET)
curl -sw "%{http_code},%{errormsg}" "$url" \
-H "Accept: $accept" "${extraHeaders[@]}" \
-H "Authorization: Bearer $token" -o $output > $errorOutput
exitCode=$?
;;
POST|PUT)
curl -X "$method" -sw "%{http_code},%{errormsg}" "$url" \
-H "Accept: $accept" "${extraHeaders[@]}" \
-H "Content-Type:application/json" \
-H "Authorization: Bearer $token" --data "$data" -o $output > $errorOutput
exitCode=$?
;;
DELETE)
curl -X DELETE -sw "%{http_code},%{errormsg}" "$url" \
-H "Accept: $accept" "${extraHeaders[@]}" \
-H "Authorization: Bearer $token" -o $output > $errorOutput
exitCode=$?
;;
*)
echo "Error: Unsupported method '$method'." >&2
exit 1
;;
esac
httpCode=$(awk -F, '{print $1}' $errorOutput)
if [ "$exitCode" != "0" ]; then
errorMsg=$(awk -F, '{print $2}' $errorOutput)
echo "Error: curl command failed with exit code $exitCode ($errorMsg)." >&2
exit 1
fi
if [[ -z "$httpCode" || "$httpCode" -gt 299 ]]; then
echo "Error: HTTP request failed with status code $httpCode." >&2
#
# See if there is any useful output in the output file.
if [ -s "$output" ]; then
if (jq -r . $output 2> /dev/null) >&2; then
exit 1
fi
fi
#
# If not just dump everything to stderr.
cat $errorOutput $output >&2
echo 1>&2
exit 1
fi
}
################################################################################
# This function URL encodes the input string.
################################################################################
urlencode() {
echo "$@" | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | while read l
do
c="`echo "$l" | grep '[^-._~0-9a-zA-Z]'`"
if [ "$l" == "" ]; then
echo -n "%20"
else
if [ -z "$c" ]; then
echo -n "$l"
else
printf %%%02X \'"$c"
fi
fi
done
echo ""
}
################################################################################
# This function confirms that the user has provided all the required parameters.
# It dependson the "required_options" associative array being defined in the
# script that sources this file. The array should contain key-value pairs
# where the key is the name of the variable to check and the value is the
# error message to display if the variable is not set. The message is displayed
# with printf so it can include formatting characters like \n.
################################################################################
check_required_options () {
missingParmeter="false"
for key in "${!required_options[@]}"; do
if [ -z "${!key}" ]; then
printf "${required_options[$key]}" >&2
missingParmeter="true"
fi
done
if [ "$missingParmeter" != "false" ]; then
usage
fi
}