Skip to content

Commit 3ed8c60

Browse files
authored
Merge pull request #27 from namusyaka/enable-multiple-users
enable multiple users
2 parents 017c79a + 30d0a0b commit 3ed8c60

File tree

10 files changed

+80
-24
lines changed

10 files changed

+80
-24
lines changed

modules/mysql/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
| Name | Description | Type | Default | Required |
88
|------|-------------|:----:|:-----:|:-----:|
99
| activation_policy | The activation policy for the master instance. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `ALWAYS` | no |
10+
| additional_databases | A list of databases to be created in your cluster | list | `<list>` | no |
11+
| additional_users | A list of users to be created in your cluster | list | `<list>` | no |
1012
| authorized_gae_applications | The list of authorized App Engine project names | list | `<list>` | no |
1113
| backup_configuration | The backup configuration block of the Cloud SQL resources This argument will be passed through the master instance directrly.<br><br>See [more details](https://www.terraform.io/docs/providers/google/r/sql_database_instance.html). | map | `<map>` | no |
1214
| database_flags | The database flags for the master instance. See [more details](https://cloud.google.com/sql/docs/mysql/flags) | list | `<list>` | no |

modules/mysql/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,13 @@ resource "google_sql_user" "default" {
9797
password = "${var.user_password == "" ? random_id.user-password.hex : var.user_password}"
9898
depends_on = ["google_sql_database_instance.default"]
9999
}
100+
101+
resource "google_sql_user" "additional_users" {
102+
count = "${length(var.additional_users)}"
103+
project = "${var.project_id}"
104+
name = "${lookup(var.additional_users[count.index], "name")}"
105+
password = "${lookup(var.additional_users[count.index], "password", random_id.user-password.hex)}"
106+
host = "${lookup(var.additional_users[count.index], "host", var.user_host)}"
107+
instance = "${google_sql_database_instance.default.name}"
108+
depends_on = ["google_sql_database_instance.default"]
109+
}

modules/mysql/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ variable "db_collation" {
304304
}
305305

306306
variable "additional_databases" {
307-
description = "The list of databases for the instacne"
307+
description = "A list of databases to be created in your cluster"
308308
default = []
309309
}
310310

@@ -322,3 +322,8 @@ variable "user_password" {
322322
description = "The password for the default user. If not set, a random one will be generated and available in the generated_user_password output variable."
323323
default = ""
324324
}
325+
326+
variable "additional_users" {
327+
description = "A list of users to be created in your cluster"
328+
default = []
329+
}

modules/postgresql/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
| Name | Description | Type | Default | Required |
88
|------|-------------|:----:|:-----:|:-----:|
99
| activation_policy | The activation policy for the master instance.Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `ALWAYS` | no |
10+
| additional_databases | A list of databases to be created in your cluster | list | `<list>` | no |
11+
| additional_users | A list of users to be created in your cluster | list | `<list>` | no |
1012
| authorized_gae_applications | The authorized gae applications for the Cloud SQL instances | list | `<list>` | no |
1113
| availability_type | The availability type for the master instance.This is only used to set up high availability for the PostgreSQL instance. Can be either `ZONAL` or `REGIONAL`. | string | `ZONAL` | no |
1214
| backup_configuration | The backup configuration block of the Cloud SQL resources This argument will be passed through the master instance directrly.<br><br>See [more details](https://www.terraform.io/docs/providers/google/r/sql_database_instance.html). | map | `<map>` | no |
@@ -45,7 +47,6 @@
4547
| read_replica_zones | The zones for the read replica instancess, it should be something like: `a,b,c`. Given zones are used rotationally for creating read replicas. | string | `` | no |
4648
| region | The region of the Cloud SQL resources | string | `us-central1` | no |
4749
| tier | The tier for the master instance. | string | `db-f1-micro` | no |
48-
| user_host | The host for the default user | string | `%` | no |
4950
| user_labels | The key/value labels for the master instances. | map | `<map>` | no |
5051
| user_name | The name of the default user | string | `default` | no |
5152
| user_password | The password for the default user. If not set, a random one will be generated and available in the generated_user_password output variable. | string | `` | no |

modules/postgresql/main.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
locals {
18-
default_user_host = ""
1918
ip_configuration_enabled = "${length(keys(var.ip_configuration)) > 0 ? true : false}"
2019

2120
ip_configurations = {
@@ -93,7 +92,15 @@ resource "google_sql_user" "default" {
9392
name = "${var.user_name}"
9493
project = "${var.project_id}"
9594
instance = "${google_sql_database_instance.default.name}"
96-
host = "${var.user_host}"
9795
password = "${var.user_password == "" ? random_id.user-password.hex : var.user_password}"
9896
depends_on = ["google_sql_database_instance.default"]
9997
}
98+
99+
resource "google_sql_user" "additional_users" {
100+
count = "${length(var.additional_users)}"
101+
project = "${var.project_id}"
102+
name = "${lookup(var.additional_users[count.index], "name")}"
103+
password = "${lookup(var.additional_users[count.index], "password", random_id.user-password.hex)}"
104+
instance = "${google_sql_database_instance.default.name}"
105+
depends_on = ["google_sql_database_instance.default"]
106+
}

modules/postgresql/variables.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ variable "db_collation" {
224224
}
225225

226226
variable "additional_databases" {
227-
description = "The list of databases for the instacne"
227+
description = "A list of databases to be created in your cluster"
228228
default = []
229229
}
230230

@@ -233,12 +233,12 @@ variable "user_name" {
233233
default = "default"
234234
}
235235

236-
variable "user_host" {
237-
description = "The host for the default user"
238-
default = "%"
239-
}
240-
241236
variable "user_password" {
242237
description = "The password for the default user. If not set, a random one will be generated and available in the generated_user_password output variable."
243238
default = ""
244239
}
240+
241+
variable "additional_users" {
242+
description = "A list of users to be created in your cluster"
243+
default = []
244+
}

test/fixtures/mysql-ha/main.tf

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,29 @@ module "mysql" {
133133
}]
134134
}
135135

136-
user_name = "tftest"
137-
user_password = "foobar"
138-
db_name = "${var.mysql_ha_name}"
139-
db_charset = "utf8mb4"
140-
db_collation = "utf8mb4_general_ci"
136+
db_name = "${var.mysql_ha_name}"
137+
db_charset = "utf8mb4"
138+
db_collation = "utf8mb4_general_ci"
141139

142140
additional_databases = [
143141
{
144142
name = "${var.mysql_ha_name}-additional"
145143
charset = "utf8mb4"
146144
collation = "utf8mb4_general_ci"
147-
}
145+
},
146+
]
147+
148+
user_name = "tftest"
149+
user_password = "foobar"
150+
151+
additional_users = [
152+
{
153+
name = "tftest2"
154+
password = "abcdefg"
155+
},
156+
{
157+
name = "tftest3"
158+
host = "localhost"
159+
},
148160
]
149161
}

test/fixtures/postgresql-ha/main.tf

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,29 @@ module "pg" {
9898
}]
9999
}
100100

