feat: support multiple environment with workspace

This commit is contained in:
RouxAntoine 2025-03-06 07:52:32 +01:00
parent 2a54e1d4ab
commit 2a96d470e0
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
2 changed files with 55 additions and 15 deletions

42
main.tf
View File

@ -12,6 +12,7 @@ terraform {
endpoints = { endpoints = {
s3 = "http://s3.localdomain" s3 = "http://s3.localdomain"
} }
//@formatter:off
key = "postgres.tfstate" key = "postgres.tfstate"
bucket = "terraform" bucket = "terraform"
region = "FR" region = "FR"
@ -22,15 +23,34 @@ terraform {
shared_credentials_files = ["~/.aws/credentials"] shared_credentials_files = ["~/.aws/credentials"]
profile = "minio" profile = "minio"
use_path_style = true use_path_style = true
//@formatter:on
}
}
locals {
# because merge function didn't support deep merging
# order matter because otherwise typing contained in var.connections is set as value in the result local.connection
connection = {for key, config in var.connections : key => merge(config, local.private_connection[key])}
private_connection = {
"default" = {
db = "postgres"
host = "database.localdomain"
port = 5432
},
"prod" = {
db = "postgres"
host = "database-trusted-primate.localdomain"
port = 5432
}
} }
} }
provider "postgresql" { provider "postgresql" {
host = "database.localdomain" host = local.connection[terraform.workspace].host
port = 5432 port = local.connection[terraform.workspace].port
database = var.postgres.db database = local.connection[terraform.workspace].db
username = var.postgres.user username = local.connection[terraform.workspace].username
password = var.postgres.secret password = local.connection[terraform.workspace].password
sslmode = "disable" sslmode = "disable"
connect_timeout = 15 connect_timeout = 15
} }
@ -55,7 +75,7 @@ module "gitea" {
username = "gitea" username = "gitea"
collate = "fr_FR.UTF-8" collate = "fr_FR.UTF-8"
ctype = "fr_FR.UTF-8" ctype = "fr_FR.UTF-8"
privileges = ["CREATE", "CONNECT", "TEMPORARY"] privileges = ["CREATE", "CONNECT", "TEMPORARY"]
} }
module "keycloak" { module "keycloak" {
@ -64,7 +84,7 @@ module "keycloak" {
username = "keycloak" username = "keycloak"
collate = "fr_FR.utf8" collate = "fr_FR.utf8"
ctype = "fr_FR.utf8" ctype = "fr_FR.utf8"
privileges = ["CREATE", "CONNECT", "TEMPORARY"] privileges = ["CREATE", "CONNECT", "TEMPORARY"]
} }
module "nextcloud" { module "nextcloud" {
@ -73,13 +93,13 @@ module "nextcloud" {
username = "nextcloud" username = "nextcloud"
collate = "fr_FR.utf8" collate = "fr_FR.utf8"
ctype = "fr_FR.utf8" ctype = "fr_FR.utf8"
privileges = ["CREATE", "CONNECT", "TEMPORARY"] privileges = ["CREATE", "CONNECT", "TEMPORARY"]
} }
module "favorite_link" { module "favorite_link" {
source = "./generic-database" source = "./generic-database"
database_name = "favorite-link" database_name = "favorite-link"
username = "favorite-link" username = "favorite-link"
} }
output "ampere_account" { output "ampere_account" {
@ -103,6 +123,6 @@ output "nextcloud_account" {
sensitive = true sensitive = true
} }
output "favorite_link_account" { output "favorite_link_account" {
value = module.favorite_link.account value = module.favorite_link.account
sensitive = true sensitive = true
} }

View File

@ -1,9 +1,29 @@
variable "connections" {
description = "postgres configuration map by environment"
sensitive = true
type = object({
default : object({
db : optional(string)
host : optional(string)
port : optional(number)
username : optional(string, "postgres")
password : string
})
prod : object({
db : optional(string)
host : optional(string)
port : optional(number)
username : optional(string, "postgres")
password : string
})
})
}
variable "postgres" { variable "postgres" {
description = "postgres configuration" description = "postgres configuration"
sensitive = true type = object({
type = object({ user = optional(string, "postgres")
user = optional(string, "postgres")
secret = string secret = string
db = optional(string, "postgres") db = optional(string, "postgres")
}) })
} }