Add projects capability to a2 CLI utility
#!/bin/bash
# Purpose:
-# Provide a very concise way to fetch collections of a2 entities at the API level.
+# Provide a very concise way to fetch collections of a2 auth entities at the API level.
# This works either inside or outside Habitat Studio.
#
# Usage:
-# a2 [ -o | --one-line ] [ -s | --show ] [ -2 | --v2 ] <collection> [ <name> <value> ]
+# a2 [ <options> ] <collection> [ <name> <value> ]
#
-# <collection> may be "users", "teams", "policies", or "tokens".
+# <collection> may be "users", "teams", "policies", "tokens", "projects", or "roles".
# Optionally specify a property <name> and <value> if you wish to emit just a single entity.
#
# Options:
# -o or --one-line output each entity on one-line
# -s or --show show the executed curl command
# -2 or --v2 use Auth V2 endpoints (pre-release, not to be disclosed to customers)
+# -p or --projects project filter; a comma-separate list as a single argument
#
# Prerequisites:
# jq preprocessor
# a2 --one users
# a2 -s users name bob
# a2 -o --show users name bob
+# a2 --v2 -o --show -p proj1,proj2 roles
#
# To run on a different environment you can adjust the env vars for a single invocation. Example:
# TARGET_HOST=https://a2-local-fresh-install-dev.cd.chef.co TOK=stV8b5NqP6qepD9I23OFn_U2OME= a2 -s policies
@@ -44,11 +46,13 @@ a2 () {
jq_option=
show_cmd=
auth_path="api/v0/auth"
+ projects=""
while [[ "$1" =~ ^- ]]; do
case "$1" in
-o|--one-line) jq_option="-c"; shift;;
-s|--show) show_cmd=1; shift;;
-2|--v2) auth_path="apis/iam/v2beta"; shift;;
+ -p|--projects) projects="projects: $2"; shift 2;;
-*) echo "'$1' unknown"; return 1;;
esac
done
@@ -64,8 +68,8 @@ a2 () {
# execute
if [ "$show_cmd" == 1 ]; then
- echo "curl -sSkH \"api-token: $TOK\" \"$TARGET_HOST/$auth_path/$1\" | jq $jq_option '$jq_script'"
+ echo "curl -sSkH \"api-token: \$TOK\" -H \"$projects\" \"$TARGET_HOST/$auth_path/$1\" | jq $jq_option '$jq_script'"
fi
- curl -sSkH "api-token: $TOK" "$TARGET_HOST/$auth_path/$1" | jq $jq_option "$jq_script"
+ curl -sSkH "api-token: $TOK" -H "$projects" "$TARGET_HOST/$auth_path/$1" | jq $jq_option "$jq_script"
}
