1
1
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,
3
3
{
4
4
role : merge (role_data[" role" ],
5
5
lookup (role_data[" role" ], " password" , null ) != null ? # Or if it's empty string?
@@ -13,21 +13,28 @@ locals {
13
13
}
14
14
)]
15
15
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 }
22
18
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 }
27
34
}
28
35
29
- # If no password passed in, then use this to generate one
30
36
resource "random_password" "user_password" {
37
+ # If no password passed in, then use this to generate one
31
38
count = length (var. roles )
32
39
33
40
length = 33
@@ -37,10 +44,17 @@ resource "random_password" "user_password" {
37
44
override_special = " !#$%^&*()<>-_"
38
45
}
39
46
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
+
40
54
# 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.
41
55
# https://www.postgresql.org/docs/current/ddl-priv.html#DDL-PRIV-CREATE
42
56
resource "postgresql_role" "role" {
43
- for_each = { for role in local . roles_with_passwords : role . role . name => role }
57
+ for_each = local. roles_map
44
58
45
59
name = each. value . role . name
46
60
superuser = each. value . role . superuser
@@ -65,8 +79,7 @@ resource "postgresql_role" "role" {
65
79
}
66
80
67
81
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
70
83
71
84
role = each. value . role
72
85
database = each. value . database
@@ -77,8 +90,7 @@ resource "postgresql_grant" "database_access" {
77
90
}
78
91
79
92
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
82
94
83
95
role = each. value . role
84
96
database = each. value . database
@@ -90,8 +102,7 @@ resource "postgresql_grant" "schema_access" {
90
102
}
91
103
92
104
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
95
106
96
107
role = each. value . role
97
108
database = each. value . database
@@ -104,8 +115,7 @@ resource "postgresql_grant" "table_access" {
104
115
}
105
116
106
117
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
109
119
110
120
role = each. value . role
111
121
database = each. value . database
@@ -117,8 +127,7 @@ resource "postgresql_grant" "sequence_access" {
117
127
}
118
128
119
129
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
122
131
123
132
role = each. value . role
124
133
database = each. value . database
0 commit comments