Skip to content

Commit ae6b3ee

Browse files
Ali-Aburubludoo
andauthored
Conditionally creates billing sink if the billing account is of type resource (#3130)
* updated billing.tf file to conditionally creates billing sink if the billing account resides out of org * replace resources with modules * replace individual iam resources with billing_iam local passed in the module * update module name and path and move iam from local to module call * update README.md * Add log_bucket option to billing_account variable, and update billing account IAM assignments * update 0-bootstrap README to reflect changes to billing account module * Update current bootstrap tests to reflect the change to billing_account variable * Create test for the case when billing account log bucket is created * running fmt --------- Co-authored-by: Ludovico Magnocavallo <[email protected]>
1 parent eae9518 commit ae6b3ee

File tree

8 files changed

+2279
-51
lines changed

8 files changed

+2279
-51
lines changed

fast/stages/0-bootstrap/README.md

Lines changed: 23 additions & 23 deletions
Large diffs are not rendered by default.

fast/stages/0-bootstrap/billing.tf

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,41 @@
1717
# tfdoc:file:description Billing export project and dataset.
1818

1919
locals {
20-
# used here for convenience, in organization.tf members are explicit
21-
billing_ext_admins = [
22-
local.principals.gcp-billing-admins,
23-
local.principals.gcp-organization-admins,
24-
module.automation-tf-bootstrap-sa.iam_email,
25-
module.automation-tf-resman-sa.iam_email
26-
]
27-
billing_ext_viewers = [
28-
module.automation-tf-bootstrap-r-sa.iam_email,
29-
module.automation-tf-resman-r-sa.iam_email
30-
]
3120
billing_mode = (
3221
var.billing_account.no_iam
3322
? null
3423
: var.billing_account.is_org_level ? "org" : "resource"
3524
)
25+
26+
_billing_iam_bindings = {
27+
"roles/billing.admin" = [
28+
local.principals.gcp-billing-admins,
29+
local.principals.gcp-organization-admins,
30+
module.automation-tf-bootstrap-sa.iam_email,
31+
module.automation-tf-resman-sa.iam_email
32+
],
33+
"roles/billing.viewer" = [
34+
module.automation-tf-bootstrap-r-sa.iam_email,
35+
module.automation-tf-resman-r-sa.iam_email
36+
],
37+
"roles/logging.configWriter" = local.billing_mode == "org" || !var.billing_account.force_create.log_bucket ? [] : [
38+
module.automation-tf-bootstrap-sa.iam_email
39+
]
40+
}
41+
42+
_billing_iam_bindings_add = flatten([for role, bindings in local._billing_iam_bindings : [
43+
for member in bindings : {
44+
member = member,
45+
role = role
46+
}
47+
]])
48+
49+
billing_iam_bindings_additive = {
50+
for b in local._billing_iam_bindings_add : "${b.role}-${b.member}" => {
51+
member = b.member
52+
role = b.role
53+
}
54+
}
3655
}
3756

3857
# billing account in same org (IAM is in the organization.tf file)
@@ -81,20 +100,28 @@ module "billing-export-dataset" {
81100

82101
# standalone billing account
83102

84-
resource "google_billing_account_iam_member" "billing_ext_admin" {
85-
for_each = toset(
86-
local.billing_mode == "resource" ? local.billing_ext_admins : []
87-
)
88-
billing_account_id = var.billing_account.id
89-
role = "roles/billing.admin"
90-
member = each.key
103+
module "billing-account-logbucket" {
104+
source = "../../../modules/logging-bucket"
105+
count = local.billing_mode == "resource" && var.billing_account.force_create.log_bucket ? 1 : 0
106+
parent_type = "project"
107+
parent = module.log-export-project.project_id
108+
id = "billing-account"
109+
location = local.locations.logging
110+
log_analytics = { enable = true }
111+
# org-level logging settings ready before we create any logging buckets
112+
depends_on = [module.organization-logging]
91113
}
92114

93-
resource "google_billing_account_iam_member" "billing_ext_viewer" {
94-
for_each = toset(
95-
local.billing_mode == "resource" ? local.billing_ext_viewers : []
96-
)
97-
billing_account_id = var.billing_account.id
98-
role = "roles/billing.viewer"
99-
member = each.key
100-
}
115+
module "billing-account" {
116+
source = "../../../modules/billing-account"
117+
count = local.billing_mode == "resource" ? 1 : 0
118+
id = var.billing_account.id
119+
iam_bindings_additive = local.billing_iam_bindings_additive
120+
logging_sinks = !var.billing_account.force_create.log_bucket ? {} : {
121+
billing_bucket_log_sink = {
122+
destination = module.billing-account-logbucket[0].id
123+
type = "logging"
124+
description = "billing-account sink (Terraform-managed)."
125+
}
126+
}
127+
}

fast/stages/0-bootstrap/variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ variable "billing_account" {
1919
type = object({
2020
id = string
2121
force_create = optional(object({
22-
dataset = optional(bool, false)
23-
project = optional(bool, false)
22+
dataset = optional(bool, false)
23+
project = optional(bool, false)
24+
log_bucket = optional(bool, false)
2425
}), {})
2526
is_org_level = optional(bool, true)
2627
no_iam = optional(bool, false)

tests/fast/stages/s0_bootstrap/cicd.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,7 @@ outputs:
24932493
force_create:
24942494
dataset: false
24952495
project: false
2496+
log_bucket: false
24962497
id: 000000-111111-222222
24972498
is_org_level: true
24982499
no_iam: false
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
billing_account = {
2+
id = "000000-111111-222222"
3+
is_org_level = false
4+
force_create = {
5+
dataset = true
6+
project = true
7+
log_bucket = true
8+
}
9+
}
10+
essential_contacts = "[email protected]"
11+
groups = {
12+
gcp-support = "group:[email protected]"
13+
}
14+
org_policies_config = {
15+
import_defaults = false
16+
}
17+
organization = {
18+
domain = "fast.example.com"
19+
id = 123456789012
20+
customer_id = "C00000000"
21+
}
22+
outputs_location = "/fast-config"
23+
prefix = "fast"

0 commit comments

Comments
 (0)