-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo
executable file
·83 lines (74 loc) · 2.52 KB
/
demo
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
export HIGHLIGHT_COLOR='\033[1;33m'
export EXAMPLE_COLOR='\033[1;32m'
export NO_COLOR='\033[0m'
export ERROR_COLOR='\033[1;31m'
export WARN_COLOR='\033[0;33m'
export INFO_COLOR='\033[1;34m'
if [[ -z "$1" ]]; then
printf "${ERROR_COLOR}No command argument. Expected: ${EXAMPLE_COLOR}demo [command]\n"
exit 1
fi
set -e # cause script to exit (stop) immediately if any one step fails
# Note the directory where this main script resides.
export REPO_DIRECTORY=$(dirname "$0")
# Clear environment variables with common names that may be set outside
# the script to avoid unexpectedly treating them as direct arguments.
unset PROJECT_ID
unset NETWORK_NAME
# Read command-line switches and populate local variables.
for var in "$@"
do
case "$var" in
--network=*)
export NETWORK_NAME="${var#*=}"
;;
--project=*)
PROJECT_ID="${var#*=}"
;;
--location=*)
APIGEE_INSTANCE_LOCATION="${var#*=}"
;;
--domain=*)
NEW_APIGEE_HOST_NAME="${var#*=}"
;;
esac
done
if [[ -z "${PROJECT_ID}" ]]; then
export PROJECT_ID=$(gcloud config get project)
if [[ -z "${PROJECT_ID}" ]]; then
printf "${ERROR_COLOR}Missing --project argument.${NO_COLOR}\n"
exit 1
fi
fi
export AUTH="Authorization: Bearer $(gcloud auth print-access-token)"
APIGEE_PEERING_RANGE=apigee-peering
APIGEE_INSTANCES=$(curl -H "$AUTH" -s "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/instances")
APIGEE_INSTANCE_JSON=$(echo $APIGEE_INSTANCES | jq -r ".instances[0]")
if ! [[ -z "$APIGEE_INSTANCE_JSON" || "$APIGEE_INSTANCE_JSON" = null ]]; then
# Apigee instance found.
if [[ -z "${NETWORK_NAME}" ]]; then
# We have an Apigee instance, but no network was specified.
# Let's infer network from peering for existing instance.
{
NETWORK_NAME=$(gcloud compute addresses list --project=$PROJECT_ID --format="value(network)" --filter="name=${APIGEE_PEERING_RANGE}" 2> /dev/null)
} || {
# Catching this error prevents the script from stopping here.
# Do some command as required for proper syntax here.
echo "" > /dev/null
}
fi
fi
# Run the command specified by the user.
# Apply a regex validation to avoid unsafe script/command execution.
if [[ $1 =~ ^[A-Za-z0-9_-]+$ ]]; then
DEMO_COMMAND_PATH=${REPO_DIRECTORY}/lib/$1
if test -f "$DEMO_COMMAND_PATH"; then
. ${DEMO_COMMAND_PATH}
else
printf "${ERROR_COLOR}Command not found: $1\n"
exit 1
fi
else
printf "${ERROR_COLOR}Invalid command: $1\n"
exit 1
fi