Skip to content

Commit 9857ac7

Browse files
authored
Merge pull request #11 from adrianlop/dbtags_and_replica
dbtags and replica support
2 parents 40f5a39 + 8d22beb commit 9857ac7

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

examples/mysql-and-postgres/main.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ data "google_client_config" "current" {}
3434

3535
data "google_compute_subnetwork" "default-us-central1" {
3636
project = "${data.google_client_config.current.project}"
37-
region = "${var.region}"
38-
name = "default"
37+
region = "${var.region}"
38+
name = "default"
3939
}
4040

4141
resource "random_id" "name" {
@@ -47,12 +47,20 @@ module "mysql-db" {
4747
source = "../../"
4848
name = "example-mysql-${random_id.name.hex}"
4949
database_version = "MYSQL_5_6"
50+
5051
ip_configuration = [{
5152
authorized_networks = [{
52-
name = "default"
53+
name = "default"
5354
value = "${data.google_compute_subnetwork.default-us-central1.ip_cidr_range}"
5455
}]
5556
}]
57+
58+
database_flags = [
59+
{
60+
name = "log_bin_trust_function_creators"
61+
value = "on"
62+
},
63+
]
5664
}
5765

5866
module "postgresql-db" {
@@ -61,9 +69,10 @@ module "postgresql-db" {
6169
name = "example-postgresql-${random_id.name.hex}"
6270
user_host = ""
6371
database_version = "POSTGRES_9_6"
72+
6473
ip_configuration = [{
6574
authorized_networks = [{
66-
name = "default"
75+
name = "default"
6776
value = "${data.google_compute_subnetwork.default-us-central1.ip_cidr_range}"
6877
}]
6978
}]

main.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
resource "google_sql_database_instance" "master" {
18-
name = "${var.name}"
19-
project = "${var.project}"
20-
region = "${var.region}"
21-
database_version = "${var.database_version}"
17+
resource "google_sql_database_instance" "default" {
18+
name = "${var.name}"
19+
project = "${var.project}"
20+
region = "${var.region}"
21+
database_version = "${var.database_version}"
22+
master_instance_name = "${var.master_instance_name}"
2223

2324
settings {
2425
tier = "${var.tier}"
@@ -33,15 +34,17 @@ resource "google_sql_database_instance" "master" {
3334
disk_type = "${var.disk_type}"
3435
pricing_plan = "${var.pricing_plan}"
3536
replication_type = "${var.replication_type}"
37+
database_flags = ["${var.database_flags}"]
3638
}
3739

3840
replica_configuration = ["${var.replica_configuration}"]
3941
}
4042

4143
resource "google_sql_database" "default" {
44+
count = "${var.master_instance_name == "" ? 1 : 0}"
4245
name = "${var.db_name}"
4346
project = "${var.project}"
44-
instance = "${google_sql_database_instance.master.name}"
47+
instance = "${google_sql_database_instance.default.name}"
4548
charset = "${var.db_charset}"
4649
collation = "${var.db_collation}"
4750
}
@@ -51,9 +54,10 @@ resource "random_id" "user-password" {
5154
}
5255

5356
resource "google_sql_user" "default" {
57+
count = "${var.master_instance_name == "" ? 1 : 0}"
5458
name = "${var.user_name}"
5559
project = "${var.project}"
56-
instance = "${google_sql_database_instance.master.name}"
60+
instance = "${google_sql_database_instance.default.name}"
5761
host = "${var.user_host}"
5862
password = "${var.user_password == "" ? random_id.user-password.hex : var.user_password}"
5963
}

outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616

1717
output instance_name {
1818
description = "The name of the database instance"
19-
value = "${google_sql_database_instance.master.name}"
19+
value = "${google_sql_database_instance.default.name}"
2020
}
2121

2222
output instance_address {
2323
description = "The IPv4 address of the master database instnace"
24-
value = "${google_sql_database_instance.master.ip_address.0.ip_address}"
24+
value = "${google_sql_database_instance.default.ip_address.0.ip_address}"
2525
}
2626

2727
output instance_address_time_to_retire {
2828
description = "The time the master instance IP address will be reitred. RFC 3339 format."
29-
value = "${google_sql_database_instance.master.ip_address.0.time_to_retire}"
29+
value = "${google_sql_database_instance.default.ip_address.0.time_to_retire}"
3030
}
3131

3232
output self_link {
3333
description = "Self link to the master instance"
34-
value = "${google_sql_database_instance.master.self_link}"
34+
value = "${google_sql_database_instance.default.self_link}"
3535
}
3636

3737
output generated_user_password {

variables.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ variable database_version {
3333
default = "MYSQL_5_6"
3434
}
3535

36+
variable master_instance_name {
37+
description = "The name of the master instance to replicate"
38+
default = ""
39+
}
40+
3641
variable tier {
3742
description = "The machine tier (First Generation) or type (Second Generation). See this page for supported tiers and pricing: https://cloud.google.com/sql/pricing"
3843
default = "db-f1-micro"
@@ -49,7 +54,7 @@ variable db_charset {
4954
}
5055

5156
variable db_collation {
52-
description = "The collation for the default database. Example for MySQL databases: 'utf8', and Postgres: 'en_US.UTF8'"
57+
description = "The collation for the default database. Example for MySQL databases: 'utf8_general_ci', and Postgres: 'en_US.UTF8'"
5358
default = ""
5459
}
5560

@@ -104,6 +109,11 @@ variable replication_type {
104109
default = "SYNCHRONOUS"
105110
}
106111

112+
variable database_flags {
113+
description = "List of Cloud SQL flags that are applied to the database server"
114+
default = []
115+
}
116+
107117
variable backup_configuration {
108118
description = "The backup_configuration settings subblock for the database setings"
109119
type = "map"

0 commit comments

Comments
 (0)