Skip to content

Commit 8f297c0

Browse files
committed
Updated the description on the README file; added list_bluexp_members; fixed a couple of bugs.
1 parent cf27454 commit 8f297c0

File tree

5 files changed

+139
-13
lines changed

5 files changed

+139
-13
lines changed

Management-Utilities/Workload-Factory-API-Samples/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Workload Factory API Samples
22

3-
This folder contains various bash shell scripts that demonstrate how to use the Workload Factory APIs to perform an action.
4-
They all depend on the [wf_utils](wf_utils) file that contains common functions used by all the scripts. One function
5-
in particular, `get_token()`, is used to get an authentication token from the BlueXP Workload Factory API. So, if you
6-
copy just some of the files from this repository, make sure to copy the `wf_utils` file as well.
3+
The idea behind this folder is to show examples of how to use the [BlueXP Workload Factory APIs](https://console.workloads.netapp.com/api-doc).
4+
Not every API is covered, but the ones required to get you started (get a refresh token, get the BlueXP accountID,
5+
get BlueXP credentials ID) are included. Once you have the informaiton provided from these APIs are ready to start
6+
calling the others. While these examples are implemented as bash shell scripts you should be able to translate them
7+
to the programming language that you prefer, such as Python, Go, or JavaScript.
8+
9+
Note that all these scripts depend on the [wf_utils](wf_utils) file that contains common functions used by all the
10+
scripts. One function in particular, `get_token()`, is used to get an authentication token from the BlueXP Workload
11+
Factory API. So, if you copy just some of the files from this repository, make sure to copy the `wf_utils` file as well.
712

813
## Prerequisites
914
To run these scripts, you need to have the following prerequisites:
@@ -17,7 +22,7 @@ command line options. For example, instead of using the `-t` option to pass the
1722
[BlueXP Refresh Token](https://docs.netapp.com/us-en/bluexp-automation/platform/create_user_token.html#1-generate-a-netapp-refresh-token),
1823
you can set the `REFRESH_TOKEN` environment variable.
1924

20-
- All scripts use the `-h` option to display the help message, which includes the available options and their descriptions.
25+
- All scripts accept the `-h` option to display the help message, which includes the available options and their descriptions.
2126

2227
Hopefully with these samples you'll be able to create your own scripts that use any the Workload Factory APIs.
2328
If you do create a new script, please consider contributing it back to this repository so that others can benefit from it.
@@ -26,6 +31,7 @@ If you do create a new script, please consider contributing it back to this repo
2631
| Script | Description |
2732
| --- | --- |
2833
| [list_bluexp_accts](list_bluexp_accts) | This list all the BlueXP accounts (a.k.a. organizations) that you have access to. |
34+
| [list_bluexp_members](list_bluexp_members) | This list all members of a provided BlueXP account. |
2935
| [list_credentials](list_credentials) | This lists all the Workload Factory credentials that you have access to. |
3036
| [list_filesystems](list_filesystems) | This lists all the FSx for ONTAP file systems that you have access to in the specified AWS region. |
3137
| [list_snapmirrors](list_snapmirrors) | This lists all the SnapMirror relationships that are associated with the specified file system. |

Management-Utilities/Workload-Factory-API-Samples/list_bluexp_accts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ if [ -z "$token" ]; then
7272
exit 1
7373
fi
7474

75-
run_curl GET "$token" https://api.bluexp.netapp.com/tenancy/account $tmpout $tmperr
76-
if jq -r '.[] | .accountName + " " + .accountPublicId' $tmpout > $tmperr; then
77-
cat $tmperr | column -t -N Name,ID
75+
run_curl GET "$token" https://api.bluexp.netapp.com/v1/management/organizations $tmpout $tmperr
76+
if jq -r '.items[] | "\(.name) \(.legacyId) \(.id)"' $tmpout > $tmperr; then
77+
cat $tmperr | column -t -N Name,ID,UUID
7878
else
7979
echo "Error: Failed to parse the response from the API." >&2
8080
exit 1
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/bin/bash
2+
#
3+
# This script is used to list all the BlueXP members that are
4+
# associated with the BlueXP account.
5+
#
6+
# It is dependent on the 'wf_utils' file that is included in this repo. That
7+
# file contains the 'get_token' function that is used to obtain a valid
8+
# access token that is needed to run the Workload Factory APIs. The file needs
9+
# to either be in the command search path or in the current directory.
10+
################################################################################
11+
12+
################################################################################
13+
# Display usage information then exits the script.
14+
################################################################################
15+
usage () {
16+
cat >&2 <<EOF
17+
This script is used to list all the BlueXP members that are
18+
associated with the BlueXP account.
19+
20+
Usage is: $(basename $0) -t refresh_token -u bluexp_account_uuid
21+
22+
Where: refresh_token - Is a refresh token used to obtain an access token needed
23+
to run the Workload Factory APIs. You can obtain a refresh
24+
token by going to https://services.cloud.netapp.com/refresh-token
25+
bluexp_account_uuid - The BlueXP account UUID to list the members for. You can
26+
find all the accounts you have access to by running the
27+
"list_bluexp_accts" script.
28+
-h - Displays this help message.
29+
30+
Note, instead of passing parameters on the command line, you can set the
31+
following environment variables instead:
32+
33+
export REFRESH_TOKEN=<refresh_token>
34+
export BLUEXP_ACCOUNT_UUID=<bluexp_account_uuid>
35+
EOF
36+
exit 1
37+
}
38+
39+
tmpout=/tmp/list_accounts-out.$$
40+
tmperr=/tmp/list_accounts-err.$$
41+
trap 'rm -f $tmpout $tmperr' exit
42+
43+
while getopts ht:u: opt; do
44+
case $opt in
45+
t) REFRESH_TOKEN="$OPTARG" ;;
46+
u) BLUEXP_ACCOUNT_UUID="$OPTARG" ;;
47+
*) usage ;;
48+
esac
49+
done
50+
#
51+
# Source the wf_utils file.
52+
wf_utils=$(command -v wf_utils)
53+
if [ -z "$wf_utils" ]; then
54+
if [ ! -x "./wf_utils" ]; then
55+
cat <<EOF >&2
56+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
57+
It is required to run this script. You can download it from:
58+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
59+
EOF
60+
exit 1
61+
else
62+
wf_utils=./wf_utils
63+
fi
64+
fi
65+
. "$wf_utils"
66+
#
67+
# Check that all the parameters are set.
68+
if [ -z "$REFRESH_TOKEN" ]; then
69+
cat >&2 <<EOF
70+
Error: A BlueXP refresh tokon is required to run this script.
71+
Can you be obtain from this web page:
72+
73+
https://services.cloud.netapp.com/refresh-token
74+
75+
EOF
76+
usage
77+
fi
78+
79+
if [ -z "$BLUEXP_ACCOUNT_UUID" ]; then
80+
cat >&2 <<EOF
81+
Error: A BlueXP account UUID is required to run this script.
82+
You can see the list of accounts you have access to by running the "list_bluexp_accts" script
83+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
84+
85+
EOF
86+
usage
87+
fi
88+
#
89+
# Check that the required commands are available.
90+
for cmd in jq curl; do
91+
if ! command -v $cmd &> /dev/null; then
92+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
93+
exit 1
94+
fi
95+
done
96+
97+
token=$(get_token)
98+
if [ -z "$token" ]; then
99+
echo "Error: Failed to obtain an access token. Exiting." >&2
100+
exit 1
101+
fi
102+
103+
run_curl GET "$token" "https://api.bluexp.netapp.com/v1/management/organizations/$BLUEXP_ACCOUNT_UUID/users?limit=1000&filter=userType%20ne%20%27agent%27" $tmpout $tmperr "" "application/vnd.netapp.bxp.users.extended+json"
104+
count=$(jq -r '.count' $tmpout 2> /dev/null)
105+
if [[ "$count" == 0 ]]; then
106+
echo "No members found for the specified BlueXP account UUID: $BLUEXP_ACCOUNT_UUID." >&2
107+
echo "Did you provide the UUID or the iD? This script needs the UUID." >&2
108+
exit 0
109+
fi
110+
111+
if jq -r '.items[] | .name + "," + if(.userType == "user") then .userType + "," + .id else .userType + "," + .auth0Id end + "," + .email' $tmpout > $tmperr; then
112+
cat $tmperr | sort -f | column -s, -t -N Name,Type,ID,Email
113+
else
114+
echo "Error: Failed to parse the response from the API." >&2
115+
exit 1
116+
fi

