-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathmain.tf
More file actions
417 lines (363 loc) · 16.3 KB
/
main.tf
File metadata and controls
417 lines (363 loc) · 16.3 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
data "google_project" "project" {
project_id = var.project_id
}
locals {
default_ack_deadline_seconds = 10
pubsub_svc_account_email = "service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}
resource "google_pubsub_schema" "schema" {
count = var.schema != null ? 1 : 0
project = var.project_id
name = var.schema.name
type = var.schema.type
definition = var.schema.definition
}
resource "google_project_iam_member" "bigquery_metadata_viewer_binding" {
count = var.grant_bigquery_project_roles && (length(var.bigquery_subscriptions) != 0) ? 1 : 0
project = var.project_id
role = "roles/bigquery.metadataViewer"
member = "serviceAccount:${local.pubsub_svc_account_email}"
}
resource "google_project_iam_member" "bigquery_data_editor_binding" {
count = var.grant_bigquery_project_roles && (length(var.bigquery_subscriptions) != 0) ? 1 : 0
project = var.project_id
role = "roles/bigquery.dataEditor"
member = "serviceAccount:${local.pubsub_svc_account_email}"
}
resource "google_project_iam_member" "storage_admin_binding" {
count = var.grant_cloud_storage_project_roles && length(var.cloud_storage_subscriptions) != 0 ? 1 : 0
project = var.project_id
role = "roles/storage.admin"
member = "serviceAccount:${local.pubsub_svc_account_email}"
}
resource "google_project_iam_member" "token_creator_binding" {
count = var.grant_token_creator ? 1 : 0
project = var.project_id
role = "roles/iam.serviceAccountTokenCreator"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_subscription.push_subscriptions,
]
}
resource "google_pubsub_topic_iam_member" "push_topic_binding" {
for_each = var.create_topic ? { for i in var.push_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
topic = each.value.dead_letter_topic
role = "roles/pubsub.publisher"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_topic.topic,
]
}
resource "google_pubsub_topic_iam_member" "pull_topic_binding" {
for_each = var.create_topic ? { for i in var.pull_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
topic = each.value.dead_letter_topic
role = "roles/pubsub.publisher"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_topic.topic,
]
}
resource "google_pubsub_topic_iam_member" "bigquery_topic_binding" {
for_each = var.create_topic ? { for i in var.bigquery_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
topic = each.value.dead_letter_topic
role = "roles/pubsub.publisher"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_topic.topic,
]
}
resource "google_pubsub_subscription_iam_member" "pull_subscription_binding" {
for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
subscription = each.value.name
role = "roles/pubsub.subscriber"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_subscription.pull_subscriptions,
]
lifecycle {
replace_triggered_by = [google_pubsub_subscription.pull_subscriptions[each.key]]
}
}
resource "google_pubsub_subscription_iam_member" "push_subscription_binding" {
for_each = var.create_subscriptions ? { for i in var.push_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
subscription = each.value.name
role = "roles/pubsub.subscriber"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_subscription.push_subscriptions,
]
lifecycle {
replace_triggered_by = [google_pubsub_subscription.push_subscriptions[each.key]]
}
}
resource "google_pubsub_subscription_iam_member" "bigquery_subscription_binding" {
for_each = var.create_subscriptions ? { for i in var.bigquery_subscriptions : i.name => i if i.dead_letter_topic != null } : {}
project = var.project_id
subscription = each.value.name
role = "roles/pubsub.subscriber"
member = "serviceAccount:${local.pubsub_svc_account_email}"
depends_on = [
google_pubsub_subscription.bigquery_subscriptions,
]
}
resource "google_pubsub_topic" "topic" {
count = var.create_topic ? 1 : 0
project = var.project_id
name = var.topic
labels = var.topic_labels
kms_key_name = var.topic_kms_key_name
message_retention_duration = var.topic_message_retention_duration
dynamic "message_storage_policy" {
for_each = var.message_storage_policy
content {
allowed_persistence_regions = message_storage_policy.key == "allowed_persistence_regions" ? message_storage_policy.value : null
}
}
dynamic "schema_settings" {
for_each = var.schema != null ? [var.schema] : []
content {
schema = google_pubsub_schema.schema[0].id
encoding = lookup(schema_settings.value, "encoding", null)
}
}
depends_on = [google_pubsub_schema.schema]
}
resource "google_pubsub_subscription" "push_subscriptions" {
for_each = var.create_subscriptions ? { for i in var.push_subscriptions : i.name => i } : {}
name = each.value.name
topic = var.create_topic ? google_pubsub_topic.topic[0].name : var.topic
project = var.project_id
labels = var.subscription_labels
ack_deadline_seconds = each.value.ack_deadline_seconds != null ? each.value.ack_deadline_seconds : local.default_ack_deadline_seconds
message_retention_duration = each.value.message_retention_duration
retain_acked_messages = each.value.retain_acked_messages
filter = each.value.filter
enable_message_ordering = each.value.enable_message_ordering
dynamic "expiration_policy" {
// check if the 'expiration_policy' key exists, if yes, return a list containing it.
for_each = each.value.expiration_policy != null ? [each.value.expiration_policy] : []
content {
ttl = expiration_policy.value
}
}
dynamic "dead_letter_policy" {
for_each = each.value.dead_letter_topic != null ? [each.value.dead_letter_topic] : []
content {
dead_letter_topic = each.value.dead_letter_topic != null ? each.value.dead_letter_topic : ""
max_delivery_attempts = each.value.max_delivery_attempts != null ? each.value.max_delivery_attempts : ""
}
}
dynamic "retry_policy" {
for_each = each.value.maximum_backoff != null ? [each.value.maximum_backoff] : []
content {
maximum_backoff = each.value.maximum_backoff != null ? each.value.maximum_backoff : ""
minimum_backoff = each.value.minimum_backoff != null ? each.value.minimum_backoff : "5"
}
}
push_config {
push_endpoint = each.value["push_endpoint"]
// FIXME: This should be programmable, but nested map isn't supported at this time.
// https://github.com/hashicorp/terraform/issues/2114
attributes = {
x-goog-version = each.value.minimum_backoff != null ? "x-goog-version" : "v1"
}
dynamic "oidc_token" {
for_each = each.value.oidc_service_account_email != null ? [true] : []
content {
service_account_email = each.value.oidc_service_account_email != null ? each.value.oidc_service_account_email : ""
audience = each.value.audience != null ? each.value.audience : ""
}
}
dynamic "no_wrapper" {
for_each = each.value.no_wrapper == true ? [true] : []
content {
write_metadata = each.value.write_metadata != null ? each.value.write_metadata : false
}
}
}
depends_on = [
google_pubsub_topic.topic,
]
}
resource "google_pubsub_subscription" "pull_subscriptions" {
for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i } : {}
name = each.value.name
topic = var.create_topic ? google_pubsub_topic.topic[0].name : var.topic
project = var.project_id
labels = var.subscription_labels
enable_exactly_once_delivery = each.value.enable_exactly_once_delivery
ack_deadline_seconds = each.value.ack_deadline_seconds != null ? each.value.ack_deadline_seconds : local.default_ack_deadline_seconds
message_retention_duration = each.value.message_retention_duration
retain_acked_messages = each.value.retain_acked_messages
filter = each.value.filter
enable_message_ordering = each.value.enable_message_ordering
dynamic "expiration_policy" {
// check if the 'expiration_policy' key exists, if yes, return a list containing it.
for_each = each.value.expiration_policy != null ? [each.value.expiration_policy] : []
content {
ttl = expiration_policy.value
}
}
dynamic "dead_letter_policy" {
for_each = each.value.dead_letter_topic != null ? [each.value.dead_letter_topic] : []
content {
dead_letter_topic = each.value.dead_letter_topic != null ? each.value.dead_letter_topic : ""
max_delivery_attempts = each.value.max_delivery_attempts != null ? each.value.max_delivery_attempts : "5"
}
}
dynamic "retry_policy" {
for_each = each.value.maximum_backoff != null ? [each.value.maximum_backoff] : []
content {
maximum_backoff = each.value.maximum_backoff != null ? each.value.maximum_backoff : ""
minimum_backoff = each.value.minimum_backoff != null ? each.value.minimum_backoff : "5"
}
}
depends_on = [
google_pubsub_topic.topic,
]
}
resource "google_pubsub_subscription" "bigquery_subscriptions" {
for_each = var.create_subscriptions ? { for i in var.bigquery_subscriptions : i.name => i } : {}
name = each.value.name
topic = var.create_topic ? google_pubsub_topic.topic[0].name : var.topic
project = var.project_id
labels = var.subscription_labels
ack_deadline_seconds = each.value.ack_deadline_seconds != null ? each.value.ack_deadline_seconds : local.default_ack_deadline_seconds
message_retention_duration = each.value.message_retention_duration
retain_acked_messages = each.value.retain_acked_messages
filter = each.value.filter
enable_message_ordering = each.value.enable_message_ordering
dynamic "expiration_policy" {
// check if the 'expiration_policy' key exists, if yes, return a list containing it.
for_each = each.value.expiration_policy != null ? [each.value.expiration_policy] : []
content {
ttl = expiration_policy.value
}
}
dynamic "dead_letter_policy" {
for_each = each.value.dead_letter_topic != null ? [each.value.dead_letter_topic] : []
content {
dead_letter_topic = each.value.dead_letter_topic != null ? each.value.dead_letter_topic : ""
max_delivery_attempts = each.value.max_delivery_attempts != null ? each.value.max_delivery_attempts : "5"
}
}
dynamic "retry_policy" {
for_each = each.value.maximum_backoff != null ? [each.value.maximum_backoff] : []
content {
maximum_backoff = each.value.maximum_backoff != null ? each.value.maximum_backoff : ""
minimum_backoff = each.value.minimum_backoff != null ? each.value.minimum_backoff : ""
}
}
bigquery_config {
table = each.value["table"]
use_topic_schema = each.value.use_topic_schema != null ? each.value.use_topic_schema : false
use_table_schema = each.value.use_table_schema != null ? each.value.use_table_schema : false
write_metadata = each.value.write_metadata != null ? each.value.write_metadata : false
drop_unknown_fields = each.value.drop_unknown_fields != null ? each.value.drop_unknown_fields : false
service_account_email = each.value.service_account_email
}
depends_on = [
google_pubsub_topic.topic,
google_project_iam_member.bigquery_metadata_viewer_binding,
google_project_iam_member.bigquery_data_editor_binding
]
}
resource "google_pubsub_subscription" "cloud_storage_subscriptions" {
for_each = var.create_subscriptions ? { for i in var.cloud_storage_subscriptions : i.name => i } : {}
name = each.value.name
topic = var.create_topic ? google_pubsub_topic.topic[0].name : var.topic
project = var.project_id
labels = var.subscription_labels
ack_deadline_seconds = each.value.ack_deadline_seconds != null ? each.value.ack_deadline_seconds : local.default_ack_deadline_seconds
message_retention_duration = each.value.message_retention_duration
retain_acked_messages = each.value.retain_acked_messages
filter = each.value.filter
enable_message_ordering = each.value.enable_message_ordering
dynamic "expiration_policy" {
// check if the 'expiration_policy' key exists, if yes, return a list containing it.
for_each = each.value.expiration_policy != null ? [each.value.expiration_policy] : []
content {
ttl = expiration_policy.value
}
}
dynamic "dead_letter_policy" {
for_each = each.value.dead_letter_topic != null ? [each.value.dead_letter_topic] : []
content {
dead_letter_topic = each.value.dead_letter_topic != null ? each.value.dead_letter_topic : ""
max_delivery_attempts = each.value.max_delivery_attempts != null ? each.value.max_delivery_attempts : "5"
}
}
dynamic "retry_policy" {
for_each = each.value.maximum_backoff != null ? [each.value.maximum_backoff] : []
content {
maximum_backoff = each.value.maximum_backoff != null ? each.value.maximum_backoff : ""
minimum_backoff = each.value.minimum_backoff != null ? each.value.minimum_backoff : ""
}
}
cloud_storage_config {
bucket = each.value["bucket"]
filename_prefix = each.value.filename_prefix
filename_suffix = each.value.filename_suffix
filename_datetime_format = each.value.filename_datetime_format
max_duration = each.value.max_duration
max_bytes = each.value.max_bytes
max_messages = each.value.max_messages
dynamic "avro_config" {
for_each = (each.value.output_format == "avro") ? [true] : []
content {
write_metadata = each.value.write_metadata
use_topic_schema = each.value.use_topic_schema
}
}
}
depends_on = [
google_pubsub_topic.topic,
google_project_iam_member.storage_admin_binding
]
}
resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_subscriber" {
for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if i.service_account != null } : {}
project = var.project_id
subscription = each.value.name
role = "roles/pubsub.subscriber"
member = "serviceAccount:${each.value.service_account}"
depends_on = [
google_pubsub_subscription.pull_subscriptions,
]
lifecycle {
replace_triggered_by = [google_pubsub_subscription.pull_subscriptions[each.key]]
}
}
resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_viewer" {
for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if i.service_account != null } : {}
project = var.project_id
subscription = each.value.name
role = "roles/pubsub.viewer"
member = "serviceAccount:${each.value.service_account}"
depends_on = [
google_pubsub_subscription.pull_subscriptions,
]
lifecycle {
replace_triggered_by = [google_pubsub_subscription.pull_subscriptions[each.key]]
}
}