Skip to content

Commit 2a14ded

Browse files
juliocckarpok78
authored andcommitted
Support project creation in different universes (GoogleCloudPlatform#2848)
* Support project creation in different universes * Fix typo * Revert prefix validation * Add test * Call new test * Do not override project name
1 parent 13d9b57 commit 2a14ded

File tree

7 files changed

+46
-6
lines changed

7 files changed

+46
-6
lines changed

modules/project/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,8 @@ alerts:
16211621
| [skip_delete](variables.tf#L240) | Deprecated. Use deletion_policy. | <code>bool</code> | | <code>null</code> |
16221622
| [tag_bindings](variables-tags.tf#L81) | Tag bindings for this project, in key => tag value id format. | <code>map&#40;string&#41;</code> | | <code>null</code> |
16231623
| [tags](variables-tags.tf#L88) | Tags by key name. If `id` is provided, key or value creation is skipped. The `iam` attribute behaves like the similarly named one at module level. | <code title="map&#40;object&#40;&#123;&#10; description &#61; optional&#40;string, &#34;Managed by the Terraform project module.&#34;&#41;&#10; iam &#61; optional&#40;map&#40;list&#40;string&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings &#61; optional&#40;map&#40;object&#40;&#123;&#10; members &#61; list&#40;string&#41;&#10; role &#61; string&#10; condition &#61; optional&#40;object&#40;&#123;&#10; expression &#61; string&#10; title &#61; string&#10; description &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings_additive &#61; optional&#40;map&#40;object&#40;&#123;&#10; member &#61; string&#10; role &#61; string&#10; condition &#61; optional&#40;object&#40;&#123;&#10; expression &#61; string&#10; title &#61; string&#10; description &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; id &#61; optional&#40;string&#41;&#10; values &#61; optional&#40;map&#40;object&#40;&#123;&#10; description &#61; optional&#40;string, &#34;Managed by the Terraform project module.&#34;&#41;&#10; iam &#61; optional&#40;map&#40;list&#40;string&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings &#61; optional&#40;map&#40;object&#40;&#123;&#10; members &#61; list&#40;string&#41;&#10; role &#61; string&#10; condition &#61; optional&#40;object&#40;&#123;&#10; expression &#61; string&#10; title &#61; string&#10; description &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings_additive &#61; optional&#40;map&#40;object&#40;&#123;&#10; member &#61; string&#10; role &#61; string&#10; condition &#61; optional&#40;object&#40;&#123;&#10; expression &#61; string&#10; title &#61; string&#10; description &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; id &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
1624-
| [vpc_sc](variables.tf#L252) | VPC-SC configuration for the project, use when `ignore_changes` for resources is set in the VPC-SC module. | <code title="object&#40;&#123;&#10; perimeter_name &#61; string&#10; perimeter_bridges &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; is_dry_run &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
1624+
| [universe](variables.tf#L252) | GCP universe where deploy the project. This will be prepended to the project id. | <code>string</code> | | <code>&#34;&#34;</code> |
1625+
| [vpc_sc](variables.tf#L259) | VPC-SC configuration for the project, use when `ignore_changes` for resources is set in the VPC-SC module. | <code title="object&#40;&#123;&#10; perimeter_name &#61; string&#10; perimeter_bridges &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10; is_dry_run &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
16251626

16261627
## Outputs
16271628

modules/project/main.tf

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
locals {
18+
# descriptive_name cannot contain colons, so we omit the universe from the default
1819
descriptive_name = (
1920
var.descriptive_name != null ? var.descriptive_name : "${local.prefix}${var.name}"
2021
)
@@ -25,6 +26,7 @@ locals {
2526
parent_type = var.parent == null ? null : split("/", var.parent)[0]
2627
parent_id = var.parent == null ? null : split("/", var.parent)[1]
2728
prefix = var.prefix == null ? "" : "${var.prefix}-"
29+
project_id = "${local.universe}${local.prefix}${var.name}"
2830
project = (
2931
var.project_create ?
3032
{
@@ -33,23 +35,24 @@ locals {
3335
name = try(google_project.project[0].name, null)
3436
}
3537
: {
36-
project_id = "${local.prefix}${var.name}"
38+
project_id = local.project_id
3739
number = try(data.google_project.project[0].number, null)
3840
name = try(data.google_project.project[0].name, null)
3941
}
4042
)
43+
universe = var.universe == "" ? "" : "${var.universe}:"
4144
}
4245

4346
data "google_project" "project" {
4447
count = var.project_create ? 0 : 1
45-
project_id = "${local.prefix}${var.name}"
48+
project_id = local.project_id
4649
}
4750

4851
resource "google_project" "project" {
4952
count = var.project_create ? 1 : 0
5053
org_id = local.parent_type == "organizations" ? local.parent_id : null
5154
folder_id = local.parent_type == "folders" ? local.parent_id : null
52-
project_id = "${local.prefix}${var.name}"
55+
project_id = local.project_id
5356
name = local.descriptive_name
5457
billing_account = var.billing_account
5558
auto_create_network = var.auto_create_network

modules/project/outputs.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ output "custom_role_id" {
2828
for k, v in google_project_iam_custom_role.roles :
2929
# build the string manually so that role IDs can be used as map
3030
# keys (useful for folder/organization/project-level iam bindings)
31-
(k) => "projects/${local.prefix}${var.name}/roles/${local.custom_roles[k].name}"
31+
(k) => "projects/${local.project_id}/roles/${local.custom_roles[k].name}"
3232
}
3333
}
3434

@@ -47,7 +47,7 @@ output "default_service_accounts" {
4747

4848
output "id" {
4949
description = "Project id."
50-
value = "${local.prefix}${var.name}"
50+
value = local.project_id
5151
depends_on = [
5252
google_project.project,
5353
data.google_project.project,

modules/project/variables.tf

+7
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ variable "skip_delete" {
249249
# }
250250
}
251251

252+
variable "universe" {
253+
description = "GCP universe where deploy the project. This will be prepended to the project id."
254+
type = string
255+
default = ""
256+
nullable = false
257+
}
258+
252259
variable "vpc_sc" {
253260
description = "VPC-SC configuration for the project, use when `ignore_changes` for resources is set in the VPC-SC module."
254261
type = object({

tests/modules/project/tftest.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ tests:
2727
org_policies_list:
2828
org_policies_boolean:
2929
iam_by_principals_additive:
30+
universe:

tests/modules/project/universe.tfvars

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
prefix = "foo"
2+
universe = "alpha"

tests/modules/project/universe.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
values:
16+
google_project.project[0]:
17+
name: foo-my-project
18+
project_id: alpha:foo-my-project
19+
20+
counts:
21+
google_project: 1
22+
23+
outputs:
24+
id: alpha:foo-my-project
25+
name: foo-my-project
26+
project_id: foo-my-project

0 commit comments

Comments
 (0)