|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script is used to list all the Workload Factory credentials the |
| 4 | +# user has 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 lists all the Workload Factory credentials you has access to. |
| 18 | +
|
| 19 | +Usage: $(basename $0) -t refresh_token -a blueXP_account_ID [-o] |
| 20 | +
|
| 21 | +Where: refresh_token - Is a refresh token used to obtain an access token needed |
| 22 | + to run the Workload Factory APIs. You can obtain a refresh |
| 23 | + token by going to https://services.cloud.netapp.com/refresh-token |
| 24 | + blueXP_account_ID - is the BlueXP account ID. You can find all the accounts |
| 25 | + you have access to by running the "list_bluexp_accts" script |
| 26 | + -o means to also show the ONTAP credentials |
| 27 | +
|
| 28 | +Instead of passing parameters on the command line, you can set the |
| 29 | +following environment variables: |
| 30 | +
|
| 31 | + export REFRESH_TOKEN=<refresh_token> |
| 32 | + export BLUEXP_ACCOUNT_ID=<blueXP_account_ID> |
| 33 | +EOF |
| 34 | + exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +################################################################################ |
| 38 | +# Main logic starts here. |
| 39 | +################################################################################ |
| 40 | +tmpout=$(mktemp /tmp/list_credentials-out.XXXXXX) |
| 41 | +tmpout2=$(mktemp /tmp/list_credentials-out2.XXXXXX) |
| 42 | +tmperr=$(mktemp /tmp/list_credentials-err.XXXXXX) |
| 43 | +trap 'rm -f $tmpout $tmpout2 $tmperr' exit |
| 44 | +# |
| 45 | +# Source the wf_utils file. |
| 46 | +wf_utils=$(command -v wf_utils) |
| 47 | +if [ -z "$wf_utils" ]; then |
| 48 | + if [ ! -x "./wf_utils" ]; then |
| 49 | + cat >&2 <<EOF |
| 50 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 51 | +It is required to run this script. You can download it from: |
| 52 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 53 | +EOF |
| 54 | + exit 1 |
| 55 | + else |
| 56 | + wf_utils=./wf_utils |
| 57 | + fi |
| 58 | +fi |
| 59 | +. "$wf_utils" |
| 60 | +# |
| 61 | +# Set defaults. |
| 62 | +filter="filter=$(urlencode "type eq 'AWS_ASSUME_ROLE'")" |
| 63 | +while getopts "ht:a:o" opt; do |
| 64 | + case $opt in |
| 65 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 66 | + a) BLUEXP_ACCOUNT="$OPTARG" ;; |
| 67 | + o) filter="";; # No filter, list all credentials. |
| 68 | + *) usage ;; |
| 69 | + esac |
| 70 | +done |
| 71 | +# |
| 72 | +# Check that all the parameters are set. |
| 73 | +if [ -z "$REFRESH_TOKEN" ]; then |
| 74 | + cat >&2 <<EOF |
| 75 | +Error: A BlueXP refresh tokon is required to run this script. |
| 76 | +Can you be obtain from this web page: |
| 77 | +
|
| 78 | + https://services.cloud.netapp.com/refresh-token |
| 79 | +
|
| 80 | +EOF |
| 81 | + usage |
| 82 | +fi |
| 83 | + |
| 84 | +if [ -z "$BLUEXP_ACCOUNT_ID" ]; then |
| 85 | + cat >&2 <<EOF |
| 86 | +Error: A BlueXP account ID is required to run this script. |
| 87 | +You can see the list of accounts you have access to by running the "list_bluexp_accts" script |
| 88 | +found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 89 | +
|
| 90 | +EOF |
| 91 | + usage |
| 92 | +fi |
| 93 | +# |
| 94 | +# Check that the required commands are available. |
| 95 | +for cmd in curl jq; do |
| 96 | + if ! command -v $cmd > /dev/null 2>&1; then |
| 97 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +done |
| 101 | + |
| 102 | +token=$(get_token) |
| 103 | +if [ -z "$token" ]; then |
| 104 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?$filter" $tmpout $tmperr |
| 109 | +jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout > $tmpout2 |
| 110 | +# |
| 111 | +# Check to see if there are more. |
| 112 | +nextToken=$(jq -r '.nextToken' $tmpout) |
| 113 | +while [ "$nextToken" != "null" ]; do |
| 114 | + run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/credentials/v1/credentials?nextToken=$nextToken&$filter" $tmpout $tmperr |
| 115 | + jq -r '.items[] | if(.type == "ONTAP") then "\(.metadata.fileSystemId) \(.type) \(.accountId) \(.id)" else "\(.metadata.name) \(.type) \(.credentials | split(":") | .[4]) \(.id)" end' $tmpout >> $tmpout2 |
| 116 | + nextToken=$(jq -r '.nextToken' $tmpout) |
| 117 | +done |
| 118 | + |
| 119 | +sort -f -k 2,2 -k 1,1 $tmpout2 | column -t -N "Name,Type,Account,ID" |
0 commit comments