Management-Utilities/Workload-Factory-API-Samples/list_credentials

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ filter="filter=$(urlencode "type eq 'AWS_ASSUME_ROLE'")"
6363
while getopts "ht:a:o" opt; do
6464
case $opt in
6565
t) REFRESH_TOKEN="$OPTARG" ;;
66-
a) BLUEXP_ACCOUNT="$OPTARG" ;;
66+
a) BLUEXP_ACCOUNT_ID="$OPTARG" ;;
6767
o) filter="";; # No filter, list all credentials.
6868
*) usage ;;
6969
esac

Management-Utilities/Workload-Factory-API-Samples/wf_utils

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ get_token() {
4646

4747
createToken=false
4848
if [ -r $tokenFile ]; then
49-
let diff="$(date +%s) - $(date -r wf_utils +%s)"
49+
let diff="$(date +%s) - $(date -r $tokenFile +%s)"
5050
if [ $diff -gt $token_valid ]; then
5151
createToken=true
5252
fi
@@ -94,27 +94,31 @@ run_curl () {
9494
output="$4"
9595
errorOutput="$5"
9696
data="$6"
97+
accept="$7"
9798
if [[ -z "$method" || -z "$token" || -z "$url" || -z "$output" || -z "$errorOutput" ]]; then
9899
echo "Error: Missing required parameters for run_curl function." >&2
99100
exit 1
100101
fi
102+
if [ -z "$accept" ]; then
103+
accept="application/json, text/plain, */*"
104+
fi
101105
case "$method" in
102106
GET|get)
103107
curl -sw "%{stderr}%{http_code},%{errormsg}" "$url" \
104-
-H "Accept:application/json, text/plain, */*" \
108+
-H "Accept: $accept" \
105109
-H "Authorization: Bearer $token" > $output 2> $errorOutput
106110
exitCode=$?
107111
;;
108112
POST|post)
109113
curl -X POST -sw "%{stderr}%{http_code},%{errormsg}" "$url" \
110-
-H "Accept:application/json, text/plain, */*" \
114+
-H "Accept: $accept" \
111115
-H "Content-Type:application/json" \
112116
-H "Authorization: Bearer $token" --data "$data" > $output 2> $errorOutput
113117
exitCode=$?
114118
;;
115119
DELETE|delete)
116120
curl -X DELETE -sw "%{stderr}%{http_code},%{errormsg}" "$url" \
117-
-H "Accept:application/json, text/plain, */*" \
121+
-H "Accept: $accept" \
118122
-H "Authorization: Bearer $token" > $output 2> $errorOutput
119123
exitCode=$?
120124
;;

0 commit comments

Comments
 (0)