Skip to content

Commit 2d4d5eb

Browse files
authored
Merge pull request #250 from NetApp/add_wf_api_samples
Made updates, and added a few more, to the Workload Factory API samples
2 parents a37f9ad + a78e1f0 commit 2d4d5eb

27 files changed

+1513
-1059
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ If you do create a new script, please consider contributing it back to this repo
3030
## Available Scripts
3131
| Script | Description |
3232
| --- | --- |
33+
| [credentials_delete](credentials_delete) | This deletes a Workload Factory credential. |
34+
| [fsxn_credentials_set](fsxn_credentials_set) | This sets the credentials for a specified FSx for ONTAP file system. |
35+
| [link_associate](link_associate) | This associates a Workload Factory Link with a specified FSx for ONTAP file system. |
36+
| [link_delete](link_delete) | This deletes a Workload Factory Link. |
37+
| [link_disassociate](link_disassociate) | This disassociates a Workload Factory Link with a specified FSx for ONTAP file system. |
38+
| [link_register](link_register) | This registers a Lambda function as a Workload Factory Link. |
3339
| [list_bluexp_accts](list_bluexp_accts) | This list all the BlueXP accounts (a.k.a. organizations) that you have access to. |
3440
| [list_bluexp_members](list_bluexp_members) | This list all members of a provided BlueXP account. |
3541
| [list_credentials](list_credentials) | This lists all the Workload Factory credentials that you have access to. |
3642
| [list_filesystems](list_filesystems) | This lists all the FSx for ONTAP file systems that you have access to in the specified AWS region. |
43+
| [list_links](list_links) | This lists all the Workload Factory Links that you have access to. |
3744
| [list_snapmirrors](list_snapmirrors) | This lists all the SnapMirror relationships that are associated with the specified file system. |
3845
| [list_svms](list_svms) | This lists all the SVMs that are associated with the specified file system. |
3946
| [list_volumes](list_volumes) | This lists all the volumes that are associated with the specified file system. |
47+
| [show_fsxn_credentials](show_fsxn_credentials) | This shows the credentials that Workload Factory has stored for the specified FSx for ONTAP file system. |
4048
| [snapmirror_break](snapmirror_break) | This breaks the SnapMirror relationship for the specified relationship. |
4149
| [snapmirror_create](snapmirror_create) | This creates a SnapMirror relationship between the specified source volume and destination SVM. |
4250
| [snapmirror_delete](snapmirror_delete) | This deletes the SnapMirror relationship for the specified relationship. |
@@ -46,7 +54,7 @@ If you do create a new script, please consider contributing it back to this repo
4654
| [snapshot_create](snapshot_create) | This creates a snapshot of the specified volume. |
4755
| [volume_clone](volume_clone) | This clones the specified volume. |
4856
| [volume_delete](volume_delete) | This deletes the specified volume. |
49-
| [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. |
57+
| [wf_utils](wf_utils) | This file contains common functions used by all the scripts. It includes the `get_token()` function that retrieves an access token from the Workload Factory API. |
5058

5159
## Author Information
5260

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/bash
2+
#
3+
################################################################################
4+
# This script is used to delete credentials stored in Workload Factory.
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+
# This function displays the usage of this script and exits.
14+
################################################################################
15+
usage() {
16+
cat >&2 <<EOF
17+
This script is used to delete credentials stored in Workload Factory.
18+
19+
usage: $(basename $0) -t refresh_token -a blueXP_account_ID -c delete_credentials_ID
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. Run 'list_bluexp_accts' to get a
25+
list of accounts you have access to.
26+
delete_credentials_ID - Is the ID of the credentials you want to delete.
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+
41+
tmpout=$(mktemp /tmp/credentials_delete-out.XXXXXX)
42+
tmperr=$(mktemp /tmp/credentials_delete-err.XXXXXX)
43+
trap 'rm -f $tmpout $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+
# Process comamnd line options.
62+
while getopts "ht:a:c:" opt; do
63+
case $opt in
64+
t) REFRESH_TOKEN="$OPTARG" ;;
65+
a) BLUEXP_ACCOUNT_ID="$OPTARG" ;;
66+
c) DELETE_CREDENTIALS_ID="$OPTARG" ;;
67+
*) usage ;;
68+
esac
69+
done
70+
#
71+
# Declare an array of required options and the error message to display if they are not set.
72+
declare -A required_options
73+
required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page:
74+
https://services.cloud.netapp.com/refresh-token\n\n'
75+
required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script.
76+
You can get the list of accounts you have access to by running the "list_bluexp_accts" script
77+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
78+
required_options["DELETE_CREDENTIALS_ID"]='Error: The ID of the credentials to delete is required.
79+
You can get a list of credentials by running the "list_credentials" script
80+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
81+
82+
check_required_options
83+
#
84+
# Check that the required commands are available.
85+
for cmd in jq curl; do
86+
if ! command -v $cmd &> /dev/null; then
87+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
88+
exit 1
89+
fi
90+
done
91+
#
92+
# Get the token to use for the API call.
93+
token=$(get_token)
94+
if [ -z "$token" ]; then
95+
echo "Error: Failed to obtain an access token. Exiting." >&2
96+
exit 1
97+
fi
98+
run_curl DELETE "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/${DELETE_CREDENTIALS_ID}" $tmpout $tmperr
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/bin/bash
2+
#
3+
################################################################################
4+
# This script is used to set the credentials for an FSxN file system.
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+
# This function displays the usage of this script and exits.
14+
################################################################################
15+
usage() {
16+
cat >&2 <<EOF
17+
18+
This script is used to set the credentials for an FSxN file system.
19+
20+
usage: $(basename $0) -t refresh_token -a blueXP_account_ID -c credentials_ID -r aws_region -f filesystem_ID -u user_ID -p password -s secret_arn
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_ID - Is the BlueXP account ID. Run 'list_bluexp_accts' to get a
26+
list of accounts you have access to.
27+
credentials_ID - Is the Workload Factory credentials ID for the AWS account.
28+
Run 'list_credentials' to get a list of Workload Factory
29+
credentials you have access to.
30+
aws_region - Is the AWS region where the file system is located.
31+
filesystem_ID - Is the ID of the FSxN file system.
32+
user_ID* - Is the user ID to set for the FSxN file system.
33+
password* - Is the password to set for the FSxN file system.
34+
secret_arn* - Is the ARN of the Secrets Manager secret that contains the
35+
credentials for the FSxN file system.
36+
37+
*NOTE: Only user_id and password OR secret_arn can be be provided at the same time.
38+
39+
Instead of passing parameters on the command line, you can set the
40+
following environment variables:
41+
42+
export REFRESH_TOKEN=<refresh_token>
43+
export BLUEXP_ACCOUNT_ID=<blueXP_account_ID>
44+
export CREDENTIALS_ID=<credentials_ID>
45+
export AWS_REGION=<aws_region>
46+
export FILESYSTEM_ID=<filesystem_ID>
47+
export USER_ID=<user_ID>
48+
export PASSWORD=<password>
49+
EOF
50+
exit 1
51+
}
52+
53+
################################################################################
54+
# Main logic starts here.
55+
################################################################################
56+
57+
tmpout=$(mktemp /tmp/fsxn_credentials_set-out.XXXXXX)
58+
tmperr=$(mktemp /tmp/fsxn_credentials_set-err.XXXXXX)
59+
trap 'rm -f $tmpout $tmperr' exit
60+
#
61+
# Source the wf_utils file.
62+
wf_utils=$(command -v wf_utils)
63+
if [ -z "$wf_utils" ]; then
64+
if [ ! -x "./wf_utils" ]; then
65+
cat >&2 <<EOF
66+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
67+
It is required to run this script. You can download it from:
68+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
69+
EOF
70+
exit 1
71+
else
72+
wf_utils=./wf_utils
73+
fi
74+
fi
75+
. "$wf_utils"
76+
#
77+
# Process command line options.
78+
while getopts "ht:a:c:r:f:u:p:s:" opt; do
79+
case $opt in
80+
t) REFRESH_TOKEN="$OPTARG" ;;
81+
a) BLUEXP_ACCOUNT_ID="$OPTARG" ;;
82+
c) CREDENTIALS_ID="$OPTARG" ;;
83+
r) AWS_REGION="$OPTARG" ;;
84+
f) FILESYSTEM_ID="$OPTARG" ;;
85+
u) USER_ID="$OPTARG" ;;
86+
p) PASSWORD="$OPTARG" ;;
87+
s) SECRET_ARN="$OPTARG" ;;
88+
*) usage ;;
89+
esac
90+
done
91+
#
92+
# Declare an array of required options and the error message to display if they are not set.
93+
declare -A required_options
94+
required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page:
95+
https://services.cloud.netapp.com/refresh-token\n\n'
96+
required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script.
97+
You can get the list of accounts you have access to by running the "list_bluexp_accts" script
98+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
99+
required_options["CREDENTIALS_ID"]='Error: The ID of the credentials to delete is required.
100+
You can get a list of credentials by running the "list_credentials" script
101+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
102+
required_options["AWS_REGION"]='Error: The AWS region where the file system is located is required.\n\n'
103+
required_options["FILESYSTEM_ID"]='Error: The ID of the FSxN file system is required.\n\n'
104+
105+
check_required_options
106+
107+
if [ -n "$USER_ID" -a -n "$PASSWORD" -a -n "$SECRET_ARN" ]; then
108+
echo "Error: You can only provide either user_id and password OR secret_arn at the same time." >&2
109+
usage
110+
elif [ -z "$USER_ID" -a -z "$PASSWORD" -a -z "$SECRET_ARN" ]; then
111+
echo "Error: You must provide either user_id and password OR secret_arn." >&2
112+
usage
113+
fi
114+
#
115+
# Check that the required commands are available.
116+
for cmd in jq curl; do
117+
if ! command -v $cmd &> /dev/null; then
118+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
119+
exit 1
120+
fi
121+
done
122+
#
123+
# Get the token to use for the API call.
124+
token=$(get_token)
125+
if [ -z "$token" ]; then
126+
echo "Error: Failed to obtain an access token. Exiting." >&2
127+
exit 1
128+
fi
129+
130+
if [ -n "$SECRET_ARN" ]; then
131+
run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/ontap-credentials" "$tmpout" "$tmperr" '{"secret":"'${SECRET_ARN}'"}'
132+
else
133+
run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/ontap-credentials" "$tmpout" "$tmperr" '{"user":"'${USER_ID}'","password":"'${PASSWORD}'","resetFsxAdminPassword":false}'
134+
fi
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
#
3+
################################################################################
4+
# This script is used to associate a link to an FSx for ONTAP file system.
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+
# This function just prints the usage of this script and exits the program.
14+
################################################################################
15+
usage() {
16+
cat >&2 <<EOF
17+
This script is used to associate a link to an FSx for ONTAP file system.
18+
19+
Usage: $(basename $0) -t refresh_token -a blueXP_account_ID -c credentials_ID -r aws_region -f filesystem_ID -l link_ID
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. Run 'list_bluexp_accts' to get a
25+
list of accounts you have access to
26+
credentials_ID - is the Workload Factory credentials ID for the AWS account. Run
27+
'list_credentials' to get a list of credentials you have access to
28+
aws_region - is the AWS region where the FSx file systems are located
29+
filesystem_ID - is the AWS file system ID of the FSx file system where the volume resides
30+
link_ID - Is the id of the link you want to disassociate
31+
32+
Instead of passing parameters on the command line, you can set the
33+
following environment variables:
34+
35+
export REFRESH_TOKEN=<refresh_token>
36+
export BLUEXP_ACCOUNT_ID=<blueXP_account_ID>
37+
export CREDENTIALS_ID=<credentials_ID>
38+
export AWS_REGION=<aws_region>
39+
EOF
40+
exit 1
41+
}
42+
43+
################################################################################
44+
# Main logic starts here.
45+
################################################################################
46+
tmpout=$(mktemp /tmp/link_assoicate-out.XXXXXX)
47+
tmperr=$(mktemp /tmp/link_assoicate-err.XXXXXX)
48+
trap 'rm -f $tmpout $tmperr' exit
49+
#
50+
# Source the wf_utils file.
51+
wf_utils=$(command -v wf_utils)
52+
if [ -z "$wf_utils" ]; then
53+
if [ ! -x "./wf_utils" ]; then
54+
cat >&2 <<EOF
55+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
56+
It is required to run this script. You can download it from:
57+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
58+
EOF
59+
exit 1
60+
else
61+
wf_utils=./wf_utils
62+
fi
63+
fi
64+
. "$wf_utils"
65+
#
66+
# Process command line options.
67+
while getopts "ht:a:c:r:f:l:" opt; do
68+
case $opt in
69+
t) REFRESH_TOKEN="$OPTARG" ;;
70+
a) BLUEXP_ACCOUNT_ID="$OPTARG" ;;
71+
c) CREDENTIALS_ID="$OPTARG" ;;
72+
r) AWS_REGION="$OPTARG" ;;
73+
f) FILESYSTEM_ID="$OPTARG" ;;
74+
l) LINK_ID="$OPTARG" ;;
75+
*) usage ;;
76+
esac
77+
done
78+
#
79+
# Declare an array of required options and the error message to display if they are not set.
80+
declare -A required_options
81+
required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page:
82+
https://services.cloud.netapp.com/refresh-token\n\n'
83+
required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script.
84+
You can get the list of accounts you have access to by running the "list_bluexp_accts" script
85+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
86+
required_options["CREDENTIALS_ID"]='Error: The ID of the credentials to delete is required.
87+
You can get a list of credentials by running the "list_credentials" script
88+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
89+
required_options["AWS_REGION"]='Error: The AWS region where the file system is located is required.\n\n'
90+
required_options["FILESYSTEM_ID"]='Error: The ID of the FSxN file system is required.\n\n'
91+
required_options["LINK_ID"]='Error: The link ID is required to run this script.
92+
You can get the list of links associated with a file systems by running the "list_links" script
93+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
94+
95+
check_required_options
96+
#
97+
# Check that the required commands are available.
98+
for cmd in jq curl; do
99+
if ! command -v $cmd &> /dev/null; then
100+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
101+
exit 1
102+
fi
103+
done
104+
105+
token=$(get_token)
106+
if [ -z "$token" ]; then
107+
echo "Error: Failed to obtain an access token. Exiting." >&2
108+
exit 1
109+
fi
110+
111+
run_curl "POST" "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/links" $tmpout $tmperr '{"linkId":"'$LINK_ID'"}'

0 commit comments

Comments
 (0)