Skip to content

Commit 2bbedef

Browse files
committed
feat: define module (#1)
1 parent 5efb9d1 commit 2bbedef

File tree

10 files changed

+258
-1
lines changed

10 files changed

+258
-1
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: daily

.github/workflows/lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint
2+
3+
on: [push, pull_request]
4+
jobs:
5+
lint:
6+
runs-on: ${{ matrix.os }}
7+
strategy:
8+
matrix:
9+
os: [ubuntu-latest]
10+
terraform-versions: [0.15.x]
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2.3.4
14+
15+
- name: Setup Terraform
16+
uses: hashicorp/setup-terraform@v1.3.2
17+
with:
18+
terraform_version: ${{ matrix['terraform-versions'] }}
19+
terraform_wrapper: false
20+
21+
- name: Terraform fmt
22+
id: fmt
23+
run: terraform fmt -check

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Terraform
2+
*.auto.tfvars*
3+
.terraform/
4+
*.tfstate*
5+
terraform.tfstate.d
6+
.terraform.lock.hcl
7+
8+
# Terratest
9+
.test-data/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ In particular:
1717

1818
```hcl
1919
module "foo" {
20-
source = "git@github.com:edgelaboratories/terraform-modules//postgresql/db?ref=v0.1.0"
20+
source = "git@github.com:edgelaboratories/terraform-postgresql-db?ref=v0.1.0"
2121
2222
database = "foo"
2323
owner = "admin"

database.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "postgresql_role" "owner" {
2+
name = var.owner
3+
login = true
4+
password = var.owner_password
5+
roles = var.roles
6+
7+
connection_limit = var.connection_limit
8+
9+
# We don't want to reassign objects especially because it's executed
10+
# in the current database (so 'postgres') so it makes no sense.
11+
skip_reassign_owned = true
12+
}
13+
14+
resource "postgresql_database" "this" {
15+
name = var.database
16+
owner = postgresql_role.owner.name
17+
18+
# CREATE DATABASE uses template1 by default, but the Terraform provider uses template0.
19+
# In RDS, the owner of the schema 'public' is 'postgres' in template1, so we can manage owner of tables
20+
# but 'rdsadmin' in template0 so we don't have all the permissions.
21+
template = "template1"
22+
23+
lc_collate = var.lc_collate
24+
lc_ctype = coalesce(var.lc_ctype, var.lc_collate)
25+
}
26+
27+
resource "postgresql_extension" "this" {
28+
count = length(var.extensions)
29+
30+
name = element(var.extensions, count.index)
31+
database = postgresql_database.this.name
32+
33+
# On destroy, force the deletion of the extension even if things not managed by Terraform depend on it.
34+
# This can happen if a database and/or a schema has been created using
35+
# Terraform, but tables depending on features of an extension were created
36+
# outside of Terraform.
37+
# In that case, it's not possible to drop the extension on the first try.
38+
drop_cascade = true
39+
}
40+
41+
## Schemas
42+
resource "postgresql_schema" "this" {
43+
count = length(var.schemas)
44+
45+
name = element(var.schemas, count.index)
46+
owner = var.schemas[count.index] == "public" ? null : postgresql_role.owner.name
47+
database = postgresql_database.this.name
48+
if_not_exists = true
49+
drop_cascade = true
50+
}

outputs.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
output "database" {
2+
value = postgresql_database.this.name
3+
}
4+
5+
output "owner" {
6+
value = postgresql_role.owner.name
7+
}
8+
9+
output "role_ro" {
10+
value = postgresql_role.read_only.name
11+
}
12+
13+
output "role_rw" {
14+
value = postgresql_role.read_write.name
15+
}

provider.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
postgresql = {
4+
source = "cyrilgdn/postgresql"
5+
}
6+
}
7+
}

read-only.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
resource "postgresql_role" "read_only" {
2+
name = "${var.database}_ro"
3+
skip_reassign_owned = true
4+
}
5+
6+
## Tables
7+
resource "postgresql_grant" "read_only_tables" {
8+
count = length(postgresql_schema.this)
9+
10+
role = postgresql_role.read_only.name
11+
database = postgresql_database.this.name
12+
schema = postgresql_schema.this[count.index].name
13+
14+
object_type = "table"
15+
privileges = ["SELECT"]
16+
}
17+
18+
resource "postgresql_default_privileges" "read_only_tables" {
19+
count = length(postgresql_schema.this)
20+
21+
role = postgresql_role.read_only.name
22+
database = postgresql_database.this.name
23+
schema = postgresql_schema.this[count.index].name
24+
25+
owner = postgresql_role.owner.name
26+
object_type = "table"
27+
privileges = ["SELECT"]
28+
}
29+
30+
## Sequences
31+
resource "postgresql_grant" "read_only_sequences" {
32+
count = length(postgresql_schema.this)
33+
34+
role = postgresql_role.read_only.name
35+
database = postgresql_database.this.name
36+
schema = postgresql_schema.this[count.index].name
37+
38+
object_type = "sequence"
39+
privileges = ["SELECT"]
40+
}
41+
42+
resource "postgresql_default_privileges" "read_only_sequences" {
43+
count = length(postgresql_schema.this)
44+
45+
role = postgresql_role.read_only.name
46+
database = postgresql_database.this.name
47+
schema = postgresql_schema.this[count.index].name
48+
49+
owner = postgresql_role.owner.name
50+
object_type = "sequence"
51+
privileges = ["SELECT"]
52+
}

read-write.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
resource "postgresql_role" "read_write" {
2+
name = "${var.database}_rw"
3+
skip_reassign_owned = true
4+
}
5+
6+
## Tables
7+
resource "postgresql_grant" "read_write_tables" {
8+
count = length(postgresql_schema.this)
9+
10+
role = postgresql_role.read_write.name
11+
database = postgresql_database.this.name
12+
schema = postgresql_schema.this[count.index].name
13+
14+
object_type = "table"
15+
privileges = ["SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE"]
16+
}
17+
18+
resource "postgresql_default_privileges" "read_write_tables" {
19+
count = length(postgresql_schema.this)
20+
21+
role = postgresql_role.read_write.name
22+
database = postgresql_database.this.name
23+
schema = postgresql_schema.this[count.index].name
24+
25+
owner = postgresql_role.owner.name
26+
object_type = "table"
27+
privileges = ["SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE"]
28+
}
29+
30+
## Sequences
31+
resource "postgresql_grant" "read_write_sequences" {
32+
count = length(postgresql_schema.this)
33+
34+
role = postgresql_role.read_write.name
35+
database = postgresql_database.this.name
36+
schema = postgresql_schema.this[count.index].name
37+
38+
object_type = "sequence"
39+
privileges = ["SELECT", "UPDATE"]
40+
}
41+
42+
resource "postgresql_default_privileges" "read_write_sequences" {
43+
count = length(postgresql_schema.this)
44+
45+
role = postgresql_role.read_write.name
46+
database = postgresql_database.this.name
47+
schema = postgresql_schema.this[count.index].name
48+
49+
owner = postgresql_role.owner.name
50+
object_type = "sequence"
51+
privileges = ["SELECT", "UPDATE"]
52+
}

variables.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "database" {
2+
description = "The name of the database"
3+
}
4+
5+
variable "schemas" {
6+
description = "The schemas to create"
7+
default = ["public"]
8+
}
9+
10+
variable "owner" {
11+
description = "The name of the owner of the database"
12+
}
13+
14+
variable "owner_password" {
15+
description = "The password for the owner of the database"
16+
}
17+
18+
variable "roles" {
19+
type = list(string)
20+
description = "A list of roles to grant to the owner of the database"
21+
default = []
22+
}
23+
24+
variable "extensions" {
25+
type = list(string)
26+
description = "A list of PostgreSQL extensions to install in the database"
27+
default = []
28+
}
29+
30+
variable "connection_limit" {
31+
default = -1
32+
description = "Maximum number of connections for the owner role"
33+
}
34+
35+
variable "lc_collate" {
36+
default = "en_US.UTF-8"
37+
description = "Controls the sort order"
38+
}
39+
40+
variable "lc_ctype" {
41+
default = null
42+
type = string
43+
}

0 commit comments

Comments
 (0)