|
| 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 |
0 commit comments