Skip to content

Commit 5085c1a

Browse files
authored
Merge pull request #244 from NetApp/add_wf_api_samples
Add wf api samples
2 parents f19a1c4 + cf27454 commit 5085c1a

19 files changed

+2604
-0
lines changed

Management-Utilities/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This subfolder contains tools that can help you manage your FSx ONTAP file syste
99
| [fsxn-rotate-secret](/Management-Utilities/fsxn-rotate-secret) | This is a Lambda function that can be used with an AWS Secrets Manager secret to rotate the FSx for ONTAP admin password. |
1010
| [iscsi-vol-create-and-mount](/Management-Utilities/iscsi-vol-create-and-mount) | This tool will create an iSCSI volume on an FSx ONTAP file system and mount it to an EC2 instance running Windows. |
1111
| [warm_performance_tier](/Management-Utilities/warm_performance_tier) | This tool to warm up the performance tier of an FSx ONTAP file system volume. |
12+
| [Workload-Factory-API-Samples](/Management-Utilities/Workload-Factory-API-Samples) | This folder contains a collection of bash scripts that demonstrate how to use the Workload Factory APIs. |
1213

1314
## Author Information
1415

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Workload Factory API Samples
2+
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.
7+
8+
## Prerequisites
9+
To run these scripts, you need to have the following prerequisites:
10+
- A bash shell.
11+
- The `curl` command-line tool installed.
12+
- The `jq` command-line JSON processor installed. You can install it using your package manager, e.g., `apt-get install jq` on Debian/Ubuntu or `brew install jq` on macOS.
13+
14+
## Notes:
15+
- All scripts allow you to set environment variables to pass options instead of having to use the
16+
command line options. For example, instead of using the `-t` option to pass the
17+
[BlueXP Refresh Token](https://docs.netapp.com/us-en/bluexp-automation/platform/create_user_token.html#1-generate-a-netapp-refresh-token),
18+
you can set the `REFRESH_TOKEN` environment variable.
19+
20+
- All scripts use the `-h` option to display the help message, which includes the available options and their descriptions.
21+
22+
Hopefully with these samples you'll be able to create your own scripts that use any the Workload Factory APIs.
23+
If you do create a new script, please consider contributing it back to this repository so that others can benefit from it.
24+
25+
## Available Scripts
26+
| Script | Description |
27+
| --- | --- |
28+
| [list_bluexp_accts](list_bluexp_accts) | This list all the BlueXP accounts (a.k.a. organizations) that you have access to. |
29+
| [list_credentials](list_credentials) | This lists all the Workload Factory credentials that you have access to. |
30+
| [list_filesystems](list_filesystems) | This lists all the FSx for ONTAP file systems that you have access to in the specified AWS region. |
31+
| [list_snapmirrors](list_snapmirrors) | This lists all the SnapMirror relationships that are associated with the specified file system. |
32+
| [list_svms](list_svms) | This lists all the SVMs that are associated with the specified file system. |
33+
| [list_volumes](list_volumes) | This lists all the volumes that are associated with the specified file system. |
34+
| [snapmirror_break](snapmirror_break) | This breaks the SnapMirror relationship for the specified relationship. |
35+
| [snapmirror_create](snapmirror_create) | This creates a SnapMirror relationship between the specified source volume and destination SVM. |
36+
| [snapmirror_delete](snapmirror_delete) | This deletes the SnapMirror relationship for the specified relationship. |
37+
| [snapmirror_resync](snapmirror_resync) | This resyncs the SnapMirror relationship for the specified relationship. |
38+
| [snapmirror_reverse](snapmirror_reverse) | This reverses the SnapMirror relationship for the specified relationship. |
39+
| [snapmirror_update](snapmirror_update) | This updates the SnapMirror relationship for the specified relationship. |
40+
| [snapshot_create](snapshot_create) | This creates a snapshot of the specified volume. |
41+
| [volume_clone](volume_clone) | This clones the specified volume. |
42+
| [volume_delete](volume_delete) | This deletes the specified volume. |
43+
| [wf_utils](wf_utils) | This file contains common functions used by all the scripts. It includes the `get_token()` function that retrieves an authentication token from the Workload Factory API. |
44+
45+
## Author Information
46+
47+
This repository is maintained by the contributors listed on [GitHub](https://github.com/NetApp/FSx-ONTAP-samples-scripts/graphs/contributors).
48+
49+
## License
50+
51+
Licensed under the Apache License, Version 2.0 (the "License").
52+
53+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0).
54+
55+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an _"AS IS"_ basis, without WARRANTIES or conditions of any kind, either express or implied.
56+
57+
See the License for the specific language governing permissions and limitations under the License.
58+
59+
© 2025 NetApp, Inc. All Rights Reserved.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
#
3+
# This script is used to list all the BlueXP accounts that a user has
4+
# access to.
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 accounts (a.k.a. organizations)
18+
that you have access to.
19+
20+
Usage is: $(basename $0) -t refresh_token
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+
26+
Note, instead of passing parameters on the command line, you can set the
27+
following environment variables instead:
28+
29+
export REFRESH_TOKEN=<refresh_token>
30+
EOF
31+
exit 1
32+
}
33+
34+
tmpout=/tmp/list_accounts-out.$$
35+
tmperr=/tmp/list_accounts-err.$$
36+
trap 'rm -f $tmpout $tmperr' exit
37+
38+
while getopts ht: opt; do
39+
case $opt in
40+
t) REFRESH_TOKEN="$OPTARG" ;;
41+
*) usage ;;
42+
esac
43+
done
44+
#
45+
# Source the wf_utils file.
46+
wf_utils=$(command -v wf_utils)
47+
if [ -z "$wf_utils" ]; then
48+
if [ ! -x "./wf_utils" ]; then
49+
cat <<EOF >&2
50+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
51+
It is required to run this script. You can download it from:
52+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
53+
EOF
54+
exit 1
55+
else
56+
wf_utils=./wf_utils
57+
fi
58+
fi
59+
. "$wf_utils"
60+
#
61+
# Check that the required commands are available.
62+
for cmd in jq curl; do
63+
if ! command -v $cmd &> /dev/null; then
64+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
65+
exit 1
66+
fi
67+
done
68+
69+
token=$(get_token)
70+
if [ -z "$token" ]; then
71+
echo "Error: Failed to obtain an access token. Exiting." >&2
72+
exit 1
73+
fi
74+
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
78+
else
79+
echo "Error: Failed to parse the response from the API." >&2
80+
exit 1
81+
fi
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
#
3+
# This script is used to list all the Workload Factory credentials the
4+
# user has access to.
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 lists all the Workload Factory credentials you has access to.
18+
19+
Usage: $(basename $0) -t refresh_token -a blueXP_account_ID [-o]
20+
21+
Where: refresh_token - Is a refresh token used to obtain an access token needed
22+
to run the Workload Factory APIs. You can obtain a refresh
23+
token by going to https://services.cloud.netapp.com/refresh-token
24+
blueXP_account_ID - is the BlueXP account ID. You can find all the accounts
25+
you have access to by running the "list_bluexp_accts" script
26+
-o means to also show the ONTAP credentials
27+
28+
Instead of passing parameters on the command line, you can set the
29+
following environment variables:
30+
31+
export REFRESH_TOKEN=<refresh_token>
32+
export BLUEXP_ACCOUNT_ID=<blueXP_account_ID>
33+
EOF
34+
exit 1
35+
}
36+
37+
################################################################################
38+
# Main logic starts here.
39+
################################################################################
40+
tmpout=$(mktemp /tmp/list_credentials-out.XXXXXX)
41+
tmpout2=$(mktemp /tmp/list_credentials-out2.XXXXXX)
42+
tmperr=$(mktemp /tmp/list_credentials-err.XXXXXX)
43+
trap 'rm -f $tmpout $tmpout2 $tmperr' exit
44+
#
45+
# Source the wf_utils file.
46+
wf_utils=$(command -v wf_utils)
47+
if [ -z "$wf_utils" ]; then
48+
if [ ! -x "./wf_utils" ]; then
49+
cat >&2 <<EOF
50+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
51+
It is required to run this script. You can download it from:
52+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
53+
EOF
54+
exit 1
55+
else
56+
wf_utils=./wf_utils
57+
fi
58+
fi
59+
. "$wf_utils"
60+
#
61+
# Set defaults.
62+
filter="filter=$(urlencode "type eq 'AWS_ASSUME_ROLE'")"
63+
while getopts "ht:a:o" opt; do
64+
case $opt in
65+
t) REFRESH_TOKEN="$OPTARG" ;;
66+
a) BLUEXP_ACCOUNT="$OPTARG" ;;
67+
o) filter="";; # No filter, list all credentials.
68+
*) usage ;;
69+
esac
70+
done
71+
#
72+
# Check that all the parameters are set.
73+
if [ -z "$REFRESH_TOKEN" ]; then
74+
cat >&2 <<EOF
75+
Error: A BlueXP refresh tokon is required to run this script.
76+
Can you be obtain from this web page:
77+
78+
https://services.cloud.netapp.com/refresh-token
79+
80+
EOF
81+
usage
82+
fi
83+
84+
if [ -z "$BLUEXP_ACCOUNT_ID" ]; then
85+
cat >&2 <<EOF
86+
Error: A BlueXP account ID is required to run this script.
87+
You can see the list of accounts you have access to by running the "list_bluexp_accts" script
88+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
89+
90+
EOF
91+
usage
92+
fi
93+
#
94+
# Check that the required commands are available.
95+
for cmd in curl jq; do
96+
if ! command -v $cmd > /dev/null 2>&1; then
97+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
98+
exit 1
99+
fi
100+
done
101+
102+
token=$(get_token)
103+
if [ -z "$token" ]; then
104+
echo "Error: Failed to obtain an access token. Exiting." >&2
105+
exit 1
106+
fi
107+
108+
run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?$filter" $tmpout $tmperr
109+
jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout > $tmpout2
110+
#
111+
# Check to see if there are more.
112+
nextToken=$(jq -r '.nextToken' $tmpout)
113+
while [ "$nextToken" != "null" ]; do
114+
run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?nextToken=$nextToken&$filter" $tmpout $tmperr
115+
jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout >> $tmpout2
116+
nextToken=$(jq -r '.nextToken' $tmpout)
117+
done
118+
119+
sort -f -k 2,2 -k 1,1 $tmpout2 | column -t -N "Name,Type,Account,ID"

0 commit comments

Comments
 (0)