Skip to content

Commit 46a1d33

Browse files
author
Evy Bongers
authored
Add all provider defaults (#4)
1 parent 29e4351 commit 46a1d33

File tree

6 files changed

+115
-21
lines changed

6 files changed

+115
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.terraform.lock.hcl
2+
.terraform/

database.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ resource "google_sql_database" "database" {
33

44
instance = google_sql_database_instance.instance.name
55
name = each.value
6+
7+
charset = "UTF8"
8+
collation = "en_US.UTF8"
69
}

instance.tf

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,77 @@ resource "google_sql_database_instance" "instance" {
33
name = var.instance_name
44
deletion_protection = var.deletion_protection
55
master_instance_name = var.primary_instance_name
6+
67
settings {
8+
activation_policy = "ALWAYS"
79
availability_type = var.highly_available == true ? "REGIONAL" : "ZONAL"
810
disk_autoresize = var.storage_autoresize
911
disk_autoresize_limit = var.storage_limit
1012
disk_size = local.storage_size
1113
disk_type = "PD_SSD"
14+
pricing_plan = "PER_USE"
1215
tier = local.tier
1316
user_labels = local.labels
17+
1418
backup_configuration {
15-
enabled = local.backup_config.enabled
16-
location = local.backup_config.location
19+
enabled = local.backup_config.enabled
20+
start_time = local.backup_config.start_time
21+
point_in_time_recovery_enabled = local.backup_config.point_in_time_recovery_enabled
22+
location = local.backup_config.location
23+
transaction_log_retention_days = local.backup_config.transaction_log_retention_days
24+
backup_retention_settings {
25+
retained_backups = local.backup_config.retained_backups
26+
retention_unit = "COUNT"
27+
}
1728
}
29+
1830
dynamic "database_flags" {
1931
for_each = var.flags
2032
iterator = flag
33+
2134
content {
2235
name = flag.key
2336
value = flag.value
2437
}
2538
}
39+
40+
insights_config {
41+
query_insights_enabled = local.insights_config.query_insights_enabled
42+
query_string_length = local.insights_config.query_string_length
43+
record_application_tags = local.insights_config.record_application_tags
44+
record_client_address = local.insights_config.record_client_address
45+
}
46+
2647
ip_configuration {
27-
ipv4_enabled = true
28-
require_ssl = true
48+
ipv4_enabled = var.ipv4_enabled
49+
private_network = var.private_network
50+
51+
require_ssl = true
2952

3053
dynamic "authorized_networks" {
3154
for_each = var.authorized_networks
3255
iterator = network
3356

3457
content {
35-
name = network.value.name
36-
value = network.value.network
58+
expiration_time = network.value.expiration_time
59+
name = network.value.name
60+
value = network.value.network
3761
}
3862
}
3963
}
64+
65+
location_preference {
66+
follow_gae_application = null
67+
zone = null
68+
}
69+
4070
dynamic "maintenance_window" {
41-
for_each = var.primary_instance_name == null ? [0] : []
71+
for_each = local.needs_maintenance_window
72+
4273
content {
43-
day = 1
44-
hour = 4
74+
day = var.maintenance_window.day
75+
hour = var.maintenance_window.hour
76+
update_track = lookup(var.maintenance_window, "update_track", "stable")
4577
}
4678
}
4779
}

locals.tf

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
locals {
22
default_backup_config = {
3-
enabled = var.highly_available == true ? true : false
4-
location = "eu"
3+
enabled = var.highly_available
4+
location = "eu"
5+
point_in_time_recovery_enabled = false
6+
retained_backups = 7
7+
transaction_log_retention_days = 7
8+
}
9+
default_insights_config = {
10+
query_string_length = 1024
11+
record_application_tags = false
12+
record_client_address = false
513
}
614
default_labels = {
715
env = var.environment
816
}
917
default_tier = var.environment == "production" ? "db-custom-2-8192" : "db-f1-micro"
1018

11-
backup_config = defaults(var.backup_config, local.default_backup_config)
12-
labels = merge(local.default_labels, var.labels)
13-
storage_size = var.storage_autoresize == true ? null : var.storage_size
14-
tier = var.tier != null ? var.tier : local.default_tier
19+
backup_config = defaults(var.backup_config, local.default_backup_config)
20+
insights_config = defaults(var.insights_config, local.default_insights_config)
21+
22+
labels = merge(local.default_labels, var.labels)
23+
storage_size = var.storage_autoresize == true ? null : var.storage_size
24+
tier = var.tier != null ? var.tier : local.default_tier
1525

1626
admin_user = "postgres"
27+
28+
# Helper vars for optional blocks. Will either be an empty set (no) or a
29+
# single item set (yes)
30+
needs_maintenance_window = var.primary_instance_name == null ? [] : [0]
1731
}

user.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ resource "random_password" "admin_user" {
44

55
resource "google_sql_user" "postgres_user" {
66
instance = google_sql_database_instance.instance.name
7+
78
name = local.admin_user
89
password = random_password.admin_user.result
10+
type = "BUILT_IN"
11+
12+
deletion_policy = null
913
}

variables.tf

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
variable "authorized_networks" {
22
type = list(object({
3-
name = string
4-
network = string
3+
name = string
4+
network = string
5+
expiration_time = optional(string)
56
}))
67
default = []
78
}
89

910
variable "backup_config" {
1011
type = object({
11-
enabled = optional(bool)
12-
location = optional(string)
12+
enabled = optional(bool)
13+
start_time = optional(string)
14+
point_in_time_recovery_enabled = optional(bool)
15+
location = optional(string)
16+
transaction_log_retention_days = optional(number)
17+
retained_backups = optional(number)
1318
})
14-
default = {
15-
}
19+
default = {}
1620
}
1721

1822
variable "database_version" {
@@ -49,16 +53,51 @@ variable "instance_name" {
4953
type = string
5054
}
5155

56+
variable "insights_config" {
57+
type = object({
58+
query_insights_enabled = bool
59+
query_string_length = optional(number)
60+
record_application_tags = optional(bool)
61+
record_client_address = optional(bool)
62+
})
63+
default = {
64+
query_insights_enabled = true
65+
}
66+
}
67+
68+
variable "ipv4_enabled" {
69+
type = bool
70+
default = false
71+
}
72+
5273
variable "labels" {
5374
type = map(string)
5475
default = {}
5576
}
5677

78+
variable "maintenance_window" {
79+
type = object({
80+
day = number
81+
hour = number
82+
update_track = optional(string)
83+
})
84+
default = {
85+
day = 1
86+
hour = 4
87+
update_track = "stable"
88+
}
89+
}
90+
5791
variable "primary_instance_name" {
5892
type = string
5993
default = null
6094
}
6195

96+
variable "private_network" {
97+
type = string
98+
default = null
99+
}
100+
62101
variable "storage_autoresize" {
63102
type = bool
64103
}

0 commit comments

Comments
 (0)