Skip to content

Commit c497707

Browse files
juliocckarpok78
authored andcommitted
Allow universe-bound projects to exclude services (GoogleCloudPlatform#2852)
* Allow universe-bound projects to exclude services * Update README
1 parent 766fcab commit c497707

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

modules/project/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1621,8 +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-
| [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> |
1624+
| [universe](variables.tf#L252) | GCP universe where to deploy the project. The prefix will be prepended to the project id. | <code title="object&#40;&#123;&#10; prefix &#61; string&#10; unavailable_services &#61; optional&#40;list&#40;string&#41;, &#91;&#93;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
1625+
| [vpc_sc](variables.tf#L261) | 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> |
16261626

16271627
## Outputs
16281628

@@ -1643,7 +1643,7 @@ alerts:
16431643
| [quota_configs](outputs.tf#L144) | Quota configurations. | |
16441644
| [quotas](outputs.tf#L155) | Quota resources. | |
16451645
| [service_agents](outputs.tf#L160) | List of all (active) service agents for this project. | |
1646-
| [services](outputs.tf#L169) | Service APIs to enabled in the project. | |
1646+
| [services](outputs.tf#L169) | Service APIs to enable in the project. | |
16471647
| [sink_writer_identities](outputs.tf#L178) | Writer identities created for each sink. | |
16481648
| [tag_keys](outputs.tf#L185) | Tag key resources. | |
16491649
| [tag_values](outputs.tf#L194) | Tag value resources. | |

modules/project/main.tf

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ locals {
2626
parent_type = var.parent == null ? null : split("/", var.parent)[0]
2727
parent_id = var.parent == null ? null : split("/", var.parent)[1]
2828
prefix = var.prefix == null ? "" : "${var.prefix}-"
29-
project_id = "${local.universe}${local.prefix}${var.name}"
29+
project_id = "${local.universe_prefix}${local.prefix}${var.name}"
3030
project = (
3131
var.project_create ?
3232
{
@@ -40,7 +40,8 @@ locals {
4040
name = try(data.google_project.project[0].name, null)
4141
}
4242
)
43-
universe = var.universe == "" ? "" : "${var.universe}:"
43+
universe_prefix = var.universe == null ? "" : "${var.universe.prefix}:"
44+
available_services = tolist(setsubtract(var.services, try(var.universe.unavailable_services, [])))
4445
}
4546

4647
data "google_project" "project" {
@@ -68,7 +69,7 @@ resource "google_project" "project" {
6869
}
6970

7071
resource "google_project_service" "project_services" {
71-
for_each = toset(var.services)
72+
for_each = toset(local.available_services)
7273
project = local.project.project_id
7374
service = each.value
7475
disable_on_destroy = var.service_config.disable_on_destroy
@@ -78,7 +79,7 @@ resource "google_project_service" "project_services" {
7879

7980
resource "google_compute_project_metadata_item" "default" {
8081
for_each = (
81-
contains(var.services, "compute.googleapis.com") ? var.compute_metadata : {}
82+
contains(local.available_services, "compute.googleapis.com") ? var.compute_metadata : {}
8283
)
8384
project = local.project.project_id
8485
key = each.key

modules/project/outputs.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ output "service_agents" {
167167
}
168168

169169
output "services" {
170-
description = "Service APIs to enabled in the project."
171-
value = var.services
170+
description = "Service APIs to enable in the project."
171+
value = local.available_services
172172
depends_on = [
173173
google_project_service.project_services,
174174
google_project_service_identity.default,

modules/project/service-agents.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Google LLC
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
1818

1919
locals {
2020
services = distinct(concat(
21-
var.services, var.service_agents_config.services_enabled
21+
local.available_services, var.service_agents_config.services_enabled
2222
))
2323
_service_agents_data = yamldecode(file("${path.module}/service-agents.yaml"))
2424
# map of api => list of agents
@@ -33,7 +33,7 @@ locals {
3333
(agent.name) => merge(agent, {
3434
email = format(agent.identity, local.project.number)
3535
iam_email = "serviceAccount:${format(agent.identity, local.project.number)}"
36-
create_jit = api == "cloudservices" || contains(var.services, api)
36+
create_jit = api == "cloudservices" || contains(local.available_services, api)
3737
})
3838
}
3939
]...)

modules/project/variables.tf

+6-4
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ variable "skip_delete" {
250250
}
251251

252252
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
253+
description = "GCP universe where to deploy the project. The prefix will be prepended to the project id."
254+
type = object({
255+
prefix = string
256+
unavailable_services = optional(list(string), [])
257+
})
258+
default = null
257259
}
258260

259261
variable "vpc_sc" {

tests/modules/project/universe.tfvars

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
prefix = "foo"
2-
universe = "alpha"
1+
prefix = "foo"
2+
universe = {
3+
prefix = "alpha"
4+
unavailable_services = [
5+
"xxx.googleapis.com",
6+
"yyy.googleapis.com"
7+
]
8+
}
9+
services = [
10+
"aaa.googleapis.com",
11+
"bbb.googleapis.com",
12+
"xxx.googleapis.com"
13+
]

tests/modules/project/universe.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@ values:
1616
google_project.project[0]:
1717
name: foo-my-project
1818
project_id: alpha:foo-my-project
19+
google_project_service.project_services["aaa.googleapis.com"]:
20+
project: alpha:foo-my-project
21+
service: aaa.googleapis.com
22+
google_project_service.project_services["bbb.googleapis.com"]:
23+
project: alpha:foo-my-project
24+
service: bbb.googleapis.com
1925

2026
counts:
2127
google_project: 1
28+
google_project_service: 2
29+
resources: 3
2230

2331
outputs:
2432
id: alpha:foo-my-project
2533
name: foo-my-project
2634
project_id: foo-my-project
35+
services:
36+
- aaa.googleapis.com
37+
- bbb.googleapis.com

0 commit comments

Comments
 (0)