Skip to content

Commit dd56df0

Browse files
committed
move intermediate vars to local
1 parent 3080b39 commit dd56df0

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
# Local .terraform directories
1010
**/.terraform/*
11-
examples/**/.terraform.lock.hcl
11+
12+
# Terraform lock file
13+
.terraform.lock.hcl
1214

1315
# IDE/Editor settings
1416
**/.idea

main.tf

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
roles_with_passwords = [for idx, role_data in var.roles : merge(role_data,
2+
_roles_with_passwords = [for idx, role_data in var.roles : merge(role_data,
33
{
44
role : merge(role_data["role"],
55
lookup(role_data["role"], "password", null) != null ? # Or if it's empty string?
@@ -13,21 +13,28 @@ locals {
1313
}
1414
)]
1515

16-
_database_grants = [for role in local.roles_with_passwords : role.database_grants if try(role.database_grants, null) != null]
17-
_default_privileges = flatten([for role in local.roles_with_passwords : role.default_privileges if try(role.default_privileges, null) != null])
18-
_schema_grants = [for role in local.roles_with_passwords : role.schema_grants if try(role.schema_grants, null) != null]
19-
_sequence_grants = [for role in local.roles_with_passwords : role.sequence_grants if try(role.sequence_grants, null) != null]
20-
_table_grants = [for role in local.roles_with_passwords : role.table_grants if try(role.table_grants, null) != null]
21-
}
16+
_database_grants = [for role in local._roles_with_passwords : role.database_grants if try(role.database_grants, null) != null]
17+
database_grants_map = { for grant in local._database_grants : format("%s-%s", grant.role, grant.database) => grant }
2218

23-
resource "postgresql_database" "logical_db" {
24-
for_each = { for database in var.databases : database.name => database }
25-
name = each.key
26-
connection_limit = each.value.connection_limit
19+
_default_privileges = flatten([for role in local._roles_with_passwords : role.default_privileges if try(role.default_privileges, null) != null])
20+
default_privileges_map = { for grant in local._default_privileges : format("%s-%s-%s-%s", grant.role, grant.database, grant.schema, grant.object_type) => grant }
21+
22+
_schema_grants = [for role in local._roles_with_passwords : role.schema_grants if try(role.schema_grants, null) != null]
23+
schema_grants_map = { for grant in local._schema_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
24+
25+
_sequence_grants = [for role in local._roles_with_passwords : role.sequence_grants if try(role.sequence_grants, null) != null]
26+
sequence_grants_map = { for grant in local._sequence_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
27+
28+
_table_grants = [for role in local._roles_with_passwords : role.table_grants if try(role.table_grants, null) != null]
29+
table_grants_map = { for grant in local._table_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
30+
31+
roles_map = { for role in local._roles_with_passwords : role.role.name => role }
32+
33+
databases_map = { for database in var.databases : database.name => database }
2734
}
2835

29-
# If no password passed in, then use this to generate one
3036
resource "random_password" "user_password" {
37+
# If no password passed in, then use this to generate one
3138
count = length(var.roles)
3239

3340
length = 33
@@ -37,10 +44,17 @@ resource "random_password" "user_password" {
3744
override_special = "!#$%^&*()<>-_"
3845
}
3946

47+
resource "postgresql_database" "logical_db" {
48+
for_each = local.databases_map
49+
50+
name = each.value.name
51+
connection_limit = each.value.connection_limit
52+
}
53+
4054
# In Postgres 15, now new users cannot create tables or write data to Postgres public schema by default. You have to grant create privilege to the new user manually.
4155
# https://www.postgresql.org/docs/current/ddl-priv.html#DDL-PRIV-CREATE
4256
resource "postgresql_role" "role" {
43-
for_each = { for role in local.roles_with_passwords : role.role.name => role }
57+
for_each = local.roles_map
4458

4559
name = each.value.role.name
4660
superuser = each.value.role.superuser
@@ -65,8 +79,7 @@ resource "postgresql_role" "role" {
6579
}
6680

6781
resource "postgresql_grant" "database_access" {
68-
69-
for_each = { for grant in local._database_grants : format("%s-%s", grant.role, grant.database) => grant }
82+
for_each = local.database_grants_map
7083

7184
role = each.value.role
7285
database = each.value.database
@@ -77,8 +90,7 @@ resource "postgresql_grant" "database_access" {
7790
}
7891

7992
resource "postgresql_grant" "schema_access" {
80-
81-
for_each = { for grant in local._schema_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
93+
for_each = local.schema_grants_map
8294

8395
role = each.value.role
8496
database = each.value.database
@@ -90,8 +102,7 @@ resource "postgresql_grant" "schema_access" {
90102
}
91103

92104
resource "postgresql_grant" "table_access" {
93-
94-
for_each = { for grant in local._table_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
105+
for_each = local.table_grants_map
95106

96107
role = each.value.role
97108
database = each.value.database
@@ -104,8 +115,7 @@ resource "postgresql_grant" "table_access" {
104115
}
105116

106117
resource "postgresql_grant" "sequence_access" {
107-
108-
for_each = { for grant in local._sequence_grants : format("%s-%s-%s", grant.role, grant.schema, grant.database) => grant }
118+
for_each = local.sequence_grants_map
109119

110120
role = each.value.role
111121
database = each.value.database
@@ -117,8 +127,7 @@ resource "postgresql_grant" "sequence_access" {
117127
}
118128

119129
resource "postgresql_default_privileges" "privileges" {
120-
121-
for_each = { for grant in local._default_privileges : format("%s-%s-%s-%s", grant.role, grant.database, grant.schema, grant.object_type) => grant }
130+
for_each = local.default_privileges_map
122131

123132
role = each.value.role
124133
database = each.value.database

0 commit comments

Comments
 (0)