From cebedafcdc56ac811ce4169bb19b639025785d52 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Sat, 30 Dec 2023 17:06:56 -0500 Subject: [PATCH] better db name generation --- terraform/clouds/aws/locals.tf | 1 + terraform/clouds/aws/postgres.tf | 2 +- terraform/clouds/aws/variables.tf | 5 +++++ terraform/clouds/azure/locals.tf | 1 + terraform/clouds/azure/postgres.tf | 2 +- terraform/clouds/azure/variables.tf | 10 +++++----- terraform/clouds/gcp/locals.tf | 1 + terraform/clouds/gcp/postgres.tf | 2 +- terraform/clouds/gcp/variables.tf | 2 +- test/main.tf | 1 + 10 files changed, 18 insertions(+), 9 deletions(-) diff --git a/terraform/clouds/aws/locals.tf b/terraform/clouds/aws/locals.tf index dc0425f..139a7b8 100644 --- a/terraform/clouds/aws/locals.tf +++ b/terraform/clouds/aws/locals.tf @@ -1,3 +1,4 @@ locals { + db_name = var.db_name == "" ? "${var.cluster_name}-plural-db" : var.db_name db_url = format("postgresql://console:%s@%s:5432/console", random_password.password.result, try(module.db[0].db_instance_address, "")) } \ No newline at end of file diff --git a/terraform/clouds/aws/postgres.tf b/terraform/clouds/aws/postgres.tf index f5806b0..6c3c2db 100644 --- a/terraform/clouds/aws/postgres.tf +++ b/terraform/clouds/aws/postgres.tf @@ -10,7 +10,7 @@ module "db" { count = var.create_db ? 1 : 0 source = "terraform-aws-modules/rds/aws" - identifier = "plural" + identifier = local.db_name engine = "postgres" engine_version = "14" diff --git a/terraform/clouds/aws/variables.tf b/terraform/clouds/aws/variables.tf index ae098ae..856be4b 100644 --- a/terraform/clouds/aws/variables.tf +++ b/terraform/clouds/aws/variables.tf @@ -3,6 +3,11 @@ variable "cluster_name" { default = "plural" } +variable "db_name" { + type = string + default = "" +} + variable "create_db" { type = bool default = true diff --git a/terraform/clouds/azure/locals.tf b/terraform/clouds/azure/locals.tf index 657bd3d..f531257 100644 --- a/terraform/clouds/azure/locals.tf +++ b/terraform/clouds/azure/locals.tf @@ -1,5 +1,6 @@ locals { + db_name = var.db_name == "" ? "${var.cluster_name}-plural-db" : var.db_name resource_group = { name = var.create_resource_group ? azurerm_resource_group.main[0].name : var.resource_group_name location = var.location diff --git a/terraform/clouds/azure/postgres.tf b/terraform/clouds/azure/postgres.tf index ad3bb36..2df52e1 100644 --- a/terraform/clouds/azure/postgres.tf +++ b/terraform/clouds/azure/postgres.tf @@ -13,7 +13,7 @@ module "postgresql" { resource_group_name = local.resource_group.name location = local.resource_group.location - server_name = var.postgres_name + server_name = local.db_name sku_name = var.db_sku storage_mb = 5120 auto_grow_enabled = true diff --git a/terraform/clouds/azure/variables.tf b/terraform/clouds/azure/variables.tf index 08d0cef..2f107fd 100644 --- a/terraform/clouds/azure/variables.tf +++ b/terraform/clouds/azure/variables.tf @@ -3,6 +3,11 @@ variable "cluster_name" { default = "plural" } +variable "db_name" { + type = string + default = "" +} + variable "create_db" { type = bool default = true @@ -43,11 +48,6 @@ variable "subnet_cidrs" { default = ["10.52.0.0/20"] } -variable "postgres_name" { - type = string - default = "plural" -} - variable "db_sku" { default = "GP_Gen5_2" } diff --git a/terraform/clouds/gcp/locals.tf b/terraform/clouds/gcp/locals.tf index f3abce3..f180d84 100644 --- a/terraform/clouds/gcp/locals.tf +++ b/terraform/clouds/gcp/locals.tf @@ -1,4 +1,5 @@ locals { + db_name = var.db_name == "" ? "${var.cluster_name}-plural-db" : var.db_name range_name = var.allocated_range_name == "" ? "${var.cluster_name}-managed-services" : var.allocated_range_name db_url = format("postgresql://console:%s@%s:5432/console", random_password.password.result, try(module.pg[0].private_ip_address, "")) # db_created = var.create_db ? module.pg.0.google_sql_user.default[0] : {} diff --git a/terraform/clouds/gcp/postgres.tf b/terraform/clouds/gcp/postgres.tf index 7b4c289..e558541 100644 --- a/terraform/clouds/gcp/postgres.tf +++ b/terraform/clouds/gcp/postgres.tf @@ -11,7 +11,7 @@ module "pg" { source = "GoogleCloudPlatform/sql-db/google//modules/postgresql" version = "18.1.0" - name = var.db_name + name = local.db_name random_instance_name = false project_id = var.project_id database_version = "POSTGRES_14" diff --git a/terraform/clouds/gcp/variables.tf b/terraform/clouds/gcp/variables.tf index d505d99..349e33f 100644 --- a/terraform/clouds/gcp/variables.tf +++ b/terraform/clouds/gcp/variables.tf @@ -99,5 +99,5 @@ variable "ip_range_services_name" { variable "db_name" { type = string - default = "plural" + default = "" } diff --git a/test/main.tf b/test/main.tf index 92291c0..7d12171 100644 --- a/test/main.tf +++ b/test/main.tf @@ -4,6 +4,7 @@ module "gcp" { cluster_name = "bootstrap-test" network = "plrl-network" subnetwork = "plrl-subnetwork" + db_name = "plural" allocated_range_name = "google-managed-services" deletion_protection = false }