|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script is used to list all the BlueXP resources that a user has |
| 4 | +# access to. |
| 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 | +# Display usage information then exits the script. |
| 14 | +################################################################################ |
| 15 | +usage () { |
| 16 | + cat >&2 <<EOF |
| 17 | +This script is used to list all the BlueXP resources that you have access to. |
| 18 | +
|
| 19 | +Usage is: $(basename $0) -t refresh_token -a BlueXP_account_ID |
| 20 | +Where: refresh_token - Is a refresh token used to obtain an access token needed |
| 21 | + to run the Workload Factory APIs. You can obtain a refresh |
| 22 | + token by going to https://services.cloud.netapp.com/refresh-token |
| 23 | + blueXP_account_ID - is the BlueXP account ID. Run 'list_bluexp_accts' to get a |
| 24 | + list of accounts you have access to. |
| 25 | +
|
| 26 | +Note, instead of passing parameters on the command line, you can set the |
| 27 | +following environment variables instead: |
| 28 | +
|
| 29 | + export REFRESH_TOKEN=<refresh_token> |
| 30 | + export BLUEXP_ACCOUNT_ID=<blueXP_account_ID> |
| 31 | +EOF |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +tmpout=/tmp/list_bluexp_resources-out.$$ |
| 36 | +tmpout2=/tmp/list_bluexp_resources-out2.$$ |
| 37 | +tmperr=/tmp/list_bluexp_resources-err.$$ |
| 38 | +trap 'rm -f $tmpout $tmpout2 $tmperr' exit |
| 39 | +# |
| 40 | +# Source the wf_utils file. |
| 41 | +wf_utils=$(command -v wf_utils) |
| 42 | +if [ -z "$wf_utils" ]; then |
| 43 | + if [ ! -x "./wf_utils" ]; then |
| 44 | + cat <<EOF >&2 |
| 45 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 46 | +It is required to run this script. You can download it from: |
| 47 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 48 | +EOF |
| 49 | + exit 1 |
| 50 | + else |
| 51 | + wf_utils=./wf_utils |
| 52 | + fi |
| 53 | +fi |
| 54 | +. "$wf_utils" |
| 55 | +# |
| 56 | +# Parse the command line options. |
| 57 | +while getopts ht:a: opt; do |
| 58 | + case $opt in |
| 59 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 60 | + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; |
| 61 | + *) usage ;; |
| 62 | + esac |
| 63 | +done |
| 64 | +# |
| 65 | +# Declare an array of required options and the error message to display if they are not set. |
| 66 | +declare -A required_options |
| 67 | +required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page: |
| 68 | + https://services.cloud.netapp.com/refresh-token\n\n' |
| 69 | + |
| 70 | +required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script. |
| 71 | +You can get the list of accounts you have access to by running the "list_bluexp_accts" script |
| 72 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n' |
| 73 | + |
| 74 | +check_required_options |
| 75 | +# |
| 76 | +# Check that the required commands are available. |
| 77 | +for cmd in jq curl; do |
| 78 | + if ! command -v $cmd &> /dev/null; then |
| 79 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | +done |
| 83 | + |
| 84 | +token=$(get_token) |
| 85 | +if [ -z "$token" ]; then |
| 86 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 87 | + exit 1 |
| 88 | +fi |
| 89 | + |
| 90 | +run_curl GET "$token" "https://api.bluexp.netapp.com/tenancy/resource?account=$BLUEXP_ACCOUNT_ID" $tmpout $tmperr |
| 91 | +if jq -r '.[] | "\(.resourceIdentifier);\(.resourceType);\(.resourceClass);\(.name);\(.metadata);\(.workspaceIds[0])"' $tmpout > $tmpout2 2> $tmperr; then |
| 92 | + cat $tmpout2 | column -s\; -t -N ID,Type,Class,name,metadata,workspaceId |
| 93 | +else |
| 94 | + echo "Error: Failed to parse the response from the API." >&2 |
| 95 | + cat $tmperr >&2 |
| 96 | + exit 1 |
| 97 | +fi |
0 commit comments