Skip to content

Commit 60e1bbc

Browse files
fix: modular gcp api enablement for management account only on Pub Sub
1 parent a0420cf commit 60e1bbc

File tree

1 file changed

+9
-110
lines changed
  • test/examples/organization_api_enablement/cdr_ciem

1 file changed

+9
-110
lines changed

test/examples/organization_api_enablement/cdr_ciem/main.tf

Lines changed: 9 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,134 +7,33 @@ The APIs needed for the CDR/CIEM feature are listed below:
77
* Note: This do not overwrite any other APIs config that your GCP project has, it will only enabled it if isn't yet.
88
*/
99

10-
# Set local variables for Organization ID and API services to enable
10+
# Set local local variables for Project ID and API services to enable
1111
locals {
12+
project = "org-child-project-1"
1213
services = [
1314
"pubsub.googleapis.com"
1415
]
15-
root_projects = [for project in data.google_projects.organization_projects.projects : project.project_id]
16-
folder_projects = jsondecode(data.local_file.projects_from_folder.content)
17-
all_projects = concat(local.root_projects, local.folder_projects)
18-
project_and_services = flatten([
19-
for project in local.all_projects : [
20-
for service in local.services : {
21-
project = project
22-
service = service
23-
}
24-
]
25-
])
2616
}
2717

2818
# GCP provider
2919
provider "google" {
20+
project = local.project
3021
region = "us-west-1"
3122
}
3223

33-
# Get list of projects under the specified organization
34-
data "google_projects" "organization_projects" {
35-
filter = "parent.type:organization parent.id:${data.google_organization.org.org_id}"
36-
}
37-
38-
data "google_organization" "org" {
39-
domain = "draios.com"
40-
}
41-
42-
data "local_file" "projects_from_folder" {
43-
filename = "project_ids.json"
44-
depends_on = [null_resource.get_projects_from_folders]
45-
}
46-
47-
# Enable API services for GCP project
24+
// Enable API services for GCP project
4825
resource "google_project_service" "enable_cdr_ciem_apis" {
49-
// create a unique key per project and service to enable each API
50-
for_each = { for item in local.project_and_services : "${item.project}-${item.service}" => item }
26+
project = local.project
5127

52-
project = each.value.project
53-
service = each.value.service
28+
for_each = toset(local.services)
29+
service = each.value
5430
disable_on_destroy = false
5531
}
5632

5733
# Output the projects and APIs enabled
5834
output "enabled_projects" {
59-
value = distinct([for resource in google_project_service.enable_cdr_ciem_apis : resource.project])
35+
value = distinct([for service in local.services : google_project_service.enable_cdr_ciem_apis[service].project])
6036
}
61-
6237
output "enabled_services" {
63-
value = distinct([for service in google_project_service.enable_cdr_ciem_apis : service.service])
64-
}
65-
66-
# Script to get projects from folders recursively and set to a file
67-
resource "null_resource" "get_projects_from_folders" {
68-
provisioner "local-exec" {
69-
command = <<EOF
70-
#!/bin/bash
71-
ORG_DOMAIN="draios.com"
72-
73-
# array to store project IDs
74-
declare -a FINAL_PROJECT_IDS
75-
76-
list_projects() {
77-
local folder_id=$1
78-
79-
# get projects from folder
80-
local projects_json=$(gcloud projects list --filter="parent.id=$folder_id AND parent.type=folder" --format=json)
81-
82-
# check valid array
83-
if ! echo "$projects_json" | jq empty >/dev/null 2>&1; then
84-
echo "Invalid JSON returned for projects list."
85-
return
86-
fi
87-
88-
# get project ids
89-
local project_ids=$(echo "$projects_json" | jq -r '.[] | .projectId')
90-
91-
# check project ids not empty and add to global variable
92-
if [ -n "$project_ids" ]; then
93-
for project_id in $project_ids; do
94-
FINAL_PROJECT_IDS+=("$project_id")
95-
done
96-
else
97-
echo "No projects found in folder $folder_id"
98-
fi
99-
}
100-
101-
list_folders_recursive() {
102-
local parent_id=$1
103-
local parent_type=$2
104-
105-
# list folders on org or other folders
106-
if [[ "$parent_type" == "organization" ]]; then
107-
folders=$(gcloud resource-manager folders list --organization=$parent_id --format=json)
108-
elif [[ "$parent_type" == "folder" ]]; then
109-
folders=$(gcloud resource-manager folders list --folder=$parent_id --format=json)
110-
fi
111-
112-
# check if there were folders returned
113-
if [ "$(echo "$folders" | jq length)" -eq 0 ]; then
114-
return
115-
fi
116-
117-
# iterate over folder and call functions recursively
118-
for folder in $(echo "$folders" | jq -c '.[]'); do
119-
folder_id=$(echo "$folder" | jq -r '.name' | awk -F'/' '{print $NF}')
120-
121-
list_projects "$folder_id"
122-
list_folders_recursive "$folder_id" "folder"
123-
done
124-
}
125-
126-
# start organization scraping
127-
ORG_JSON=$(gcloud organizations list --filter="displayName:$ORG_DOMAIN" --format=json)
128-
ORG_ID=$(echo "$ORG_JSON" | jq -r '.[0].name' | sed 's/organizations\///')
129-
if [ -z "$ORG_ID" ]; then
130-
echo "Organization with display name '$DISPLAY_NAME' not found."
131-
exit 1
132-
fi
133-
134-
echo "Listing all projects in folders for organization: $ORG_DOMAIN"
135-
list_folders_recursive "$ORG_ID" "organization"
136-
printf "%s\n" "$${FINAL_PROJECT_IDS[@]}" | jq -R . | jq -s . > "project_ids.json"
137-
echo "Projects listed and saved to local file."
138-
EOF
139-
}
38+
value = [for service in local.services : google_project_service.enable_cdr_ciem_apis[service].service]
14039
}

0 commit comments

Comments
 (0)