-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
266 lines (188 loc) · 7.2 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
terraform {
# Required Providers
# https://www.terraform.io/language/providers/requirements#requiring-providers
required_providers {
# Google Cloud Platform Provider
# https://registry.terraform.io/providers/hashicorp/google/latest/docs
google = {
source = "hashicorp/google"
}
}
}
# Google Cloud Provider
# https://registry.terraform.io/providers/hashicorp/google/latest/docs
# The google_cloud_identity_group resource requires this if you are using User ADCs (Application Default Credentials).
# Your account must have the serviceusage.services.use permission on the billing_project you defined as well.
# The following APIs must be enabled on the billing_project:
# - cloudresourcemanager.googleapis.com
# - cloudidentity.googleapis.com
# This is only needed during bootstrapping.
# provider "google" {
# billing_project = var.billing_project
# user_project_override = true
# }
# IAM Policy Data Source
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/iam_policy
data "google_iam_policy" "this" {
for_each = var.folder_iam_policies
dynamic "binding" {
for_each = each.value.bindings
content {
members = binding.value.members
role = binding.value.role
}
}
}
# Billing Budget Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/billing_budget
resource "google_billing_budget" "organization" {
amount {
specified_amount {
currency_code = "USD"
units = var.organization_monthly_budget_amount
}
}
billing_account = var.billing_account
display_name = "Organization Monthly"
threshold_rules {
threshold_percent = 0.50
spend_basis = "CURRENT_SPEND"
}
threshold_rules {
threshold_percent = 0.75
spend_basis = "CURRENT_SPEND"
}
threshold_rules {
threshold_percent = 1.0
spend_basis = "CURRENT_SPEND"
}
}
resource "google_billing_budget" "services" {
for_each = var.folder_services
amount {
specified_amount {
currency_code = "USD"
units = each.value.monthly_budget_amount
}
}
billing_account = var.billing_account
budget_filter {
resource_ancestors = [google_folder.service[each.key].id]
}
display_name = "${each.value.display_name} Monthly"
threshold_rules {
threshold_percent = 0.50
spend_basis = "CURRENT_SPEND"
}
threshold_rules {
threshold_percent = 0.75
spend_basis = "CURRENT_SPEND"
}
threshold_rules {
threshold_percent = 1.0
spend_basis = "CURRENT_SPEND"
}
}
# Cloud Identity Group Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_identity_group
resource "google_cloud_identity_group" "this" {
for_each = var.identity_groups
description = each.value.description
display_name = each.value.display_name
initial_group_config = "EMPTY"
# When you signed up for Google Workspace or Cloud Identity, your account is assigned a unique customer ID.
# You can look up this ID in your Admin console.
# https://admin.google.com/ac/accountsettings/profile
parent = "customers/${var.customer_id}"
group_key {
id = "${each.key}@${var.primary_domain}"
}
labels = {
# Must not contain more than one entry and must contain the following entry:
"cloudidentity.googleapis.com/groups.discussion_forum" = ""
}
}
# Identity Group Membership
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_identity_group_membership
resource "google_cloud_identity_group_membership" "managers" {
# Iterate over local.managers to create a resource for each user in the manager list.
for_each = local.managers
group = google_cloud_identity_group.this[each.value.group].id
preferred_member_key {
id = each.value.manager
}
# MEMBER role must be specified. The order of roles should not be changed.
roles { name = "MEMBER" }
roles { name = "MANAGER" }
}
resource "google_cloud_identity_group_membership" "members" {
# Iterate over local.members to create a resource for each user in the member list.
for_each = local.members
group = google_cloud_identity_group.this[each.value.group].id
preferred_member_key {
id = each.value.member
}
roles { name = "MEMBER" }
}
resource "google_cloud_identity_group_membership" "owners" {
# Iterate over local.owners to create a resource for each user in the owner list.
for_each = local.owners
group = google_cloud_identity_group.this[each.value.group].id
preferred_member_key {
id = each.value.owner
}
# MEMBER role must be specified. The order of roles should not be changed.
roles { name = "OWNER" }
roles { name = "MEMBER" }
}
# Folder Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_folder
# Folder resources optionally provide an additional grouping mechanism and isolation
# boundaries between projects. They can be seen as sub-organizations within the organization
# resource. Folder resources can be used to model different legal entities, departments, and
# teams within a company. For example, a first level of folder resources could be used to
# represent the main departments in your organization.
# https://cloud.google.com/resource-manager/docs/cloud-platform-resource-hierarchy#folders
resource "google_folder" "team" {
for_each = var.folder_teams
display_name = each.value.display_name
parent = "organizations/${var.organization_id}"
}
resource "google_folder" "service" {
for_each = var.folder_services
display_name = each.value.display_name
parent = google_folder.team[each.value.parent].name
}
resource "google_folder" "environment" {
for_each = local.environments
display_name = each.value.environment
parent = google_folder.service[each.value.service].name
}
# Folder IAM Policy Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_folder_iam#google_folder_iam_policy
resource "google_folder_iam_policy" "this" {
for_each = var.folder_iam_policies
folder = each.key
policy_data = data.google_iam_policy.this[each.key].policy_data
}
# Organization IAM Custom Role Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_organization_iam_custom_role
resource "google_organization_iam_custom_role" "this" {
for_each = var.organization_custom_iam_roles
org_id = var.organization_id
role_id = each.value.role_id
title = each.value.title
description = each.value.description
permissions = each.value.permissions
}
# Organization IAM Member Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_organization_iam#google_organization_iam_member
# Predefined roles provide granular access for a specific service and are managed by Google Cloud.
# https://cloud.google.com/iam/docs/understanding-roles#predefined
# In this step, you grant to group by assigning roles at the organization level.
resource "google_organization_iam_member" "this" {
for_each = local.roles
member = "group:${google_cloud_identity_group.this[each.value.group].group_key[0].id}"
org_id = var.organization_id
role = each.value.role
}