Skip to content

Commit 458ff51

Browse files
enhance(modular): use org domain instead of org ID for gcp api enablement
1 parent dd35f11 commit 458ff51

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

test/examples/organization_api_enablement/cdr_ciem/main.tf

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ The APIs needed for the CDR/CIEM feature are listed below:
99

1010
# Set local variables for Organization ID and API services to enable
1111
locals {
12-
organizationID = "933620940614"
1312
services = [
1413
"pubsub.googleapis.com"
1514
]
@@ -33,7 +32,11 @@ provider "google" {
3332

3433
# Get list of projects under the specified organization
3534
data "google_projects" "organization_projects" {
36-
filter = "parent.type:organization parent.id:${local.organizationID}"
35+
filter = "parent.type:organization parent.id:${data.google_organization.org.org_id}"
36+
}
37+
38+
data "google_organization" "org" {
39+
domain = "draios.com"
3740
}
3841

3942
data "local_file" "projects_from_folder" {
@@ -65,7 +68,7 @@ resource "null_resource" "get_projects_from_folders" {
6568
provisioner "local-exec" {
6669
command = <<EOF
6770
#!/bin/bash
68-
ORG_ID="933620940614"
71+
ORG_DOMAIN="draios.com"
6972
7073
# array to store project IDs
7174
declare -a FINAL_PROJECT_IDS
@@ -120,7 +123,15 @@ resource "null_resource" "get_projects_from_folders" {
120123
done
121124
}
122125
123-
echo "Listing all projects in folders for organization: $ORG_ID"
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"
124135
list_folders_recursive "$ORG_ID" "organization"
125136
printf "%s\n" "$${FINAL_PROJECT_IDS[@]}" | jq -R . | jq -s . > "project_ids.json"
126137
echo "Projects listed and saved to local file."

test/examples/organization_api_enablement/cspm/main.tf

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ In addition, since CSPM is needed for onboard any GCP project these other APIs a
1616

1717
# Set local variables for Organization ID and API services to enable
1818
locals {
19-
organizationID = "933620940614"
2019
services = [
2120
# CSPM specific APIs
2221
"sts.googleapis.com",
@@ -49,7 +48,11 @@ provider "google" {
4948

5049
# Get list of projects under the specified organization
5150
data "google_projects" "organization_projects" {
52-
filter = "parent.type:organization parent.id:${local.organizationID}"
51+
filter = "parent.type:organization parent.id:${data.google_organization.org.org_id}"
52+
}
53+
54+
data "google_organization" "org" {
55+
domain = "draios.com"
5356
}
5457

5558
data "local_file" "projects_from_folder" {
@@ -81,7 +84,7 @@ resource "null_resource" "get_projects_from_folders" {
8184
provisioner "local-exec" {
8285
command = <<EOF
8386
#!/bin/bash
84-
ORG_ID="933620940614"
87+
ORG_DOMAIN="draios.com"
8588
8689
# array to store project IDs
8790
declare -a FINAL_PROJECT_IDS
@@ -136,7 +139,15 @@ resource "null_resource" "get_projects_from_folders" {
136139
done
137140
}
138141
139-
echo "Listing all projects in folders for organization: $ORG_ID"
142+
# start organization scraping
143+
ORG_JSON=$(gcloud organizations list --filter="displayName:$ORG_DOMAIN" --format=json)
144+
ORG_ID=$(echo "$ORG_JSON" | jq -r '.[0].name' | sed 's/organizations\///')
145+
if [ -z "$ORG_ID" ]; then
146+
echo "Organization with display name '$DISPLAY_NAME' not found."
147+
exit 1
148+
fi
149+
150+
echo "Listing all projects in folders for organization: $ORG_DOMAIN"
140151
list_folders_recursive "$ORG_ID" "organization"
141152
printf "%s\n" "$${FINAL_PROJECT_IDS[@]}" | jq -R . | jq -s . > "project_ids.json"
142153
echo "Projects listed and saved to local file."

test/examples/organization_api_enablement/vm/main.tf

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ The APIs needed for the VM feature are listed below:
99

1010
# Set local variables for Organization ID and API services to enable
1111
locals {
12-
organizationID = "933620940614"
1312
services = [
1413
"compute.googleapis.com"
1514
]
@@ -33,7 +32,11 @@ provider "google" {
3332

3433
# Get list of projects under the specified organization
3534
data "google_projects" "organization_projects" {
36-
filter = "parent.type:organization parent.id:${local.organizationID}"
35+
filter = "parent.type:organization parent.id:${data.google_organization.org.org_id}"
36+
}
37+
38+
data "google_organization" "org" {
39+
domain = "draios.com"
3740
}
3841

3942
data "local_file" "projects_from_folder" {
@@ -66,7 +69,7 @@ resource "null_resource" "get_projects_from_folders" {
6669
provisioner "local-exec" {
6770
command = <<EOF
6871
#!/bin/bash
69-
ORG_ID="933620940614"
72+
ORG_DOMAIN="draios.com"
7073
7174
# array to store project IDs
7275
declare -a FINAL_PROJECT_IDS
@@ -121,6 +124,14 @@ resource "null_resource" "get_projects_from_folders" {
121124
done
122125
}
123126
127+
# start organization scraping
128+
ORG_JSON=$(gcloud organizations list --filter="displayName:$ORG_DOMAIN" --format=json)
129+
ORG_ID=$(echo "$ORG_JSON" | jq -r '.[0].name' | sed 's/organizations\///')
130+
if [ -z "$ORG_ID" ]; then
131+
echo "Organization with display name '$DISPLAY_NAME' not found."
132+
exit 1
133+
fi
134+
124135
echo "Listing all projects in folders for organization: $ORG_ID"
125136
list_folders_recursive "$ORG_ID" "organization"
126137
printf "%s\n" "$${FINAL_PROJECT_IDS[@]}" | jq -R . | jq -s . > "project_ids.json"

0 commit comments

Comments
 (0)