Skip to content

Commit 670ad80

Browse files
authored
chore: adds a script to fetch all gcp projects (#146)
1 parent f274d18 commit 670ad80

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,14 @@ output "me" {
152152
```
153153
154154
### Q: In organizaitonal setup, Compliance trust-relationship is not being deployed on our projects
155-
A: If your organizational uses folders we currently don't support that.
156-
<br/>S: A workaround would be to use the `benchmark_project_ids` parameter so you can define the projects where compliance role is to be deployed explicitly. Let us know if this workaround won't be enough and we will work on implementing a solution.
155+
156+
As for 2023 April, organizations with projects under organizational unit folders, is supported with the
157+
[organizational compliance example](./examples/organization-org_compliance)
158+
159+
<br/>S: If you want to target specific projects, you can still use the `benchmark_project_ids` parameter so you can define
160+
the projects where compliance role is to be deployed explicitly.
161+
<br/>You can use the [fetch-gcp-rojects.sh](./resources/fetch-gcp-projects.sh) utility to list organization member projects
162+
<br/>Let us know if this workaround won't be enough, and we will work on implementing a solution.
157163

158164
### Q: Compliance is not working. How can I check everything is properly setup
159165

examples/organization-org_compliance/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ This example deploys Secure for Cloud into a GCP organizational account.
1616
## Prerequisites
1717

1818
1. Configure [Terraform **GCP** Provider](https://registry.terraform.io/providers/hashicorp/google/latest/docs)
19-
2. Following **roles** are required in your GCP organization/project credentials
19+
2. Run the script at `resources/fetch-gcp-projects.sh <organization_ID>`. copy the output and provide it as input in the module
20+
as benchmark_project_ids. e.g benchmark_project_ids = ["id1","id2"]. This script provides list of
21+
all projects under folders and subfolders under an organization. If you don't provide this list
22+
by default only those projects are selected which are directly under the org.
23+
3. To find your organization id please visit https://cloud.google.com/resource-manager/reference/rest/v1/projects/getAncestry
24+
4. Following **roles** are required in your GCP organization/project credentials
2025
* _Owner_
2126
* _Organization Admin_
22-
3. Besides, the following GCP **APIs must be enabled** to deploy resources correctly for:
27+
* _Organization ID_
28+
5. Besides, the following GCP **APIs must be enabled** to deploy resources correctly for:
2329

2430
### Cloud Connector
2531

resources/fetch-gcp-projects.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
#
4+
# usage
5+
# ./fetch-gcp-projects.sh <ORG-ID>
6+
7+
# will return
8+
# ["A", "B", "C"]
9+
#
10+
11+
12+
#
13+
# Function to list projects under a folder recursively
14+
#
15+
list_projects_recursive() {
16+
local folder_id="$1"
17+
18+
# List projects under the current folder
19+
printf " %s" $(gcloud projects list --filter="parent.id=$folder_id" --format="value(projectId)")
20+
21+
# List subfolders and call this function recursively
22+
local subfolders=$(gcloud resource-manager folders list --folder=$folder_id --format="value(name)")
23+
for subfolder in $subfolders; do
24+
list_projects_recursive "$subfolder"
25+
done
26+
}
27+
28+
29+
30+
#
31+
# main flow
32+
#
33+
34+
if [ $# -ne 1 ]; then
35+
echo "script must be launched with the organization id"
36+
echo "usage $0 organizationId"
37+
exit 1
38+
fi
39+
40+
41+
org_id=$1
42+
projectIds=()
43+
projectIds+=$(gcloud projects list --filter="parent.type=organization AND parent.id=$org_id" --format="value(projectId)")
44+
45+
# List top-level folders
46+
folders=$(gcloud resource-manager folders list --organization=$org_id --format="value(name)")
47+
48+
# Iterate through the top-level folders and list projects recursively
49+
for folder in $folders; do
50+
projectIds+=$(list_projects_recursive "$folder")
51+
done
52+
53+
projectList="["
54+
55+
for value in $projectIds; do
56+
projectList="$projectList\"$value\", "
57+
done
58+
59+
# Remove the trailing comma and space
60+
projectList="${projectList%, }]"
61+
62+
echo $projectList

0 commit comments

Comments
 (0)