Skip to content

Commit

Permalink
fix: Updated users and databases creation to use for_each (#100)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Users and databases have been moved and require a state migration. See the upgrade guide for details.
  • Loading branch information
Dev25 committed Apr 29, 2020
1 parent 1d9bc25 commit d433995
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 54 deletions.
6 changes: 0 additions & 6 deletions examples/mysql-ha/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ module "mysql" {

additional_databases = [
{
project = var.project_id
instance = local.instance_name
name = "${var.mysql_ha_name}-additional"
charset = "utf8mb4"
collation = "utf8mb4_general_ci"
Expand All @@ -153,18 +151,14 @@ module "mysql" {

additional_users = [
{
project = var.project_id
name = "tftest2"
password = "abcdefg"
host = "localhost"
instance = local.instance_name
},
{
project = var.project_id
name = "tftest3"
password = "abcdefg"
host = "localhost"
instance = local.instance_name
},
]
}
4 changes: 0 additions & 4 deletions examples/mysql-private/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,14 @@ module "safer-mysql-db" {
// Cloud SQL proxy.
additional_users = [
{
project = var.project_id
name = "app"
password = "PaSsWoRd"
host = "localhost"
instance = local.instance_name
},
{
project = var.project_id
name = "readonly"
password = "PaSsWoRd"
host = "localhost"
instance = local.instance_name
},
]

Expand Down
6 changes: 0 additions & 6 deletions examples/postgresql-ha/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ module "pg" {
name = "${var.pg_ha_name}-additional"
charset = "UTF8"
collation = "en_US.UTF8"
instance = local.instance_name
project = var.project_id
},
]

Expand All @@ -152,18 +150,14 @@ module "pg" {

additional_users = [
{
project = var.project_id
name = "tftest2"
password = "abcdefg"
host = "localhost"
instance = local.instance_name
},
{
project = var.project_id
name = "tftest3"
password = "abcdefg"
host = "localhost"
instance = local.instance_name
},
]
}
25 changes: 12 additions & 13 deletions modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ locals {
disabled = {}
}

databases = { for db in var.additional_databases : db.name => db }
users = { for u in var.additional_users : u.name => u }

// HA method using REGIONAL availability_type requires binary logs to be enabled
binary_log_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "binary_log_enabled", null)
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", null)
Expand Down Expand Up @@ -117,11 +120,11 @@ resource "google_sql_database" "default" {
}

resource "google_sql_database" "additional_databases" {
count = length(var.additional_databases)
for_each = local.databases
project = var.project_id
name = var.additional_databases[count.index]["name"]
charset = lookup(var.additional_databases[count.index], "charset", null)
collation = lookup(var.additional_databases[count.index], "collation", null)
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]
}
Expand All @@ -145,15 +148,11 @@ resource "google_sql_user" "default" {
}

resource "google_sql_user" "additional_users" {
count = length(var.additional_users)
project = var.project_id
name = var.additional_users[count.index]["name"]
password = lookup(
var.additional_users[count.index],
"password",
random_id.user-password.hex,
)
host = lookup(var.additional_users[count.index], "host", var.user_host)
for_each = local.users
project = var.project_id
name = each.value.name
password = lookup(each.value, "password", random_id.user-password.hex)
host = lookup(each.value, "host", var.user_host)
instance = google_sql_database_instance.default.name
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
Expand Down
4 changes: 0 additions & 4 deletions modules/mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,9 @@ variable "db_collation" {
variable "additional_databases" {
description = "A list of databases to be created in your cluster"
type = list(object({
project = string
name = string
charset = string
collation = string
instance = string
}))
default = []
}
Expand All @@ -501,11 +499,9 @@ variable "user_password" {
variable "additional_users" {
description = "A list of users to be created in your cluster"
type = list(object({
project = string
name = string
password = string
host = string
instance = string
}))
default = []
}
Expand Down
23 changes: 11 additions & 12 deletions modules/postgresql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ locals {
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 }
}

resource "google_sql_database_instance" "default" {
Expand Down Expand Up @@ -112,11 +115,11 @@ resource "google_sql_database" "default" {
}

resource "google_sql_database" "additional_databases" {
count = length(var.additional_databases)
for_each = local.databases
project = var.project_id
name = var.additional_databases[count.index]["name"]
charset = lookup(var.additional_databases[count.index], "charset", "")
collation = lookup(var.additional_databases[count.index], "collation", "")
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]
}
Expand All @@ -139,14 +142,10 @@ resource "google_sql_user" "default" {
}

resource "google_sql_user" "additional_users" {
count = length(var.additional_users)
project = var.project_id
name = var.additional_users[count.index]["name"]
password = lookup(
var.additional_users[count.index],
"password",
random_id.user-password.hex,
)
for_each = local.users
project = var.project_id
name = each.value.name
password = lookup(each.value, "password", random_id.user-password.hex)
instance = google_sql_database_instance.default.name
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
}
Expand Down
5 changes: 0 additions & 5 deletions modules/postgresql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,9 @@ variable "db_collation" {
variable "additional_databases" {
description = "A list of databases to be created in your cluster"
type = list(object({
project = string
name = string
charset = string
collation = string
instance = string
}))
default = []
}
Expand All @@ -328,11 +326,8 @@ variable "user_password" {
variable "additional_users" {
description = "A list of users to be created in your cluster"
type = list(object({
project = string
name = string
password = string
host = string
instance = string
}))
default = []
}
Expand Down
4 changes: 0 additions & 4 deletions modules/safer_mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,9 @@ variable "db_collation" {
variable "additional_databases" {
description = "A list of databases to be created in your cluster"
type = list(object({
project = string
name = string
charset = string
collation = string
instance = string
}))
default = []
}
Expand All @@ -456,11 +454,9 @@ variable "user_password" {
variable "additional_users" {
description = "A list of users to be created in your cluster"
type = list(object({
project = string
name = string
password = string
host = string
instance = string
}))
default = []
}
Expand Down

0 comments on commit d433995

Please sign in to comment.