-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcp_get_proj-ids.sh
executable file
·78 lines (61 loc) · 1.89 KB
/
gcp_get_proj-ids.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
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
#!/usr/bin/env bash
# setting CSV gcloud format for script
FORMAT="csv[no-heading](name,displayName.encode(base64))"
printf "Are you parsing the full organization or just folders?\n"
read -p "Enter ORG or FLD: "
# Function to enumerate Folders recursively
folders() {
LINES=("$@")
for LINE in ${LINES[@]}
do
# Parses lines of the form folder,name
VALUES=(${LINE//,/ })
FOLDER=${VALUES[0]}
# Decodes the encoded name
NAME=$(echo ${VALUES[1]} | base64 --decode)
#printf "Folder: ${FOLDER} (${NAME})\n\n"
#printf "Project info:\n\n"
projects ${FOLDER}
printf "${PROJECT}\n"
folders $(gcloud resource-manager folders list \
--folder=${FOLDER} \
--format="${FORMAT}")
done
}
# Function to get Folder's Projects
projects() {
# Format Project information into table
# FORMAT_PRJ="table[box,title='Folder ${NAME} Project List'] \
# (createTime:sort=1,name,projectNumber,projectId:label=ProjectID,parent.id:label=Parent)"
FORMAT_PRJ="table[box] \
(name,projectNumber,projectId:label=ProjectID)"
FORMAT_PRJ_CSV="csv[no-heading](name,projectNumber,projectId:label=ProjectID)"
PROJECT=$(gcloud projects list \
--filter parent.id:${FOLDER} \
--format="${FORMAT_PRJ_CSV}")
}
# Request CLI input
# Parse depending on input
# ORG gives all folders and projects
# FLD starts at a specific parent folder
if [ ${REPLY} == "ORG" ]; then
# Start at the Org
if [ -z "${ORGANIZATION}" ]; then
read -p "Enter Organization ID: " ORGID
ORGANIZATION=${ORGID}
fi
printf "Organization: ${ORGANIZATION}\n\n"
PARSEOPT="organization=${ORGANIZATION}"
else
read -p "Enter Folder ID #: " folderID
FOLDER=${folderID}
PARSEOPT="folder=${FOLDER}"
projects ${FOLDER}
# Print out projects information
printf "${PROJECT}\n\n"
fi
LINES=$(gcloud resource-manager folders list \
--"${PARSEOPT}" \
--format="${FORMAT}")
# Descend
folders ${LINES[0]}