Skip to content

Commit 663a911

Browse files
committed
update with tests for the parts of terraform logic we handle
1 parent 17f290e commit 663a911

File tree

11 files changed

+148
-174
lines changed

11 files changed

+148
-174
lines changed

.github/workflows/notion-sync.yaml

Lines changed: 0 additions & 30 deletions
This file was deleted.

.github/workflows/test.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: TF Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
permissions:
10+
actions: read
11+
checks: write
12+
contents: read
13+
id-token: write
14+
pull-requests: read
15+
16+
jobs:
17+
tf-test:
18+
name: ${{ matrix.tf }} Test
19+
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
tf: [tofu, terraform]
23+
steps:
24+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
25+
26+
- name: Aqua Cache
27+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
28+
if: ${{ !github.event.act }} # Don't enable the cache step if we're using act for testing
29+
with:
30+
path: ~/.local/share/aquaproj-aqua
31+
key: v1-aqua-installer-${{runner.os}}-${{runner.arch}}-${{hashFiles('aqua.yaml')}}
32+
restore-keys: |
33+
v1-aqua-installer-${{runner.os}}-${{runner.arch}}-
34+
35+
- name: Install Aqua
36+
uses: aquaproj/aqua-installer@5e54e5cee8a95ee2ce7c04cb993da6dfad13e59c # v3.2.1
37+
with:
38+
aqua_version: v2.48.1
39+
40+
- name: Aqua Install
41+
shell: bash
42+
run: aqua install --tags ${{ matrix.tf }}
43+
44+
- run: ${{ matrix.tf }} init
45+
- run: ${{ matrix.tf }} test

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module "logical_dbs" {
4747
name = "system_user"
4848
login = true
4949
superuser = false
50-
password = "insecure-pass-for-demo-app"
50+
password = "insecure-pass-for-readme-app"
5151
}
5252
5353
table_grants = {
@@ -80,7 +80,7 @@ module "logical_dbs" {
8080
role = {
8181
name = "readonly_user"
8282
login = true
83-
password = "insecure-pass-for-demo-readonly"
83+
password = "insecure-pass-for-readme-readonly"
8484
superuser = false
8585
}
8686

examples/complete/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ module "app_dbs" {
44
source = "../../"
55

66
databases = var.databases
7-
8-
roles = var.roles
7+
roles = var.roles
98
}

examples/complete/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
output "databases" {
2+
value = module.app_dbs.databases
3+
}
4+
15
output "database_access" {
26
value = module.app_dbs.database_access
37
}

examples/complete/validation_script.py

Lines changed: 0 additions & 129 deletions
This file was deleted.

examples/complete/variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ variable "databases" {
4040
name = string
4141
connection_limit = number
4242
}))
43+
default = []
4344
}
4445

4546

@@ -104,5 +105,6 @@ variable "roles" {
104105
privileges = list(string)
105106
}))
106107
}))
108+
default = []
107109
description = "List of static postgres roles to create and related permissions. These are for applications that use static credentials and don't use IAM DB Auth. See defaults: https://registry.terraform.io/providers/cyrilgdn/postgresql/latest/docs/resources/postgresql_role"
108110
}

main.tf

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
resource "postgresql_database" "logical_db" {
2-
for_each = { for database in var.databases : database.name => database }
3-
name = each.key
4-
connection_limit = each.value.connection_limit
5-
}
6-
7-
81
locals {
92
roles_with_passwords = [for idx, role_data in var.roles : merge(role_data,
103
{
@@ -27,6 +20,12 @@ locals {
2720
_table_grants = [for role in local.roles_with_passwords : role.table_grants if try(role.table_grants, null) != null]
2821
}
2922

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
27+
}
28+
3029
# If no password passed in, then use this to generate one
3130
resource "random_password" "user_password" {
3231
count = length(var.roles)
@@ -74,7 +73,7 @@ resource "postgresql_grant" "database_access" {
7473
object_type = each.value.object_type
7574
privileges = each.value.privileges
7675

77-
depends_on = [postgresql_database.logical_db]
76+
depends_on = [postgresql_database.logical_db, postgresql_role.role]
7877
}
7978

8079
resource "postgresql_grant" "schema_access" {
@@ -87,7 +86,7 @@ resource "postgresql_grant" "schema_access" {
8786
object_type = each.value.object_type
8887
privileges = each.value.privileges
8988

90-
depends_on = [postgresql_database.logical_db]
89+
depends_on = [postgresql_database.logical_db, postgresql_role.role]
9190
}
9291

9392
resource "postgresql_grant" "table_access" {
@@ -114,7 +113,7 @@ resource "postgresql_grant" "sequence_access" {
114113
object_type = each.value.object_type
115114
privileges = each.value.privileges
116115

117-
depends_on = [postgresql_database.logical_db]
116+
depends_on = [postgresql_database.logical_db, postgresql_role.role]
118117
}
119118

120119
resource "postgresql_default_privileges" "privileges" {
@@ -128,5 +127,5 @@ resource "postgresql_default_privileges" "privileges" {
128127
object_type = each.value.object_type
129128
privileges = each.value.privileges
130129

131-
depends_on = [postgresql_database.logical_db]
130+
depends_on = [postgresql_database.logical_db, postgresql_role.role]
132131
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
output "databases" {
2+
value = postgresql_database.logical_db
3+
}
4+
15
output "database_access" {
26
value = postgresql_grant.database_access
37
}

0 commit comments

Comments
 (0)