|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +################################################################################ |
| 4 | +# This script is used to list all the cifs shares associated with a volume on |
| 5 | +# an ONTAP file system. |
| 6 | +# |
| 7 | +# It is dependent on the 'wf_utils' file that is included in this repo. That |
| 8 | +# file contains the 'get_token' function that is used to obtain a valid |
| 9 | +# access token that is needed to run the Workload Factory APIs. The file needs |
| 10 | +# to either be in the command search path or in the current directory. |
| 11 | +################################################################################ |
| 12 | + |
| 13 | +################################################################################ |
| 14 | +# This function just prints the usage of this script and exits the program. |
| 15 | +################################################################################ |
| 16 | +usage() { |
| 17 | + cat >&2 <<EOF |
| 18 | +This script is used to list all the cifs shares associated with a volume on |
| 19 | +an ONTAP file system. |
| 20 | +
|
| 21 | +Usage: $(basename $0) -t refresh_token -a blueXP_account_ID -c credentials_ID -r aws_region -f filesystem_ID -v volume_ID |
| 22 | +
|
| 23 | +Where: refresh_token - Is a refresh token used to obtain an access token needed |
| 24 | + to run the Workload Factory APIs. You can obtain a refresh |
| 25 | + token by going to https://services.cloud.netapp.com/refresh-token |
| 26 | + blueXP_account_ID - is the BlueXP account ID. Run 'list_bluexp_accts' to get a |
| 27 | + list of accounts you have access to |
| 28 | + credentials_ID - is the Workload Factory credentials ID for the AWS account. Run |
| 29 | + 'list_credentials' to get a list of credentials you have access to |
| 30 | + aws_region - is the AWS region where the FSx file systems are located |
| 31 | + filesystem_id - is the AWS file system ID of the FSx file system where the volume resides |
| 32 | + volume_ID - is the AWS volume ID of the volume where you want to create the cifs share. |
| 33 | +
|
| 34 | +Instead of passing parameters on the command line, you can set the |
| 35 | +following environment variables: |
| 36 | +
|
| 37 | + export REFRESH_TOKEN=<refresh_token> |
| 38 | + export BLUEXP_ACCOUNT_ID=<blueXP_account_ID> |
| 39 | + export CREDENTIALS_ID=<credentials_ID> |
| 40 | + export AWS_REGION=<aws_region> |
| 41 | +EOF |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +################################################################################ |
| 46 | +# Main logic starts here. |
| 47 | +################################################################################ |
| 48 | +tmpout=$(mktemp /tmp/create_share-out.XXXXXX) |
| 49 | +tmperr=$(mktemp /tmp/create_share-err.XXXXXX) |
| 50 | +trap 'rm -f $tmpout $tmperr' exit |
| 51 | +# |
| 52 | +# Source the wf_utils file. |
| 53 | +wf_utils=$(command -v wf_utils) |
| 54 | +if [ -z "$wf_utils" ]; then |
| 55 | + if [ ! -x "./wf_utils" ]; then |
| 56 | + cat >&2 <<EOF |
| 57 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 58 | +It is required to run this script. You can download it from: |
| 59 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 60 | +EOF |
| 61 | + exit 1 |
| 62 | + else |
| 63 | + wf_utils=./wf_utils |
| 64 | + fi |
| 65 | +fi |
| 66 | +. "$wf_utils" |
| 67 | +# |
| 68 | +# Process command line options. |
| 69 | +while getopts "ht:a:c:r:f:v:" opt; do |
| 70 | + case $opt in |
| 71 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 72 | + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; |
| 73 | + c) CREDENTIALS_ID="$OPTARG" ;; |
| 74 | + r) AWS_REGION="$OPTARG" ;; |
| 75 | + f) FILESYSTEM_ID="$OPTARG" ;; |
| 76 | + v) VOLUME_ID="$OPTARG" ;; |
| 77 | + *) usage ;; |
| 78 | + esac |
| 79 | +done |
| 80 | +# |
| 81 | +# Declare an array of required options and the error message to display if they are not set. |
| 82 | +declare -A required_options |
| 83 | +required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh token is required to run this script. It can be obtain from this web page: |
| 84 | + https://services.cloud.netapp.com/refresh-token\n\n' |
| 85 | +required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script. |
| 86 | +You can get the list of accounts you have access to by running the "list_bluexp_accts" script |
| 87 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 88 | +required_options["CREDENTIALS_ID"]='Error: A Worload Factory Credential ID is required to run this script. |
| 89 | +You can get a list of credentials by running the "list_credentials" script |
| 90 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 91 | +required_options["AWS_REGION"]='Error: The AWS region where the file system is located is required.\n\n' |
| 92 | +required_options["FILESYSTEM_ID"]='Error: The ID of the FSxN file system is required.\n\n' |
| 93 | +required_options["VOLUME_ID"]='Error: An AWS Volume ID is required to run this script. |
| 94 | +You can get the list of file systems you have access to by running the "list_volumes" script |
| 95 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 96 | + |
| 97 | +check_required_options |
| 98 | +# |
| 99 | +# Check if the required commands are available. |
| 100 | +for cmd in jq curl; do |
| 101 | + if ! command -v $cmd &> /dev/null; then |
| 102 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +done |
| 106 | + |
| 107 | +token=$(get_token) |
| 108 | +if [ -z "$token" ]; then |
| 109 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | +# |
| 113 | +# Get the existing shares and the junction path |
| 114 | +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/${VOLUME_ID}?include=cifsShares" $tmpout $tmperr |
| 115 | +jq -r '.cifsShares[] | "\(.name),\(.path)"' $tmpout | column -t -s, -N "Share Name,Path" |
| 116 | + |
0 commit comments