-
Notifications
You must be signed in to change notification settings - Fork 92
/
main.tf
151 lines (139 loc) · 6.18 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
/**
* Copyright 2024 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.
*/
#-----------------#
# Local variables #
#-----------------#
locals {
is_project_level = var.parent_resource_type == "project"
is_folder_level = var.parent_resource_type == "folder"
is_org_level = var.parent_resource_type == "organization"
is_billing_level = var.parent_resource_type == "billing_account"
# Locals for outputs to ensure the value is available after the resource is created
log_sink_writer_identity = local.is_project_level ? element(concat(google_logging_project_sink.sink[*].writer_identity, [""]), 0) : local.is_folder_level ? element(concat(google_logging_folder_sink.sink[*].writer_identity, [""]), 0) : local.is_org_level ? element(concat(google_logging_organization_sink.sink[*].writer_identity, [""]), 0) : local.is_billing_level ? element(concat(google_logging_billing_account_sink.sink[*].writer_identity, [""]), 0) : ""
log_sink_resource_id = local.is_project_level ? element(concat(google_logging_project_sink.sink[*].id, [""]), 0) : local.is_folder_level ? element(concat(google_logging_folder_sink.sink[*].id, [""]), 0) : local.is_org_level ? element(concat(google_logging_organization_sink.sink[*].id, [""]), 0) : local.is_billing_level ? element(concat(google_logging_billing_account_sink.sink[*].id, [""]), 0) : ""
log_sink_resource_name = local.is_project_level ? element(concat(google_logging_project_sink.sink[*].name, [""]), 0) : local.is_folder_level ? element(concat(google_logging_folder_sink.sink[*].name, [""]), 0) : local.is_org_level ? element(concat(google_logging_organization_sink.sink[*].name, [""]), 0) : local.is_billing_level ? element(concat(google_logging_billing_account_sink.sink[*].name, [""]), 0) : ""
log_sink_parent_id = local.is_project_level ? element(concat(google_logging_project_sink.sink[*].project, [""]), 0) : local.is_folder_level ? element(concat(google_logging_folder_sink.sink[*].folder, [""]), 0) : local.is_org_level ? element(concat(google_logging_organization_sink.sink[*].org_id, [""]), 0) : local.is_billing_level ? element(concat(google_logging_billing_account_sink.sink[*].billing_account, [""]), 0) : ""
# Bigquery sink options
bigquery_options = var.bigquery_options == null ? [] : var.unique_writer_identity == true ? tolist([var.bigquery_options]) : []
}
#-----------#
# Log sinks #
#-----------#
# Project-level
resource "google_logging_project_sink" "sink" {
count = local.is_project_level ? 1 : 0
name = var.log_sink_name
description = var.description
project = var.parent_resource_id
filter = var.filter
destination = var.destination_uri
unique_writer_identity = var.unique_writer_identity
disabled = var.disabled
dynamic "bigquery_options" {
for_each = local.bigquery_options
content {
use_partitioned_tables = bigquery_options.value.use_partitioned_tables
}
}
dynamic "exclusions" {
for_each = var.exclusions
content {
name = exclusions.value.name
description = exclusions.value.description
filter = exclusions.value.filter
disabled = exclusions.value.disabled
}
}
}
# Folder-level
resource "google_logging_folder_sink" "sink" {
count = local.is_folder_level ? 1 : 0
name = var.log_sink_name
description = var.description
folder = var.parent_resource_id
filter = var.filter
include_children = var.include_children
intercept_children = var.intercept_children
destination = var.destination_uri
disabled = var.disabled
dynamic "bigquery_options" {
for_each = local.bigquery_options
content {
use_partitioned_tables = bigquery_options.value.use_partitioned_tables
}
}
dynamic "exclusions" {
for_each = var.exclusions
content {
name = exclusions.value.name
description = exclusions.value.description
filter = exclusions.value.filter
disabled = exclusions.value.disabled
}
}
}
# Org-level
resource "google_logging_organization_sink" "sink" {
count = local.is_org_level ? 1 : 0
name = var.log_sink_name
description = var.description
org_id = var.parent_resource_id
filter = var.filter
include_children = var.include_children
intercept_children = var.intercept_children
destination = var.destination_uri
disabled = var.disabled
dynamic "bigquery_options" {
for_each = local.bigquery_options
content {
use_partitioned_tables = bigquery_options.value.use_partitioned_tables
}
}
dynamic "exclusions" {
for_each = var.exclusions
content {
name = exclusions.value.name
description = exclusions.value.description
filter = exclusions.value.filter
disabled = exclusions.value.disabled
}
}
}
# Billing Account-level
resource "google_logging_billing_account_sink" "sink" {
count = local.is_billing_level ? 1 : 0
name = var.log_sink_name
description = var.description
billing_account = var.parent_resource_id
filter = var.filter
destination = var.destination_uri
disabled = var.disabled
dynamic "bigquery_options" {
for_each = local.bigquery_options
content {
use_partitioned_tables = bigquery_options.value.use_partitioned_tables
}
}
dynamic "exclusions" {
for_each = var.exclusions
content {
name = exclusions.value.name
description = exclusions.value.description
filter = exclusions.value.filter
disabled = exclusions.value.disabled
}
}
}