Skip to content

Commit

Permalink
fix: add depends on replicas for user creation (#268)
Browse files Browse the repository at this point in the history
* fix:adds user timeouts as a module variables

* added depends_on for replicas and modified tier for pg examples

* lint fixes

* empty commit to restart the build

* adding replicas dependency for users for MySQL

* fixing lint errors

Co-authored-by: Awais Malik <[email protected]>
  • Loading branch information
g-awmalik and g-awmalik authored Jan 4, 2022
1 parent a53fdac commit d45df79
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/postgresql-public-iam/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module "postgresql-db" {
project_id = var.project_id
zone = "us-central1-c"
region = "us-central1"
tier = "db-f1-micro"
tier = "db-custom-2-13312"

deletion_protection = false

Expand Down
2 changes: 1 addition & 1 deletion examples/postgresql-public/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module "postgresql-db" {
project_id = var.project_id
zone = "us-central1-c"
region = "us-central1"
tier = "db-f1-micro"
tier = "db-custom-2-13312"

deletion_protection = false

Expand Down
38 changes: 23 additions & 15 deletions modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,32 @@ resource "random_id" "additional_passwords" {
}

resource "google_sql_user" "default" {
count = var.enable_default_user ? 1 : 0
name = var.user_name
project = var.project_id
instance = google_sql_database_instance.default.name
host = var.user_host
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
count = var.enable_default_user ? 1 : 0
name = var.user_name
project = var.project_id
instance = google_sql_database_instance.default.name
host = var.user_host
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}

resource "google_sql_user" "additional_users" {
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
type = lookup(each.value, "type", "BUILT_IN")
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
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
type = lookup(each.value, "type", "BUILT_IN")
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}

resource "null_resource" "module_depends_on" {
Expand Down
32 changes: 20 additions & 12 deletions modules/postgresql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,29 @@ resource "random_id" "additional_passwords" {
}

resource "google_sql_user" "default" {
count = var.enable_default_user ? 1 : 0
name = var.user_name
project = var.project_id
instance = google_sql_database_instance.default.name
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
count = var.enable_default_user ? 1 : 0
name = var.user_name
project = var.project_id
instance = google_sql_database_instance.default.name
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}

resource "google_sql_user" "additional_users" {
for_each = local.users
project = var.project_id
name = each.value.name
password = coalesce(each.value["password"], random_id.additional_passwords[each.value.name].hex)
instance = google_sql_database_instance.default.name
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
for_each = local.users
project = var.project_id
name = each.value.name
password = coalesce(each.value["password"], random_id.additional_passwords[each.value.name].hex)
instance = google_sql_database_instance.default.name
depends_on = [
null_resource.module_depends_on,
google_sql_database_instance.default,
google_sql_database_instance.replicas,
]
}

resource "google_project_iam_member" "iam_binding" {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/postgresql-public-iam/controls/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
basename = attribute('name')
db_version = "POSTGRES_9_6"
region = "us-central1"
tier = "db-f1-micro"
tier = "db-custom-2-13312"
public_ip_address = attribute('public_ip_address')

activation_policy = "ALWAYS"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/postgresql-public/controls/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
basename = attribute('name')
db_version = "POSTGRES_9_6"
region = "us-central1"
tier = "db-f1-micro"
tier = "db-custom-2-13312"
public_ip_address = attribute('public_ip_address')

activation_policy = "ALWAYS"
Expand Down

0 comments on commit d45df79

Please sign in to comment.