|
| 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/clone_volume-out.XXXXXX) |
| 47 | +tmperr=$(mktemp /tmp/clone_volume-err.XXXXXX) |
| 48 | +trap 'rm -f $tmpout $tmperr' exit |
| 49 | + |
| 50 | +while getopts "ht:a:c:r:f:l:" opt; do |
| 51 | + case $opt in |
| 52 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 53 | + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; |
| 54 | + c) CREDENTIALS_ID="$OPTARG" ;; |
| 55 | + r) AWS_REGION="$OPTARG" ;; |
| 56 | + f) FILESYSTEM_ID="$OPTARG" ;; |
| 57 | + l) LINK_ID="$OPTARG" ;; |
| 58 | + *) usage ;; |
| 59 | + esac |
| 60 | +done |
| 61 | +# |
| 62 | +# Check fi the required parameters are set. |
| 63 | +missing_args=false |
| 64 | +if [ -z "$REFRESH_TOKEN" ]; then |
| 65 | + cat >&2 <<EOF |
| 66 | +Error: A BlueXP refresh tokon is required to run this script. |
| 67 | +Can you be obtain from this web page: |
| 68 | +
|
| 69 | + https://services.cloud.netapp.com/refresh-token |
| 70 | +
|
| 71 | +EOF |
| 72 | + missing_args=true |
| 73 | +fi |
| 74 | + |
| 75 | +if [ -z "$BLUEXP_ACCOUNT_ID" ]; then |
| 76 | + cat >&2 <<EOF |
| 77 | +Error: A BlueXP account is required to run this script. |
| 78 | +You can get the list of accounts you have access to by running the "list_bluexp_accts" script |
| 79 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 80 | +
|
| 81 | +EOF |
| 82 | + missing_args=true |
| 83 | +fi |
| 84 | + |
| 85 | +if [ -z "$CREDENTIALS_ID" ]; then |
| 86 | + cat >&2 <<EOF |
| 87 | +Error: A Workload Factory credentials ID is required to run this script. |
| 88 | +You can get the list of credentials you have access to by running the "list_credentials" script |
| 89 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 90 | +
|
| 91 | +EOF |
| 92 | + missing_args=true |
| 93 | +fi |
| 94 | + |
| 95 | +if [ -z "$AWS_REGION" ]; then |
| 96 | + cat >&2 <<EOF |
| 97 | +Error: The AWS region where the file system is located is required to run this script. |
| 98 | +
|
| 99 | +EOF |
| 100 | + missing_args=true |
| 101 | +fi |
| 102 | + |
| 103 | +if [ -z "$FILESYSTEM_ID" ]; then |
| 104 | + cat >&2 <<EOF |
| 105 | +Error: An FSx file system ID is required to run this script. |
| 106 | +You can get the list of file systems you have access to by running the "list_filesystems" script |
| 107 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 108 | +
|
| 109 | +EOF |
| 110 | + missing_args=true |
| 111 | +fi |
| 112 | + |
| 113 | +if [ -z "$LINK_ID" ]; then |
| 114 | + cat >&2 <<EOF |
| 115 | +Error: The link ID is required to run this script. |
| 116 | +You can get the list of links associated with a file systems by running the "list_links" script |
| 117 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 118 | +
|
| 119 | +EOF |
| 120 | + missing_args=true |
| 121 | +fi |
| 122 | + |
| 123 | +if [ "$missing_args" != "false" ]; then |
| 124 | + usage |
| 125 | +fi |
| 126 | +# |
| 127 | +# Source the wf_utils file. |
| 128 | +wf_utils=$(command -v wf_utils) |
| 129 | +if [ -z "$wf_utils" ]; then |
| 130 | + if [ ! -x "./wf_utils" ]; then |
| 131 | + cat >&2 <<EOF |
| 132 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 133 | +It is required to run this script. You can download it from: |
| 134 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 135 | +EOF |
| 136 | + exit 1 |
| 137 | + else |
| 138 | + wf_utils=./wf_utils |
| 139 | + fi |
| 140 | +fi |
| 141 | +. "$wf_utils" |
| 142 | +# |
| 143 | +# Check that the required commands are available. |
| 144 | +for cmd in jq curl; do |
| 145 | + if ! command -v $cmd &> /dev/null; then |
| 146 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | +done |
| 150 | + |
| 151 | +token=$(get_token) |
| 152 | +if [ -z "$token" ]; then |
| 153 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 154 | + exit 1 |
| 155 | +fi |
| 156 | + |
| 157 | +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