101-
user_name = "tftest"
102-
user_password = "foobar"
103-
db_name = "${var.pg_ha_name}"
104-
db_charset = "UTF8"
105-
db_collation = "en_US.UTF8"
101+
db_name = "${var.pg_ha_name}"
102+
db_charset = "UTF8"
103+
db_collation = "en_US.UTF8"
106104

107105
additional_databases = [
108106
{
109107
name = "${var.pg_ha_name}-additional"
110108
charset = "UTF8"
111109
collation = "en_US.UTF8"
112-
}
110+
},
111+
]
112+
113+
user_name = "tftest"
114+
user_password = "foobar"
115+
116+
additional_users = [
117+
{
118+
name = "tftest2"
119+
password = "abcdefg"
120+
},
121+
{
122+
name = "tftest3"
123+
host = "localhost"
124+
},
113125
]
114126
}

test/integration/mysql-ha/controls/mysql.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
end
131131
end
132132

133-
describe google_sql_users(project: project_id, database: basename).where(user_name: /\Atftest\z/) do
133+
describe google_sql_users(project: project_id, database: basename).where(user_name: /\Atftest/) do
134+
its(:count) { should be 3 }
135+
it { should exist }
136+
end
137+
138+
describe google_sql_users(project: project_id, database: basename).where(user_host: 'localhost') do
134139
it { should exist }
135140
end

test/integration/postgresql-ha/controls/pg.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
end
9797
end
9898

99-
describe google_sql_users(project: project_id, database: basename).where(user_name: /\Atftest\z/) do
99+
describe google_sql_users(project: project_id, database: basename).where(user_name: /\Atftest/) do
100+
# NOTE: postgresql has `postgres` as a default user.
101+
its(:count) { should be 4 }
100102
it { should exist }
101103
end

0 commit comments

Comments
 (0)