From 6dcbef56e65a3d1ab720cc128fec130a589658c7 Mon Sep 17 00:00:00 2001 From: Maximilian Schlenz Date: Mon, 7 Jul 2025 10:04:39 +0200 Subject: [PATCH 1/2] postgres tf module --- postgres/main.tf | 33 +++++++++++++++++++++++++ postgres/outputs.tf | 11 +++++++++ postgres/providers.tf | 9 +++++++ postgres/variables.tf | 56 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 postgres/main.tf create mode 100644 postgres/outputs.tf create mode 100644 postgres/providers.tf create mode 100644 postgres/variables.tf diff --git a/postgres/main.tf b/postgres/main.tf new file mode 100644 index 0000000..0507717 --- /dev/null +++ b/postgres/main.tf @@ -0,0 +1,33 @@ +locals { + user_count = length(var.users) + db_count = length(var.databases) +} + +resource "stackit_postgresflex_instance" "this" { + project_id = var.project_id + name = var.name + version = var.ver + flavor = var.flavor + storage = var.storage + replicas = var.replicas + acl = var.acl + backup_schedule = var.backup_schedule +} + +resource "stackit_postgresflex_user" "user" { + count = local.user_count + + project_id = var.project_id + instance_id = stackit_postgresflex_instance.this.instance_id + username = var.users[count.index].username + roles = var.users[count.index].roles +} + +resource "stackit_postgresflex_database" "db" { + count = local.db_count + + project_id = var.project_id + instance_id = stackit_postgresflex_instance.this.instance_id + name = var.databases[count.index].name + owner = var.databases[count.index].owner +} diff --git a/postgres/outputs.tf b/postgres/outputs.tf new file mode 100644 index 0000000..216884c --- /dev/null +++ b/postgres/outputs.tf @@ -0,0 +1,11 @@ +output "instance_id" { + value = stackit_postgresflex_instance.this.instance_id +} + +output "user_ids" { + value = stackit_postgresflex_user.user[*].user_id +} + +output "database_ids" { + value = stackit_postgresflex_database.db[*].database_id +} diff --git a/postgres/providers.tf b/postgres/providers.tf new file mode 100644 index 0000000..6e038c3 --- /dev/null +++ b/postgres/providers.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.9.0" + required_providers { + stackit = { + source = "stackitcloud/stackit" + version = "0.56.0" + } + } +} diff --git a/postgres/variables.tf b/postgres/variables.tf new file mode 100644 index 0000000..dbe1920 --- /dev/null +++ b/postgres/variables.tf @@ -0,0 +1,56 @@ +variable "project_id" { + type = string +} + +variable "name" { + type = string +} + +variable "ver" { + type = number +} + +variable "flavor" { + type = object({ + cpu = number, + ram = number + }) +} + +variable "storage" { + type = object({ + class = string, + size = number + }) +} + +variable "replicas" { + type = number +} + +variable "acl" { + type = list(string) +} + +variable "backup_schedule" { + type = string + +} + +variable "users" { + description = "List of users" + type = list(object({ + username = string + roles = set(string) + })) + default = [] +} + +variable "databases" { + description = "List of databases" + type = list(object({ + name = string + owner = string + })) + default = [] +} From 8ec98bf1da9c191410bf5a127d6c9d0d6f49b554 Mon Sep 17 00:00:00 2001 From: Maximilian Schlenz Date: Mon, 14 Jul 2025 13:46:06 +0200 Subject: [PATCH 2/2] fix for new structure with nested resources inside project --- postgres/main.tf | 2 +- postgres/variables.tf | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/postgres/main.tf b/postgres/main.tf index 0507717..75f628d 100644 --- a/postgres/main.tf +++ b/postgres/main.tf @@ -18,7 +18,7 @@ resource "stackit_postgresflex_user" "user" { count = local.user_count project_id = var.project_id - instance_id = stackit_postgresflex_instance.this.instance_id + instance_id = var.instance_id != "" ? var.instance_id : stackit_postgresflex_instance.this.instance_id username = var.users[count.index].username roles = var.users[count.index].roles } diff --git a/postgres/variables.tf b/postgres/variables.tf index dbe1920..061dd3f 100644 --- a/postgres/variables.tf +++ b/postgres/variables.tf @@ -4,6 +4,21 @@ variable "project_id" { variable "name" { type = string + + validation { + condition = length(regexall("^[a-z]([-a-z0-9]*[a-z0-9])?$", var.name)) > 0 + error_message = < 0 + ]) + error_message = <