mirror of
https://gitlab.sarex.io/infra/terraform-contour-mirror.git
synced 2026-08-06 18:51:36 +03:00
56 lines
1.6 KiB
HCL
56 lines
1.6 KiB
HCL
locals {
|
|
databases_map = { for db in var.databases : "${db.cluster_id}:${db.database.name}:${db.user.name}" => db }
|
|
}
|
|
|
|
resource "random_password" "user_password" {
|
|
for_each = local.databases_map
|
|
|
|
length = try(each.value.user.password_length, 32)
|
|
special = try(each.value.user.password_special, false)
|
|
upper = true
|
|
lower = true
|
|
numeric = true
|
|
}
|
|
|
|
data "yandex_mdb_postgresql_cluster" "existing" {
|
|
for_each = toset([for db in var.databases : db.cluster_id])
|
|
cluster_id = each.value
|
|
}
|
|
|
|
resource "yandex_mdb_postgresql_user" "this" {
|
|
for_each = local.databases_map
|
|
|
|
cluster_id = each.value.cluster_id
|
|
name = each.value.user.name
|
|
password = random_password.user_password[each.key].result
|
|
conn_limit = try(each.value.user.conn_limit, 10)
|
|
|
|
# permissions только для ДРУГИХ существующих БД
|
|
# на свою БД user получает права автоматически как owner
|
|
dynamic "permission" {
|
|
for_each = distinct(compact(try(each.value.user.permissions, [])))
|
|
content {
|
|
database_name = permission.value
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "yandex_mdb_postgresql_database" "this" {
|
|
for_each = local.databases_map
|
|
|
|
cluster_id = each.value.cluster_id
|
|
name = each.value.database.name
|
|
owner = yandex_mdb_postgresql_user.this[each.key].name
|
|
lc_collate = try(each.value.database.lc_collate, "en_US.UTF-8")
|
|
lc_type = try(each.value.database.lc_type, "en_US.UTF-8")
|
|
|
|
depends_on = [yandex_mdb_postgresql_user.this]
|
|
|
|
dynamic "extension" {
|
|
for_each = try(each.value.database.extensions, [])
|
|
content {
|
|
name = extension.value
|
|
}
|
|
}
|
|
}
|