|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +################################################################################ |
| 4 | +# This script is used to register a Lambda function as an Workload Factory link. |
| 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 | +# This function displays the usage of this script and exits. |
| 14 | +################################################################################ |
| 15 | +usage() { |
| 16 | + cat >&2 <<EOF |
| 17 | +This script is used to register a Lambda function as an Workload Factory link. |
| 18 | +
|
| 19 | +usage: $(basename $0) -t refresh_token -a blueXP_account_ID -n Name -l Lambda_ARN |
| 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. Run 'list_bluexp_accts' to get a |
| 25 | + list of accounts you have access to. |
| 26 | + Name - Is the name you want associated with the Link within the Workload Factory UI. |
| 27 | + Lambda_ARN - Is the ARN of the AWS Lambda function. |
| 28 | +
|
| 29 | +Instead of passing parameters on the command line, you can set the |
| 30 | +following environment variables: |
| 31 | +
|
| 32 | + export REFRESH_TOKEN=<refresh_token> |
| 33 | + export BLUEXP_ACCOUNT_ID=<blueXP_account_ID> |
| 34 | +EOF |
| 35 | + exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +################################################################################ |
| 39 | +# Main logic starts here. |
| 40 | +################################################################################ |
| 41 | + |
| 42 | +tmpout=$(mktemp /tmp/list_filesystems-out.XXXXXX) |
| 43 | +tmpout2=$(mktemp /tmp/list_filesystems-out2.XXXXXX) |
| 44 | +tmperr=$(mktemp /tmp/list_filesystems-err.XXXXXX) |
| 45 | +trap 'rm -f $tmpout $tmpout2 $tmperr' exit |
| 46 | + |
| 47 | +while getopts "ht:a:l:n:" opt; do |
| 48 | + case $opt in |
| 49 | + t) REFRESH_TOKEN="$OPTARG" ;; |
| 50 | + a) BLUEXP_ACCOUNT_ID="$OPTARG" ;; |
| 51 | + l) LAMBDA_ARN="$OPTARG" ;; |
| 52 | + n) NAME="$OPTARG" ;; |
| 53 | + *) usage ;; |
| 54 | + esac |
| 55 | +done |
| 56 | +# |
| 57 | +# Check that all the required parameters are set. |
| 58 | +missingParmeter="false" |
| 59 | +if [ -z "$REFRESH_TOKEN" ]; then |
| 60 | + cat >&2 <<EOF |
| 61 | +Error: A BlueXP refresh tokon is required to run this script. It can be obtain from this web page: |
| 62 | +
|
| 63 | + https://services.cloud.netapp.com/refresh-token |
| 64 | +EOF |
| 65 | + missingParmeter=true |
| 66 | +fi |
| 67 | + |
| 68 | +if [ -z "$BLUEXP_ACCOUNT_ID" ]; then |
| 69 | + cat >&2 <<EOF |
| 70 | +Erorr: 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 |
| 73 | +
|
| 74 | +EOF |
| 75 | + missingParmeter=true |
| 76 | +fi |
| 77 | + |
| 78 | +if [ -z "$NAME" ]; then |
| 79 | + cat >&2 <<EOF |
| 80 | +Error: You must provide a name for the link to register. |
| 81 | +EOF |
| 82 | + missingParmeter=true |
| 83 | +fi |
| 84 | + |
| 85 | +if [ -z "$LAMBDA_ARN" ]; then |
| 86 | + cat >&2 <<EOF |
| 87 | +Error: The ARN of the AWS Lambda function is required to run this script. |
| 88 | +You can get the ARN from the AWS console. |
| 89 | +EOF |
| 90 | + missingParmeter=true |
| 91 | +fi |
| 92 | + |
| 93 | +if [ "$missingParmeter" != "false" ]; then |
| 94 | + usage |
| 95 | +fi |
| 96 | +# |
| 97 | +# Source the wf_utils file. |
| 98 | +wf_utils=$(command -v wf_utils) |
| 99 | +if [ -z "$wf_utils" ]; then |
| 100 | + if [ ! -x "./wf_utils" ]; then |
| 101 | + cat >&2 <<EOF |
| 102 | +Error: The 'wf_utils' script was not found in the current directory or in the command search path. |
| 103 | +It is required to run this script. You can download it from: |
| 104 | +https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples |
| 105 | +EOF |
| 106 | + exit 1 |
| 107 | + else |
| 108 | + wf_utils=./wf_utils |
| 109 | + fi |
| 110 | +fi |
| 111 | +. "$wf_utils" |
| 112 | +# |
| 113 | +# Check that the required commands are available. |
| 114 | +for cmd in jq curl; do |
| 115 | + if ! command -v $cmd &> /dev/null; then |
| 116 | + echo "Error: The required command '$cmd' was not found. Please install it." >&2 |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | +done |
| 120 | +# |
| 121 | +# Get the token to use for the API call. |
| 122 | +token=$(get_token) |
| 123 | +if [ -z "$token" ]; then |
| 124 | + echo "Error: Failed to obtain an access token. Exiting." >&2 |
| 125 | + exit 1 |
| 126 | +fi |
| 127 | +run_curl POST "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/links/v1/links" $tmpout $tmperr '{"type": "lambda", "name": "'${NAME}'", "arn": "'${LAMBDA_ARN}'"}' |
0 commit comments