Merge pull request 'feature/mapping-psql-dbname' (#10) from feature/mapping-psql-dbname into main
Reviewed-on: #10 Reviewed-by: Stanislav_Kopp <stanislav.kopp@mail.schwarz>
This commit is contained in:
commit
0273ecc4a1
2 changed files with 32 additions and 8 deletions
|
|
@ -3,6 +3,14 @@ output "postgres_instance_id" {
|
||||||
value = stackit_postgresflex_instance.this.instance_id
|
value = stackit_postgresflex_instance.this.instance_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
# Build a map: username => db_name
|
||||||
|
user_to_db = {
|
||||||
|
for db in var.postgres_databases :
|
||||||
|
db.user_name => db.db_name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Postgres Credential Output
|
# Postgres Credential Output
|
||||||
output "postgres_credentials" {
|
output "postgres_credentials" {
|
||||||
value = {
|
value = {
|
||||||
|
|
@ -12,7 +20,7 @@ output "postgres_credentials" {
|
||||||
username = u.username
|
username = u.username
|
||||||
password = u.password
|
password = u.password
|
||||||
port = u.port
|
port = u.port
|
||||||
db_name = stackit_postgresflex_database.this[u.username].name
|
db_name = stackit_postgresflex_database.this[local.user_to_db[u.username]].name
|
||||||
uri = u.uri
|
uri = u.uri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,26 @@ variable "postgres_instance_region" {
|
||||||
|
|
||||||
# Postgres User and DB Configs
|
# Postgres User and DB Configs
|
||||||
variable "postgres_databases" {
|
variable "postgres_databases" {
|
||||||
description = "list of users and databases"
|
description = "list of users and databases"
|
||||||
type = list(object({
|
type = list(object({
|
||||||
db_name = string # db name inside the instance
|
db_name = string # db name inside the instance
|
||||||
user_name = string # username and owner for postgres db
|
user_name = string # username and owner for postgres db
|
||||||
user_roles = list(string) # List of database access levels for the user. Supported values are: login, createdb.
|
user_roles = list(string) # List of database access levels for the user. Supported values are: login, createdb.
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
# Validation: each db_name must be unique
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
validation {
|
||||||
|
condition = length(distinct([for db in var.postgres_databases : db.db_name])) == length(var.postgres_databases)
|
||||||
|
error_message = "Each db_name must be unique."
|
||||||
|
}
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
# Validation: each user_name must be unique
|
||||||
|
# -----------------------------------------------------------------
|
||||||
|
validation {
|
||||||
|
condition = length(distinct([for db in var.postgres_databases : db.user_name])) == length(var.postgres_databases)
|
||||||
|
error_message = "Each user_name must be unique."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue