Skip to content

Commit 3de26ab

Browse files
committed
enable mutltiple databases
To support multiple databases, this commit adds the `additional_databases` argument in both modules.
1 parent 52a7b8e commit 3de26ab

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

modules/mysql/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ resource "google_sql_database" "default" {
7070
depends_on = ["google_sql_database_instance.default"]
7171
}
7272

73+
resource "google_sql_database" "additional_databases" {
74+
count = "${length(var.additional_databases)}"
75+
project = "${var.project_id}"
76+
name = "${lookup(var.additional_databases[count.index], "name")}"
77+
charset = "${lookup(var.additional_databases[count.index], "charset", "")}"
78+
collation = "${lookup(var.additional_databases[count.index], "collation", "")}"
79+
instance = "${google_sql_database_instance.default.name}"
80+
depends_on = ["google_sql_database_instance.default"]
81+
}
82+
7383
resource "random_id" "user-password" {
7484
keepers = {
7585
name = "${google_sql_database_instance.default.name}"

modules/mysql/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ variable "db_collation" {
303303
default = ""
304304
}
305305

306+
variable "additional_databases" {
307+
description = "The list of databases for the instacne"
308+
default = []
309+
}
310+
306311
variable "user_name" {
307312
description = "The name of the default user"
308313
default = "default"

modules/postgresql/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ resource "google_sql_database" "default" {
7070
depends_on = ["google_sql_database_instance.default"]
7171
}
7272

73+
resource "google_sql_database" "additional_databases" {
74+
count = "${length(var.additional_databases)}"
75+
project = "${var.project_id}"
76+
name = "${lookup(var.additional_databases[count.index], "name")}"
77+
charset = "${lookup(var.additional_databases[count.index], "charset", "")}"
78+
collation = "${lookup(var.additional_databases[count.index], "collation", "")}"
79+
instance = "${google_sql_database_instance.default.name}"
80+
depends_on = ["google_sql_database_instance.default"]
81+
}
82+
7383
resource "random_id" "user-password" {
7484
keepers = {
7585
name = "${google_sql_database_instance.default.name}"

modules/postgresql/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ variable "db_collation" {
223223
default = ""
224224
}
225225

226+
variable "additional_databases" {
227+
description = "The list of databases for the instacne"
228+
default = []
229+
}
230+
226231
variable "user_name" {
227232
description = "The name of the default user"
228233
default = "default"

test/fixtures/mysql-ha/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,12 @@ module "mysql" {
138138
db_name = "${var.mysql_ha_name}"
139139
db_charset = "utf8mb4"
140140
db_collation = "utf8mb4_general_ci"
141+
142+
additional_databases = [
143+
{
144+
name = "${var.mysql_ha_name}-additional"
145+
charset = "utf8mb4"
146+
collation = "utf8mb4_general_ci"
147+
}
148+
]
141149
}

test/fixtures/postgresql-ha/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,12 @@ module "pg" {
103103
db_name = "${var.pg_ha_name}"
104104
db_charset = "UTF8"
105105
db_collation = "en_US.UTF8"
106+
107+
additional_databases = [
108+
{
109+
name = "${var.pg_ha_name}-additional"
110+
charset = "UTF8"
111+
collation = "en_US.UTF8"
112+
}
113+
]
106114
}

0 commit comments

Comments
 (0)