-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp-list-projects-in-folder.sh
executable file
·39 lines (31 loc) · 1.23 KB
/
gcp-list-projects-in-folder.sh
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
#!/usr/bin/env bash
. "$(dirname "$BASH_SOURCE")/lib/parse_args.sh"
REQUIRED=("folder-id")
parse_args __DESCRIPTION "Lists all projects contained in a GCP folder or its subfolders" \
__USAGE "gcp-list-projects-in-folder.sh folder_id
folder_id: The id of the root folder that should be searched" "$@"
set_trap 1 2
folder_id="${NAMED_ARGS["folder-id"]}"
shift
items=("folders/$folder_id")
debug=false
i=0
echo "Searching folders recursively..." >&2
while [[ $(( "$i" < "${#items[@]}" )) -eq 1 ]];
do
echo "Searching: ${items[i]}" >&2
if [[ "${items[i]}" =~ "folders/".* ]]
then
$debug && echo "current (name): $(gcloud resource-manager folders describe "${items[i]/#folders\//}" --format="get(displayName)")" >&2
$debug && echo "items+=(\$(gcloud resource-manager folders list --folder \"${items[i]/#folders\//}\" --format='get(NAME)' \"$@\" 2>/dev/null | xargs))" >&2
items+=($(gcloud resource-manager folders list --folder "${items[i]/#folders\//}" --format='get(NAME)' "$@" 2>/dev/null | xargs))
fi
i="$((i+1))"
done
echo "" >&2
echo "Projects:" >&2
echo "=========" >&2
for folder in "${items[@]}"
do
gcloud projects list --filter="parent.id=${folder/#folders\//}" --format="get(PROJECT_ID)"
done