forked from terraform-google-modules/terraform-google-sql-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
256 lines (223 loc) · 8.5 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
/**
* Copyright 2019 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.
*/
locals {
master_instance_name = var.random_instance_name ? "${var.name}-${random_id.suffix[0].hex}" : var.name
ip_configuration_enabled = length(keys(var.ip_configuration)) > 0 ? true : false
ip_configurations = {
enabled = var.ip_configuration
disabled = {}
}
databases = { for db in var.additional_databases : db.name => db }
users = { for u in var.additional_users : u.name => u }
iam_users = [for iu in var.iam_user_emails : {
email = iu,
is_account_sa = trimsuffix(iu, "gserviceaccount.com") == iu ? false : true
}]
// HA method using REGIONAL availability_type requires point in time recovery to be enabled
point_in_time_recovery_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "point_in_time_recovery_enabled", false)
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", false)
retained_backups = lookup(var.backup_configuration, "retained_backups", null)
retention_unit = lookup(var.backup_configuration, "retention_unit", null)
}
resource "random_id" "suffix" {
count = var.random_instance_name ? 1 : 0
byte_length = 4
}
resource "google_sql_database_instance" "default" {
provider = google-beta
project = var.project_id
name = local.master_instance_name
database_version = var.database_version
region = var.region
encryption_key_name = var.encryption_key_name
deletion_protection = var.deletion_protection
settings {
tier = var.tier
activation_policy = var.activation_policy
availability_type = var.availability_type
dynamic "backup_configuration" {
for_each = [var.backup_configuration]
content {
binary_log_enabled = false
enabled = local.backups_enabled
start_time = lookup(backup_configuration.value, "start_time", null)
location = lookup(backup_configuration.value, "location", null)
point_in_time_recovery_enabled = local.point_in_time_recovery_enabled
transaction_log_retention_days = lookup(backup_configuration.value, "transaction_log_retention_days", null)
dynamic "backup_retention_settings" {
for_each = local.retained_backups != null || local.retention_unit != null ? [var.backup_configuration] : []
content {
retained_backups = local.retained_backups
retention_unit = local.retention_unit
}
}
}
}
dynamic "ip_configuration" {
for_each = [local.ip_configurations[local.ip_configuration_enabled ? "enabled" : "disabled"]]
content {
ipv4_enabled = lookup(ip_configuration.value, "ipv4_enabled", null)
private_network = lookup(ip_configuration.value, "private_network", null)
require_ssl = lookup(ip_configuration.value, "require_ssl", null)
allocated_ip_range = lookup(ip_configuration.value, "allocated_ip_range", null)
dynamic "authorized_networks" {
for_each = lookup(ip_configuration.value, "authorized_networks", [])
content {
expiration_time = lookup(authorized_networks.value, "expiration_time", null)
name = lookup(authorized_networks.value, "name", null)
value = lookup(authorized_networks.value, "value", null)
}
}
}
}
dynamic "insights_config" {
for_each = var.insights_config != null ? [var.insights_config] : []
content {
query_insights_enabled = true
query_string_length = lookup(insights_config.value, "query_string_length", 1024)
record_application_tags = lookup(insights_config.value, "record_application_tags", false)
record_client_address = lookup(insights_config.value, "record_client_address", false)
}
}
disk_autoresize = var.disk_autoresize
disk_autoresize_limit = var.disk_autoresize_limit
disk_size = var.disk_size
disk_type = var.disk_type
pricing_plan = var.pricing_plan
dynamic "database_flags" {
for_each = var.database_flags
content {
name = lookup(database_flags.value, "name", null)
value = lookup(database_flags.value, "value", null)
}
}
user_labels = var.user_labels
location_preference {
zone = var.zone
}
maintenance_window {
day = var.maintenance_window_day
hour = var.maintenance_window_hour
update_track = var.maintenance_window_update_track
}
}
lifecycle {
ignore_changes = [
settings[0].disk_size
]
}
timeouts {
create = var.create_timeout
update = var.update_timeout
delete = var.delete_timeout
}
depends_on = [null_resource.module_depends_on]
}
resource "google_sql_database" "default" {
count = var.enable_default_db ? 1 : 0
name = var.db_name
project = var.project_id
instance = google_sql_database_instance.default.name
charset = var.db_charset
collation = var.db_collation
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
resource "google_sql_database" "additional_databases" {
for_each = local.databases
project = var.project_id
name = each.value.name
charset = lookup(each.value, "charset", null)
collation = lookup(each.value, "collation", null)
instance = google_sql_database_instance.default.name
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
resource "random_password" "user-password" {
keepers = {
name = google_sql_database_instance.default.name
}
length = 32
special = false
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
resource "random_password" "additional_passwords" {
for_each = local.users
keepers = {
name = google_sql_database_instance.default.name
}
length = 32
special = false
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
resource "google_sql_user" "default" {
count = var.enable_default_user ? 1 : 0
name = var.user_name
project = var.project_id
instance = google_sql_database_instance.default.name
password = var.user_password == "" ? random_password.user-password.result : var.user_password
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}
resource "google_sql_user" "additional_users" {
for_each = local.users
project = var.project_id
name = each.value.name
password = coalesce(each.value["password"], random_password.additional_passwords[each.value.name].result)
instance = google_sql_database_instance.default.name
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}
resource "google_project_iam_member" "iam_binding" {
for_each = {
for iu in local.iam_users :
"${iu.email} ${iu.is_account_sa}" => iu
}
project = var.project_id
role = "roles/cloudsql.instanceUser"
member = each.value.is_account_sa ? (
"serviceAccount:${each.value.email}"
) : (
"user:${each.value.email}"
)
}
resource "google_sql_user" "iam_account" {
for_each = {
for iu in local.iam_users :
"${iu.email} ${iu.is_account_sa}" => iu
}
project = var.project_id
name = each.value.is_account_sa ? (
trimsuffix(each.value.email, ".gserviceaccount.com")
) : (
each.value.email
)
instance = google_sql_database_instance.default.name
type = each.value.is_account_sa ? "CLOUD_IAM_SERVICE_ACCOUNT" : "CLOUD_IAM_USER"
depends_on = [
null_resource.module_depends_on,
google_project_iam_member.iam_binding,
]
}
resource "null_resource" "module_depends_on" {
triggers = {
value = length(var.module_depends_on)
}
}