-
Notifications
You must be signed in to change notification settings - Fork 3
/
mc_support
executable file
·86 lines (77 loc) · 2.23 KB
/
mc_support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash -ue
# Hit a backend support endpoint.
usage() {
(
echo "usage: ${0##*/} [options] ENDPOINT [<<EOF ... post body ... EOF]"
echo "Hit a backend support endpoint."
echo
echo "Note: Ensure that you've authenticated with onelogin-aws and are on the VPN."
echo
echo "options:"
(
echo " -h, --help@ show usage help"
echo " -e ENV@ environment to query (default: dev)"
echo " -X METHOD@ HTTP method to use (default: POST)"
echo " -d@ dry run; show the curl command that would be run"
) | column -ts@
echo
echo "Example:"
echo
echo 'mc_support -e dev -XPOST "/support/endpoint?q=whatever" <<EOF'
echo "{"
echo ' "key": "value"'
echo "}"
echo "EOF"
) 1>&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b' || [[ -z $1 ]]; then
usage
fi
# shellcheck source=../.shell_control
# shellcheck disable=SC1091
source "$HOME/.shell_control" || echo "$(tput bold)error: ~/.shell_control not installed!$(tput sgr0)" >&2
ENV=${ENV:-dev}
METHOD="POST"
DRYRUN=0
while getopts "he:X:d" opt; do
case "$opt" in
h) usage ;;
e) ENV="$OPTARG" ;;
X) METHOD="$OPTARG" ;;
d) DRYRUN=1 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
PROXY_URL="https://cli.$ENV.mcinfra.io"
ENDPOINT="$1"
if [[ $METHOD == "POST" ]]; then
POST_BODY=""
if [[ ! -t 0 ]]; then
while read -r line; do
POST_BODY="${POST_BODY}${line}"
done
fi
if [[ $METHOD == "POST" && -n $POST_BODY ]]; then
show "Using POST Body: $POST_BODY"
fi
fi
CURL_CMD="awscurl --profile '$ENV'"
CURL_CMD="$CURL_CMD -XPOST '${PROXY_URL}${ENDPOINT}"
if [[ "?" =~ $ENDPOINT ]]; then
CURL_CMD="$CURL_CMD&method=${METHOD}'"
else
CURL_CMD="$CURL_CMD?method=${METHOD}'"
fi
CURL_CMD="$CURL_CMD -H 'Accept: application/json'"
CURL_CMD="$CURL_CMD -H 'Content-Type: application/json'"
if [[ $METHOD == "POST" && -n $POST_BODY ]]; then
CURL_CMD="$CURL_CMD -H 'Content-Type: application/json'"
CURL_CMD="$CURL_CMD -d '$(echo "$POST_BODY" | jq -c)'"
fi
CURL_CMD="$CURL_CMD; echo"
show "$CURL_CMD"
if [[ $DRYRUN -eq 0 ]]; then
run "$CURL_CMD"
fi