|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script adds a filesystem to a BlueXP workspace and sets the filesystem |
| 4 | +# password based on the secret stored in AWS Secrets Manager. The secret must |
| 5 | +# be of "other" type with two fields (case sensitive): username and password. |
| 6 | +# The username field is optional and defaults to fsxadmin if not present. |
| 7 | +# |
| 8 | +################################################################################ |
| 9 | +# Display usage information then exists the script. |
| 10 | +################################################################################ |
| 11 | +usage () { |
| 12 | + cat 1>&2 <<EOF |
| 13 | +Usage: $(basename $0) -t REFRESH_TOKEN -a blueXP_BLUE_ACCOUNT_ID -c CREDENTIALS_ID -r aws_AWS_REGION -f FILESYSTEM_ID -C CONNECTOR_ID -w WORKSPACE_ID -s SECRET_NAME -u USERNAME -p PASSWORD [-P] |
| 14 | +
|
| 15 | +Where: REFRESH_TOKEN - Is a refresh token used to obtain an access token needed |
| 16 | + to run the Workload Factory APIs. You can obtain a refresh |
| 17 | + token by going to https://services.cloud.netapp.com/refresh-token |
| 18 | + blueXP_BLUE_ACCOUNT_ID - is the BlueXP account ID. Run 'list_bluexp_accts' to get a |
| 19 | + list of accounts you have access to |
| 20 | + CREDENTIALS_ID - is the Workload Factory credentials ID for the AWS account. Run |
| 21 | + 'list_credentials' to get a list of credentials you have access to |
| 22 | + aws_AWS_REGION - is the AWS AWS_REGION where the FSx file systems are located |
| 23 | + FILESYSTEM_ID - is the AWS file system ID of the FSx file system where the volume resides |
| 24 | + CONNECTOR_ID - The BlueXP connector ID. Run 'list_bluexp_connectors' to get a list |
| 25 | + of connectors you have access to |
| 26 | + WORKSPACE_ID - The BlueXP workspace ID. Run 'list_bluexp_workspaces' to get a list of workspaces |
| 27 | + you have access to. |
| 28 | + SECRET_NAME - The name of the secret that holds the credentials to assoicate with the FSxN. |
| 29 | + USERNAME - The username to set the password for (defaults to fsxadmin if not specified). |
| 30 | + PASSWORD - The password to set for the username. |
| 31 | + -P - Means to just try to set the password. |
| 32 | +
|
| 33 | +Instead of passing parameters on the command line, you can set the following |
| 34 | +environment variables: |
| 35 | +
|
| 36 | + export REFRESH_TOKEN=<REFRESH_TOKEN> |
| 37 | + export BLUEXP_BLUE_ACCOUNT_ID=<blueXP_BLUE_ACCOUNT_ID> |
| 38 | + export CREDENTIALS_ID=<CREDENTIALS_ID> |
| 39 | + export AWS_AWS_REGION=<aws_AWS_REGION> |
| 40 | + export USERNAME=<username> |
| 41 | + export PASSWORD=<password> |
| 42 | +
|
| 43 | +NOTES: |
| 44 | +o You must provide either a SECRET_NAME or a passowrd. |
| 45 | +o If you don't provide a username, it will default to fsxadmin unless |
| 46 | + you use a secret and the secret has a username field. |
| 47 | +o If you provide a username and a secret, the username provided |
| 48 | + will be used and the username field in the secret will be ignored. |
| 49 | +o The secret must be of "other" type with two fields: username and password. |
| 50 | +EOF |
| 51 | + exit 1 |
| 52 | +} |
| 53 | + |
| 54 | +################################################################################ |
| 55 | +# Main logic starts here. |
| 56 | +################################################################################ |
| 57 | +tmpout=$(mktemp /tmp/add_fsxn_to_bluexp-out.XXXXXX) |
| 58 | +tmperr=$(mktemp /tmp/add_fsxn_to_bluexp-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 | +# Parse the command line options. |
| 78 | +skip_add=false |
| 79 | +while getopts "ht:a:c:r:f:C:s:w:u:p:P" opt; do |
| 80 | + case ${opt} in |
| 81 | + t) REFRESH_TOKEN=$OPTARG ;; |
| 82 | + a) BLUEXP_ACCOUNT_ID=$OPTARG ;; |
| 83 | + c) CREDENTIALS_ID=$OPTARG ;; |
| 84 | + r) AWS_REGION=$OPTARG ;; |
| 85 | + f) FILESYSTEM_ID=$OPTARG ;; |
| 86 | + C) CONNECTOR_ID=$OPTARG ;; |
| 87 | + s) SECRET_NAME=$OPTARG ;; |
| 88 | + w) WORKSPACE_ID=$OPTARG ;; |
| 89 | + u) USERNAME=$OPTARG ;; |
| 90 | + p) PASSWORD=$OPTARG ;; |
| 91 | + P) skip_add=true ;; |
| 92 | + *) usage ;; |
| 93 | + esac |
| 94 | +done |
| 95 | +# |
| 96 | +# Declare an array of required options and the error message to display if they are not set. |
| 97 | +declare -A required_options |
| 98 | +required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page: |
| 99 | + https://services.cloud.netapp.com/refresh-token\n\n' |
| 100 | +required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script. |
| 101 | +You can get the list of accounts you have access to by running the "list_bluexp_accts" script |
| 102 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 103 | +required_options["CREDENTIALS_ID"]='Error: The ID of the credentials to delete is required. |
| 104 | +You can get a list of credentials by running the "list_credentials" script |
| 105 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 106 | +required_options["AWS_REGION"]='Error: The AWS region where the file system is located is required.\n\n' |
| 107 | +required_options["FILESYSTEM_ID"]='Error: The ID of the FSxN file system is required.\n\n' |
| 108 | +required_options["CONNECTOR_ID"]='Error: A BlueXP connector ID is required to run this script. |
| 109 | +You can get the list of connectors you have access to by running the "list_bluexp_connectors" script |
| 110 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 111 | +required_options["WORKSPACE_ID"]='Error: A BlueXP workspace ID is required to run this script. |
| 112 | +You can get the list of workspaces you have access to by running the "list_bluexp_workspaces" script |
| 113 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 114 | + |
| 115 | +check_required_options |
| 116 | + |
| 117 | +if [ -z "$SECRET_NAME" -a -z "$PASSWORD" ]; then |
| 118 | + echo "Error: You must provide either a secret name or password." >&2 |
| 119 | + usage |
| 120 | +fi |
| 121 | +# |
| 122 | +# If the username and/or password were not provide, get them from the secret. |
| 123 | +if [ -z "$USERNAME" -a ! -z "$SECRET_NAME" ]; then |
| 124 | + USERNAME=$(aws secretsmanager --region $AWS_REGION get-secret-value --secret-id $SECRET_NAME --query SecretString --output text | sed -ne 's/.*"username":"\([^"]*\).*/\1/p') |
| 125 | +fi |
| 126 | +if [ -z "$USERNAME" ]; then |
| 127 | + USERNAME="fsxadmin" |
| 128 | +fi |
| 129 | +if [ -z "$PASSWORD" ]; then |
| 130 | + PASSWORD=$(aws secretsmanager --region $AWS_REGION get-secret-value --secret-id $SECRET_NAME --query SecretString --output text | sed -ne 's/.*"password":"\([^"]*\).*/\1/p') |
| 131 | +fi |
| 132 | +if [ -z "$PASSWORD" ]; then |
| 133 | + echo "Failed to get the password from the secret." >&2 |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | +# |
| 137 | +# Check that the required commands are available. |
| 138 | +for cmd in jq curl; do |
| 139 | + if ! command -v $cmd &> /dev/null; then |
| 140 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 141 | + exit 1 |
| 142 | + fi |
| 143 | +done |
| 144 | +# |
| 145 | +# Get an access token. |
| 146 | +token=$(get_token) |
| 147 | +if [ -z "$token" ]; then |
| 148 | + echo "Failed to get a token." |
| 149 | + exit 1 |
| 150 | +fi |
| 151 | +# |
| 152 | +# Add the filesystem to the BlueXP workspace. |
| 153 | +if [ "$skip_add" == false ]; then |
| 154 | + echo -n "Adding $FILESYSTEM_ID to BlueXP workspace $WORKSPACE_ID..." |
| 155 | + run_curl "POST" "$token" "https://api.workloads.netapp.com/accounts/$BLUEXP_ACCOUNT_ID/fsx/v2/credentials/$CREDENTIALS_ID/regions/$AWS_REGION/bluexp/register-file-systems?workspaceId=$WORKSPACE_ID" $tmpout $tmperr '["'$FILESYSTEM_ID'"]' 'application/json' 'x-bluexp-source: true' "x-agent-id: $CONNECTOR_ID" |
| 156 | + echo "Done." |
| 157 | +fi |
| 158 | +# |
| 159 | +# Set the filesystem password. |
| 160 | +echo -n "Setting password for $USERNAME on $FILESYSTEM_ID..." |
| 161 | +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": "'$USERNAME'", "password":"'$PASSWORD'","resetFsxAdminPassword":false}' 'application/json' 'x-bluexp-source: true' "x-agent-id: $CONNECTOR_ID" |
| 162 | +echo "Done." |
0 commit comments