|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script is used to list all the BlueXP members that are |
| 4 | +# associated with the BlueXP account. |
| 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 members that are |
| 18 | +associated with the BlueXP account. |
| 19 | +
|
| 20 | +Usage is: $(basename $0) -t refresh_token -u bluexp_account_uuid |
| 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_uuid - The BlueXP account UUID to list the members for. You can |
| 26 | + find all the accounts you have access to by running the |
| 27 | + "list_bluexp_accts" script. |
| 28 | + -h - Displays this help message. |
| 29 | +
|
| 30 | +Note, instead of passing parameters on the command line, you can set the |
| 31 | +following environment variables instead: |
| 32 | +
|
| 33 | + export REFRESH_TOKEN=<refresh_token> |
| 34 | + export BLUEXP_ACCOUNT_UUID=<bluexp_account_uuid> |
| 35 | +EOF |
| 36 | + exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +tmpout=/tmp/list_accounts-out.$$ |
| 40 | +tmperr=/tmp/list_accounts-err.$$ |
| 41 | +trap 'rm -f $tmpout $tmperr' exit |
| 42 | + |
| 43 | +while getopts ht:u: opt; do |
| 44 | + case $opt in |
| 45 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 46 | + u) BLUEXP_ACCOUNT_UUID="$OPTARG" ;; |
| 47 | + *) usage ;; |
| 48 | + esac |
| 49 | +done |
| 50 | +# |
| 51 | +# Source the wf_utils file. |
| 52 | +wf_utils=$(command -v wf_utils) |
| 53 | +if [ -z "$wf_utils" ]; then |
| 54 | + if [ ! -x "./wf_utils" ]; then |
| 55 | + cat <<EOF >&2 |
| 56 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 57 | +It is required to run this script. You can download it from: |
| 58 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 59 | +EOF |
| 60 | + exit 1 |
| 61 | + else |
| 62 | + wf_utils=./wf_utils |
| 63 | + fi |
| 64 | +fi |
| 65 | +. "$wf_utils" |
| 66 | +# |
| 67 | +# Check that all the parameters are set. |
| 68 | +if [ -z "$REFRESH_TOKEN" ]; then |
| 69 | + cat >&2 <<EOF |
| 70 | +Error: A BlueXP refresh tokon is required to run this script. |
| 71 | +Can you be obtain from this web page: |
| 72 | +
|
| 73 | + https://services.cloud.netapp.com/refresh-token |
| 74 | +
|
| 75 | +EOF |
| 76 | + usage |
| 77 | +fi |
| 78 | + |
| 79 | +if [ -z "$BLUEXP_ACCOUNT_UUID" ]; then |
| 80 | + cat >&2 <<EOF |
| 81 | +Error: A BlueXP account UUID is required to run this script. |
| 82 | +You can see the list of accounts you have access to by running the "list_bluexp_accts" script |
| 83 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 84 | +
|
| 85 | +EOF |
| 86 | + usage |
| 87 | +fi |
| 88 | +# |
| 89 | +# Check that the required commands are available. |
| 90 | +for cmd in jq curl; do |
| 91 | + if ! command -v $cmd &> /dev/null; then |
| 92 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +done |
| 96 | + |
| 97 | +token=$(get_token) |
| 98 | +if [ -z "$token" ]; then |
| 99 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 100 | + exit 1 |
| 101 | +fi |
| 102 | + |
| 103 | +run_curl GET "$token" "https://api.bluexp.netapp.com/v1/management/organizations/$BLUEXP_ACCOUNT_UUID/users?limit=1000&filter=userType%20ne%20%27agent%27" $tmpout $tmperr "" "application/vnd.netapp.bxp.users.extended+json" |
| 104 | +count=$(jq -r '.count' $tmpout 2> /dev/null) |
| 105 | +if [[ "$count" == 0 ]]; then |
| 106 | + echo "No members found for the specified BlueXP account UUID: $BLUEXP_ACCOUNT_UUID." >&2 |
| 107 | + echo "Did you provide the UUID or the iD? This script needs the UUID." >&2 |
| 108 | + exit 0 |
| 109 | +fi |
| 110 | + |
| 111 | +if jq -r '.items[] | .name + "," + if(.userType == "user") then .userType + "," + .id else .userType + "," + .auth0Id end + "," + .email' $tmpout > $tmperr; then |
| 112 | + cat $tmperr | sort -f | column -s, -t -N Name,Type,ID,Email |
| 113 | +else |
| 114 | + echo "Error: Failed to parse the response from the API." >&2 |
| 115 | + exit 1 |
| 116 | +fi |
0 commit comments