diff --git a/Management-Utilities/README.md b/Management-Utilities/README.md index a7b93de..9e9b7a1 100644 --- a/Management-Utilities/README.md +++ b/Management-Utilities/README.md @@ -9,6 +9,7 @@ This subfolder contains tools that can help you manage your FSx ONTAP file syste | [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. | | [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. | | [warm_performance_tier](/Management-Utilities/warm_performance_tier) | This tool to warm up the performance tier of an FSx ONTAP file system volume. | +| [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. | ## Author Information diff --git a/Management-Utilities/Workload-Factory-API-Samples/README.md b/Management-Utilities/Workload-Factory-API-Samples/README.md new file mode 100644 index 0000000..164b3cc --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/README.md @@ -0,0 +1,59 @@ +# Workload Factory API Samples + +This folder contains various bash shell scripts that demonstrate how to use the Workload Factory APIs to perform an action. +They all depend on the [wf_utils](wf_utils) file that contains common functions used by all the scripts. One function +in particular, `get_token()`, is used to get an authentication token from the BlueXP Workload Factory API. So, if you +copy just some of the files from this repository, make sure to copy the `wf_utils` file as well. + +## Prerequisites +To run these scripts, you need to have the following prerequisites: +- A bash shell. +- The `curl` command-line tool installed. +- 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. + +## Notes: +- All scripts allow you to set environment variables to pass options instead of having to use the +command line options. For example, instead of using the `-t` option to pass the +[BlueXP Refresh Token](https://docs.netapp.com/us-en/bluexp-automation/platform/create_user_token.html#1-generate-a-netapp-refresh-token), +you can set the `REFRESH_TOKEN` environment variable. + +- All scripts use the `-h` option to display the help message, which includes the available options and their descriptions. + +Hopefully with these samples you'll be able to create your own scripts that use any the Workload Factory APIs. +If you do create a new script, please consider contributing it back to this repository so that others can benefit from it. + +## Available Scripts +| Script | Description | +| --- | --- | +| [list_bluexp_accts](list_bluexp_accts) | This list all the BlueXP accounts (a.k.a. organizations) that you have access to. | +| [list_credentials](list_credentials) | This lists all the Workload Factory credentials that you have access to. | +| [list_filesystems](list_filesystems) | This lists all the FSx for ONTAP file systems that you have access to in the specified AWS region. | +| [list_snapmirrors](list_snapmirrors) | This lists all the SnapMirror relationships that are associated with the specified file system. | +| [list_svms](list_svms) | This lists all the SVMs that are associated with the specified file system. | +| [list_volumes](list_volumes) | This lists all the volumes that are associated with the specified file system. | +| [snapmirror_break](snapmirror_break) | This breaks the SnapMirror relationship for the specified relationship. | +| [snapmirror_create](snapmirror_create) | This creates a SnapMirror relationship between the specified source volume and destination SVM. | +| [snapmirror_delete](snapmirror_delete) | This deletes the SnapMirror relationship for the specified relationship. | +| [snapmirror_resync](snapmirror_resync) | This resyncs the SnapMirror relationship for the specified relationship. | +| [snapmirror_reverse](snapmirror_reverse) | This reverses the SnapMirror relationship for the specified relationship. | +| [snapmirror_update](snapmirror_update) | This updates the SnapMirror relationship for the specified relationship. | +| [snapshot_create](snapshot_create) | This creates a snapshot of the specified volume. | +| [volume_clone](volume_clone) | This clones the specified volume. | +| [volume_delete](volume_delete) | This deletes the specified volume. | +| [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. | + +## Author Information + +This repository is maintained by the contributors listed on [GitHub](https://github.com/NetApp/FSx-ONTAP-samples-scripts/graphs/contributors). + +## License + +Licensed under the Apache License, Version 2.0 (the "License"). + +You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0). + +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. + +See the License for the specific language governing permissions and limitations under the License. + +© 2025 NetApp, Inc. All Rights Reserved. diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_bluexp_accts b/Management-Utilities/Workload-Factory-API-Samples/list_bluexp_accts new file mode 100755 index 0000000..2ea9d38 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_bluexp_accts @@ -0,0 +1,81 @@ +#!/bin/bash +# +# This script is used to list all the BlueXP accounts that a user has +# access to. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# Display usage information then exits the script. +################################################################################ +usage () { + cat >&2 < +EOF + exit 1 +} + +tmpout=/tmp/list_accounts-out.$$ +tmperr=/tmp/list_accounts-err.$$ +trap 'rm -f $tmpout $tmperr' exit + +while getopts ht: opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + *) usage ;; + esac +done +# +# Source the wf_utils file. +wf_utils=$(command -v wf_utils) +if [ -z "$wf_utils" ]; then + if [ ! -x "./wf_utils" ]; then + cat <&2 +Error: The 'wf_utils' script was not found in the current directory or in the command search path. +It is required to run this script. You can download it from: +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples +EOF + exit 1 + else + wf_utils=./wf_utils + fi +fi +. "$wf_utils" +# +# Check that the required commands are available. +for cmd in jq curl; do + if ! command -v $cmd &> /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" https://api.bluexp.netapp.com/tenancy/account $tmpout $tmperr +if jq -r '.[] | .accountName + " " + .accountPublicId' $tmpout > $tmperr; then + cat $tmperr | column -t -N Name,ID +else + echo "Error: Failed to parse the response from the API." >&2 + exit 1 +fi diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_credentials b/Management-Utilities/Workload-Factory-API-Samples/list_credentials new file mode 100755 index 0000000..4316ae6 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_credentials @@ -0,0 +1,119 @@ +#!/bin/bash +# +# This script is used to list all the Workload Factory credentials the +# user has access to. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# Display usage information then exits the script. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/list_credentials-out.XXXXXX) +tmpout2=$(mktemp /tmp/list_credentials-out2.XXXXXX) +tmperr=$(mktemp /tmp/list_credentials-err.XXXXXX) +trap 'rm -f $tmpout $tmpout2 $tmperr' exit +# +# Source the wf_utils file. +wf_utils=$(command -v wf_utils) +if [ -z "$wf_utils" ]; then + if [ ! -x "./wf_utils" ]; then + cat >&2 <&2 <&2 < /dev/null 2>&1; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?$filter" $tmpout $tmperr +jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout > $tmpout2 +# +# Check to see if there are more. +nextToken=$(jq -r '.nextToken' $tmpout) +while [ "$nextToken" != "null" ]; do + run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?nextToken=$nextToken&$filter" $tmpout $tmperr + jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout >> $tmpout2 + nextToken=$(jq -r '.nextToken' $tmpout) +done + +sort -f -k 2,2 -k 1,1 $tmpout2 | column -t -N "Name,Type,Account,ID" diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_filesystems b/Management-Utilities/Workload-Factory-API-Samples/list_filesystems new file mode 100755 index 0000000..ec3de21 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_filesystems @@ -0,0 +1,147 @@ +#!/bin/bash +# +################################################################################ +# This script is used to list all the FSx for ONTAP file systems that the +# user has access to. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ +# +################################################################################ +# This function displays the usage of this script and exits. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ + +tmpout=$(mktemp /tmp/list_filesystems-out.XXXXXX) +tmpout2=$(mktemp /tmp/list_filesystems-out2.XXXXXX) +tmperr=$(mktemp /tmp/list_filesystems-err.XXXXXX) +trap 'rm -f $tmpout $tmpout2 $tmperr' exit + +while getopts "ht:a:c:r:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check that all the required parameters are set. +missingParmeter="false" +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done +# +# Get the token to use for the API call. +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems" $tmpout $tmperr +jq -r '.items[] | "\(.id),\(.name),\(.region),\(.deploymentType),\(.status.status)"' $tmpout > $tmpout2 +# +# Check to see if there are more. +nextToken=$(jq -r '.nextToken' $tmpout) +while [ "$nextToken" != "null" ]; do + run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems" + jq -r '.items[] | "\(.id),\(.name),\(.region),\(.deploymentType),\(.status.status)"' $tmpout >> $tmpout2 + nextToken=$(jq -r '.nextToken' $tmpout) +done +sort -t, -f -k 2,2 $tmpout2 | column -t -s, -N ID,Name,Region,Deployment_Type,Status diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_snapmirrors b/Management-Utilities/Workload-Factory-API-Samples/list_snapmirrors new file mode 100755 index 0000000..7174bf8 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_snapmirrors @@ -0,0 +1,158 @@ +#!/bin/bash +# +################################################################################ +# This script is used to list all the SnapMirror relationships for a given +# FSx for ONTAP file systems. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/list_snapmirror-out.XXXXXX) +tmpout2=$(mktemp /tmp/list_snapmirror-out2.XXXXXX) +tmperr=$(mktemp /tmp/list_snapmirror-err.XXXXXX) +trap 'rm -f $tmpout $tmpout2 $tmperr' exit + +while getopts "ht:a:c:r:f:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/relationships" $tmpout $tmperr +jq -r '.items[] | "\(.sourceCluster)::\(.sourceSvm):\(.sourceVolume) \(.destinationCluster)::\(if(.destinationSvm != null) then .destinationSvm else "Unknown" end):\(.destinationVolume) \(.id) \(if(.isHealthy != null) then .isHealthy else "Unknown" end) \(if(.mirrorState != null) then .mirrorState else "Unknown" end) \(if(.schedule != null) then .schedule else "None" end) \(if(.lagTime != null) then .lagTime else "Unknown" end)"' $tmpout > $tmpout2 +# +# Check to see if there are more. +nextToken=$(jq -r '.nextToken' $tmpout) +while [ "$nextToken" != "null" ]; do + run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/relationships?nextToken=${nextToken}" $tmpout $tmperr + jq -r '.items[] | "\(.sourceCluster)::\(.sourceSvm):\(.sourceVolume) \(.destinationCluster)::\(if(.destinationSvm != null) then .destinationSvm else "Unknown" end):\(.destinationVolume) \(.id) \(if(.isHealthy != null) then .isHealthy else "Unknown" end) \(if(.mirrorState != null) then .mirrorState else "Unknown" end) \(if(.schedule != null) then.schedule else "None" end) \(if(.lagTime != null) then .lagTime else "Unknown" end)"' $tmpout >> $tmpout2 + nextToken=$(jq -r '.nextToken' $tmpout) +done +sort -f $tmpout2 | column -t -N Source,Destination,SnapMirror_Relationship_ID,Healthy,State,Schedule,Lag_Time diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_svms b/Management-Utilities/Workload-Factory-API-Samples/list_svms new file mode 100755 index 0000000..96596e2 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_svms @@ -0,0 +1,147 @@ +#!/bin/bash +# +################################################################################ +# This script is used to list all the SVMs associated with a given FSx file +# system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=/tmp/list_svms-out.$$ +tmperr=/tmp/list_svms-err.$$ +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/svms" $tmpout $tmperr +jq -r '.items[] | "\(.name) \(.id) \(.fileSystemId) \(.lifecycle)"' $tmpout | sort -f | column -t -N "Name,ID,filesystem_ID,Status" diff --git a/Management-Utilities/Workload-Factory-API-Samples/list_volumes b/Management-Utilities/Workload-Factory-API-Samples/list_volumes new file mode 100755 index 0000000..351e072 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/list_volumes @@ -0,0 +1,158 @@ +#!/bin/bash +# +################################################################################ +# This script is used to list all the FSx for ONTAP volumes that the +# user has access to. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/list_volumes-out.XXXXXX) +tmpout2=$(mktemp /tmp/list_volumes-out2.XXXXXX) +tmperr=$(mktemp /tmp/list_volumes-err.XXXXXX) +trap 'rm -f $tmpout $tmpout2 $tmperr' exit + +while getopts "ht:a:c:r:f:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/volumes" $tmpout $tmperr +jq -r '.items[] | "\(.name) \(.id) \(.svmId) \(.lifecycle)"' $tmpout > $tmpout2 +# +# Check to see if there are more. +nextToken=$(jq -r '.nextToken' $tmpout) +while [ "$nextToken" != "null" ]; do + run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/volumes?nextToken=${nextToken}" $tmpout $tmperr + jq -r '.items[] | "\(.name) \(.id) \(.svmId) \(.lifecycle)"' $tmpout >> $tmpout2 + nextToken=$(jq -r '.nextToken' $tmpout) +done +sort -f $tmpout2 | column -t -N Name,ID,SVM_ID,Status diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_break b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_break new file mode 100755 index 0000000..20ab7df --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_break @@ -0,0 +1,159 @@ +#!/bin/bash +# +################################################################################ +# This script is used to break a SnapMirror relationship on a +# FSx for ONTAP file systems. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/break_mirror-out.XXXXXX) +tmperr=$(mktemp /tmp/break_mirror-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:s:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + s) SNAPMIRROR_ID="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check if the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: Required command '$cmd' not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain a token. Exiting." >&2 + exit 1 +fi + +run_curl "POST" "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${DESTINATION_CREDENTIALS_ID}/regions/${DESTINATION_AWS_REGION}/file-systems/${DESTINATION_FS_ID}/relationships/${SNAPMIRROR_ID}/break" $tmpout $tmperr '{}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_create b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_create new file mode 100755 index 0000000..8989e82 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_create @@ -0,0 +1,237 @@ +#!/bin/bash +# +################################################################################ +# This script is used to create a SnapMirror relationship on a +# FSx for ONTAP file systems. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/create_mirror-out.XXXXXX) +tmperr=$(mktemp /tmp/create_mirror-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +SOURCE_TYPE="FSx" +DESTINATION_TYPE="FSx" +THROTTLE=102400 # 100 MiB/s +SCHEDULE="hourly" +while getopts "ht:a:l:s:C:R:F:Y:M:N:V:c:r:f:y:m:n:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + l) THROTTLE="$OPTARG" ;; + s) SCHEDULE="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + y) DESTINATION_TYPE="$OPTARG" ;; + m) DESTINATION_SVM_ID="$OPTARG" ;; + n) DESTINATION_SVM_NAME="$OPTARG" ;; + F) SOURCE_FS_ID="$OPTARG" ;; + C) SOURCE_CREDENTIALS_ID="$OPTARG" ;; + R) SOURCE_AWS_REGION="$OPTARG" ;; + Y) SOURCE_TYPE="$OPTARG" ;; + M) SOURCE_SVM_ID="$OPTARG" ;; + N) SOURCE_SVM_NAME="$OPTARG" ;; + V) SOURCE_VOLUME="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check if the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 <&2 + usage ;; +esac +case "$DESTINATION_TYPE" in + "FSx") DESTINATION_TYPE="FileSystem" ;; + "on-prem") DESTINATION_TYPE="OnPrem" ;; + *) echo "Error: Invalid destination type '$DESTINATION_TYPE'. Must be either 'FSx' or 'on-prem'." >&2 + usage ;; +esac +# +# Source the wf_utils file. +wf_utils=$(command -v wf_utils) +if [ -z "$wf_utils" ]; then + if [ ! -x "./wf_utils" ]; then + cat >&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication" \ + $tmpout $tmperr \ + '{"source": { + "type":"'${SOURCE_TYPE}'", + "id":"'${SOURCE_FS_ID}'", + "credentialsId":"'${SOURCE_CREDENTIALS_ID}'", + "region":"'${SOURCE_AWS_REGION}'", + "volumes": [ + { + "name":"'${SOURCE_VOLUME}'", + "svmName":"'${SOURCE_SVM_NAME}'", + "svmId":"'${SOURCE_SVM_ID}'" + } + ] + }, + "destination": { + "type":"'${DESTINATION_TYPE}'", + "id":"'${DESTINATION_FS_ID}'", + "credentialsId":"'${DESTINATION_CREDENTIALS_ID}'", + "region":"'${DESTINATION_AWS_REGION}'", + "svm": {"id":"'${DESTINATION_SVM_ID}'", "name":"'${DESTINATION_SVM_NAME}'"}, + "volume": {"tieringPolicy": {"coolingPeriod": 2, "name": "AUTO"}} + }, + "throttle":'${THROTTLE}', + "schedule": "'${SCHEDULE}'" + }' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_delete b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_delete new file mode 100755 index 0000000..a8d99af --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_delete @@ -0,0 +1,197 @@ +#!/bin/bash +# +################################################################################ +# This script is used to delete a SnapMirror relationship on a +# FSx for ONTAP file systems. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/delete_mirror-out.XXXXXX) +tmperr=$(mktemp /tmp/delete_mirror-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +SOURCE_TYPE="FSx" +DESTINATION_TYPE="FSx" +THROTTLE=102400 # 100 MiB/s +SCHEDULE="hourly" +while getopts "ht:a:s:C:R:F:Y:c:r:f:y:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + s) SNAPMIRROR_ID="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + y) DESTINATION_TYPE="$OPTARG" ;; + C) SOURCE_CREDENTIALS_ID="$OPTARG" ;; + R) SOURCE_AWS_REGION="$OPTARG" ;; + F) SOURCE_FS_ID="$OPTARG" ;; + Y) SOURCE_TYPE="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check if the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 + usage ;; +esac +case "$DESTINATION_TYPE" in + "FSx") DESTINATION_TYPE="FileSystem" ;; + "on-prem") DESTINATION_TYPE="OnPrem" ;; + *) echo "Error: Invalid destination type '$DESTINATION_TYPE'. Must be either 'FSx' or 'on-prem'." >&2 + usage ;; +esac +# +# Source the wf_utils file. +wf_utils=$(command -v wf_utils) +if [ -z "$wf_utils" ]; then + if [ ! -x "./wf_utils" ]; then + cat >&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${DESTINATION_CREDENTIALS_ID}/regions/${DESTINATION_AWS_REGION}/file-systems/${DESTINATION_FS_ID}/relationships/${SNAPMIRROR_ID}/delete" \ + $tmpout $tmperr \ + '{"source": { + "type":"'${SOURCE_TYPE}'", + "id":"'${SOURCE_FS_ID}'", + "credentialsId":"'${SOURCE_CREDENTIALS_ID}'", + "region":"'${SOURCE_AWS_REGION}'" + } + }' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_resync b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_resync new file mode 100755 index 0000000..56a8fe0 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_resync @@ -0,0 +1,155 @@ +#!/bin/bash +# +################################################################################ +# This script is used to re-sync a SnapMirror relationships on a +# FSx for ONTAP file systems. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/resync-mirror-out.XXXXXX) +tmperr=$(mktemp /tmp/resync-mirror-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:s:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + s) SNAPMIRROR_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${DESTINATION_CREDENTIALS_ID}/regions/${DESTINATION_AWS_REGION}/file-systems/${DESTINATION_FS_ID}/relationships/${SNAPMIRROR_ID}/re-sync" $tmpout $tmperr '{}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_reverse b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_reverse new file mode 100755 index 0000000..d885a3f --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_reverse @@ -0,0 +1,155 @@ +#!/bin/bash +# +################################################################################ +# This script is used to reverse a SnapMirror relationship on a +# FSx for ONTAP file system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# auth0 token. The file needs to either be in the command search path or in +# the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/reverse-snapmirror-out.XXXXXX) +tmperr=$(mktemp /tmp/reverse-snapmirror-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:s:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + s) SNAPMIRROR_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${DESTINATION_CREDENTIALS_ID}/regions/${DESTINATION_AWS_REGION}/file-systems/${DESTINATION_FS_ID}/relationships/${SNAPMIRROR_ID}/reverse" $tmpout $tmperr '{}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapmirror_update b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_update new file mode 100755 index 0000000..7620c3a --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapmirror_update @@ -0,0 +1,155 @@ +#!/bin/bash +# +################################################################################ +# This script is used to update a SnapMirror relationship on a +# FSx for ONTAP file system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/snapmirror-update-out.XXXXXX) +tmperr=$(mktemp /tmp/snapmirror-update-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:s:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) DESTINATION_CREDENTIALS_ID="$OPTARG" ;; + r) DESTINATION_AWS_REGION="$OPTARG" ;; + f) DESTINATION_FS_ID="$OPTARG" ;; + s) SNAPMIRROR_ID="$OPTARG" ;; + *) usage ;; + esac +done + +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/replication/${DESTINATION_CREDENTIALS_ID}/regions/${DESTINATION_AWS_REGION}/file-systems/${DESTINATION_FS_ID}/relationships/${SNAPMIRROR_ID}/update" $tmpout $tmperr '{}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/snapshot_create b/Management-Utilities/Workload-Factory-API-Samples/snapshot_create new file mode 100755 index 0000000..c122dc5 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/snapshot_create @@ -0,0 +1,169 @@ +#!/bin/bash +# +################################################################################ +# This script is used to create a snapshot of an volume in an FSx for ONTAP +# file system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/create_snapshot-out.XXXXXX) +tmperr=$(mktemp /tmp/create_snapshot-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:v:s:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + v) VOLUME_ID="$OPTARG" ;; + s) SNAPSHOT_NAME="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check if the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +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}/volumes/${VOLUME_ID}/snapshots" $tmpout $tmperr '{"name": "'"$SNAPSHOT_NAME"'"}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/volume_clone b/Management-Utilities/Workload-Factory-API-Samples/volume_clone new file mode 100755 index 0000000..c0200b8 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/volume_clone @@ -0,0 +1,177 @@ +#!/bin/bash +# +################################################################################ +# This script is used to clone a volume of an FSx for ONTAP file system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/clone_volume-out.XXXXXX) +tmperr=$(mktemp /tmp/clone_volume-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +while getopts "ht:a:c:r:f:v:s:n:" opt; do + case $opt in + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + v) VOLUME_ID="$OPTARG" ;; + s) SNAPSHOT_NAME="$OPTARG" ;; + n) CLONE_NAME="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check fi the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +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}/volumes/${VOLUME_ID}/clones" $tmpout $tmperr '{"snapshotName": "'"$SNAPSHOT_NAME"'", "name": "'"$CLONE_NAME"'"}' diff --git a/Management-Utilities/Workload-Factory-API-Samples/volume_delete b/Management-Utilities/Workload-Factory-API-Samples/volume_delete new file mode 100755 index 0000000..9a8b771 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/volume_delete @@ -0,0 +1,160 @@ +#!/bin/bash +# +################################################################################ +# This script is used to delete a volume of an FSx for ONTAP file system. +# +# It is dependent on the 'wf_utils' file that is included in this repo. That +# file contains the 'get_token' function that is used to obtain a valid +# access token that is needed to run the Workload Factory APIs. The file needs +# to either be in the command search path or in the current directory. +################################################################################ + +################################################################################ +# This function just prints the usage of this script and exits the program. +################################################################################ +usage() { + cat >&2 < + export BLUEXP_ACCOUNT_ID= + export CREDENTIALS_ID= + export AWS_REGION= +EOF + exit 1 +} + +################################################################################ +# Main logic starts here. +################################################################################ +tmpout=$(mktemp /tmp/clone_volume-out.XXXXXX) +tmperr=$(mktemp /tmp/clone_volume-err.XXXXXX) +trap 'rm -f $tmpout $tmperr' exit + +skipFinalBackup="skipFinalBackup=true" +while getopts "hbt:a:c:r:f:v:s:n:" opt; do + case $opt in + b) skipFinalBackup="skipFinalBackup=false" ;; + t) REFRESH_TOKEN="$OPTARG" ;; + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; + c) CREDENTIALS_ID="$OPTARG" ;; + r) AWS_REGION="$OPTARG" ;; + f) FILESYSTEM_ID="$OPTARG" ;; + v) VOLUME_ID="$OPTARG" ;; + *) usage ;; + esac +done +# +# Check fi the required parameters are set. +missing_args=false +if [ -z "$REFRESH_TOKEN" ]; then + cat >&2 <&2 <&2 <&2 <&2 <&2 <&2 < /dev/null; then + echo "Error: The required command '$cmd' was not found. Please install it." >&2 + exit 1 + fi +done + +token=$(get_token) +if [ -z "$token" ]; then + echo "Error: Failed to obtain an access token. Exiting." >&2 + exit 1 +fi + +run_curl "DELETE" "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/volumes/${VOLUME_ID}?${skipFinalBackup}" $tmpout $tmperr diff --git a/Management-Utilities/Workload-Factory-API-Samples/wf_utils b/Management-Utilities/Workload-Factory-API-Samples/wf_utils new file mode 100755 index 0000000..d2d8d59 --- /dev/null +++ b/Management-Utilities/Workload-Factory-API-Samples/wf_utils @@ -0,0 +1,169 @@ +#!/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" + tmpfile=$(mktemp -t create_token.XXXXXX) + trap 'rm -f "$tmpfile"' RETURN + + 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 wf_utils +%s)" + if [ $diff -gt $token_valid ]; then + createToken=true + fi + else + createToken=true + fi + + if [ $createToken == "true" ]; then + 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" + }' > $tmpfile 2>&1 + token=$(sed -ne 's/."access_token":"\([^"]*\).*/\1/p' < $tmpfile) + + 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 parameters: +# 1. HTTP method (GET or POST) +# 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 requests) +################################################################################ +run_curl () { + + method="$1" + token="$2" + url="$3" + output="$4" + errorOutput="$5" + data="$6" + 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 + case "$method" in + GET|get) + curl -sw "%{stderr}%{http_code},%{errormsg}" "$url" \ + -H "Accept:application/json, text/plain, */*" \ + -H "Authorization: Bearer $token" > $output 2> $errorOutput + exitCode=$? + ;; + POST|post) + curl -X POST -sw "%{stderr}%{http_code},%{errormsg}" "$url" \ + -H "Accept:application/json, text/plain, */*" \ + -H "Content-Type:application/json" \ + -H "Authorization: Bearer $token" --data "$data" > $output 2> $errorOutput + exitCode=$? + ;; + DELETE|delete) + curl -X DELETE -sw "%{stderr}%{http_code},%{errormsg}" "$url" \ + -H "Accept:application/json, text/plain, */*" \ + -H "Authorization: Bearer $token" > $output 2> $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 "" +} diff --git a/README.md b/README.md index 9e9fc5f..ee0afb1 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Have a great idea? We'd love to hear it! Please email us at [ng-fsxn-github-samp * [Rotate AWS Secrets Manager Secret](/Management-Utilities/fsxn-rotate-secret) * [FSx ONTAP iscsi volume creation automation for Windows](/Management-Utilities/iscsi-vol-create-and-mount) * [Warm Performance Tier](/Management-Utilities/warm_performance_tier) + * [Workload Factory API Samples](/Management-Utilities/Workload-Factory-API-Samples) * [Monitoring](/Monitoring) * [CloudWatch Dashboard for FSx for ONTAP](/Monitoring/CloudWatch-FSx) * [Export LUN metrics from an FSx ONTAP to Amazon CloudWatch](/Monitoring/LUN-monitoring)