-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
250 lines (203 loc) · 9.92 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
locals {
backup_configuration = length(var.backup_configuration) > 0 ? [var.backup_configuration] : []
ip_configuration = length(var.ip_configuration) > 0 ? [var.ip_configuration] : []
location_preference = length(var.location_preference) > 0 ? [var.location_preference] : []
maintenance_window = length(var.maintenance_window) > 0 ? [var.maintenance_window] : []
deny_maintenance_period = length(var.deny_maintenance_period) > 0 ? [var.deny_maintenance_period] : []
insights_config = length(var.insights_config) > 0 ? [var.insights_config] : []
password_validation_policy = length(var.password_validation_policy) > 0 ? [var.password_validation_policy] : []
replica_configuration = var.master_instance_name != null && length(var.replica_configuration) > 0 ? [var.replica_configuration] : []
data_cache_config = length(var.data_cache_config) > 0 ? [var.data_cache_config] : []
}
resource "google_sql_database_instance" "instance" {
count = var.module_enabled ? 1 : 0
depends_on = [var.module_depends_on]
name = var.name
database_version = var.database_version
region = var.region
master_instance_name = var.master_instance_name
project = var.project
deletion_protection = var.deletion_protection
encryption_key_name = var.encryption_key_name
settings {
tier = var.tier
activation_policy = var.activation_policy
availability_type = var.availability_type
deletion_protection_enabled = var.deletion_protection_enabled
# disable disk_autoresize if the user requested a specific disk_size
disk_autoresize = var.disk_size != null ? false : 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
user_labels = var.user_labels
dynamic "data_cache_config" {
for_each = local.data_cache_config
content {
data_cache_enabled = try(data_cache_config.value.data_cache_enabled, false)
}
}
dynamic "database_flags" {
for_each = var.database_flags
content {
name = database_flags.value.name
value = database_flags.value.value
}
}
dynamic "backup_configuration" {
for_each = local.backup_configuration
content {
binary_log_enabled = try(backup_configuration.value.binary_log_enabled, null)
enabled = try(backup_configuration.value.enabled, null)
start_time = try(backup_configuration.value.start_time, null)
point_in_time_recovery_enabled = try(backup_configuration.value.point_in_time_recovery_enabled, null)
location = try(backup_configuration.value.location, null)
transaction_log_retention_days = try(backup_configuration.value.transaction_log_retention_days, null)
dynamic "backup_retention_settings" {
for_each = try(backup_configuration.value.backup_retention_settings, [])
content {
retained_backups = try(backup_retention_settings.value.retained_backups, null)
retention_unit = try(backup_retention_settings.value.retention_unit, "COUNT")
}
}
}
}
dynamic "ip_configuration" {
for_each = local.ip_configuration
content {
ipv4_enabled = try(ip_configuration.value.ipv4_enabled, null)
private_network = try(ip_configuration.value.private_network, null)
require_ssl = try(ip_configuration.value.require_ssl, null)
ssl_mode = try(ip_configuration.value.ssl_mode, null)
allocated_ip_range = try(ip_configuration.value.allocated_ip_range, null)
enable_private_path_for_google_cloud_services = try(ip_configuration.value.enable_private_path_for_google_cloud_services, null)
dynamic "authorized_networks" {
for_each = try(ip_configuration.value.authorized_networks, [])
content {
expiration_time = try(authorized_networks.value.expiration_time, null)
name = try(authorized_networks.value.name, null)
value = try(authorized_networks.value.value, null)
}
}
dynamic "psc_config" {
for_each = try(ip_configuration.value.psc_config, [])
content {
psc_enabled = try(psc_config.value.psc_enabled, false)
allowed_consumer_projects = try(psc_config.value.allowed_consumer_projects, [])
}
}
}
}
dynamic "location_preference" {
for_each = local.location_preference
content {
zone = try(location_preference.value.zone, null)
}
}
dynamic "maintenance_window" {
for_each = local.maintenance_window
content {
day = try(maintenance_window.value.day, null)
hour = try(maintenance_window.value.hour, null)
update_track = try(maintenance_window.value.update_track, null)
}
}
dynamic "deny_maintenance_period" {
for_each = local.deny_maintenance_period
content {
start_date = try(deny_maintenance_period.value.start_date, null)
end_date = try(deny_maintenance_period.value.end_date, null)
time = try(deny_maintenance_period.value.time, null)
}
}
dynamic "insights_config" {
for_each = local.insights_config
content {
query_insights_enabled = try(insights_config.value.query_insights_enabled, null)
query_string_length = try(insights_config.value.query_string_length, null)
record_application_tags = try(insights_config.value.record_application_tags, null)
record_client_address = try(insights_config.value.record_client_address, null)
}
}
dynamic "password_validation_policy" {
for_each = local.password_validation_policy
content {
min_length = try(password_validation_policy.value.min_length, null)
complexity = try(password_validation_policy.value.complexity, null)
reuse_interval = try(password_validation_policy.value.reuse_interval, null)
disallow_username_substring = try(password_validation_policy.value.disallow_username_substring, null)
password_change_interval = try(password_validation_policy.value.password_change_interval, null)
enable_password_policy = try(password_validation_policy.value.enable_password_policy, null)
}
}
}
dynamic "replica_configuration" {
for_each = local.replica_configuration
content {
ca_certificate = try(replica_configuration.value.ca_certificate, null)
client_certificate = try(replica_configuration.value.client_certificate, null)
client_key = try(replica_configuration.value.client_key, null)
connect_retry_interval = try(replica_configuration.value.connect_retry_interval, null)
dump_file_path = try(replica_configuration.value.dump_file_path, null)
failover_target = try(replica_configuration.value.failover_target, null)
master_heartbeat_period = try(replica_configuration.value.master_heartbeat_period, null)
password = try(replica_configuration.value.password, null)
ssl_cipher = try(replica_configuration.value.ssl_cipher, null)
username = try(replica_configuration.value.username, null)
verify_server_certificate = try(replica_configuration.value.verify_server_certificate, null)
}
}
timeouts {
create = try(var.module_timeouts.google_sql_database_instance.create, "30m")
update = try(var.module_timeouts.google_sql_database_instance.update, "30m")
delete = try(var.module_timeouts.google_sql_database_instance.delete, "30m")
}
}
locals {
sql_databases = { for b in var.sql_databases : try(b.id, b.name) => b }
}
resource "google_sql_database" "database" {
for_each = var.module_enabled ? local.sql_databases : {}
project = var.project
instance = google_sql_database_instance.instance[0].name
name = each.value.name
charset = try(each.value.charset, null)
collation = try(each.value.collation, null)
deletion_policy = try(each.value.deletion_policy, "DELETE")
timeouts {
create = try(var.module_timeouts.google_sql_database.create, "15m")
update = try(var.module_timeouts.google_sql_database.update, "10m")
delete = try(var.module_timeouts.google_sql_database.delete, "10m")
}
}
locals {
sql_ssl_certs = { for b in var.sql_ssl_certs : try(b.id, b.common_name) => b }
}
resource "google_sql_ssl_cert" "client_cert" {
for_each = var.module_enabled ? local.sql_ssl_certs : {}
project = var.project
instance = google_sql_database_instance.instance[0].name
common_name = each.value.common_name
timeouts {
create = try(var.module_timeouts.google_sql_ssl_cert.create, "10m")
delete = try(var.module_timeouts.google_sql_ssl_cert.delete, "10m")
}
}
locals {
sql_users = { for b in var.sql_users : b.name => b }
}
resource "google_sql_user" "users" {
for_each = var.module_enabled ? local.sql_users : {}
project = var.project
instance = google_sql_database_instance.instance[0].name
name = each.value.name
password = try(each.value.password, null)
type = try(each.value.type, null)
deletion_policy = try(each.value.deletion_policy, null)
host = try(each.value.host, null)
timeouts {
create = try(var.module_timeouts.google_sql_user.create, "10m")
update = try(var.module_timeouts.google_sql_user.update, "10m")
delete = try(var.module_timeouts.google_sql_user.delete, "10m")
}
}