Skip to content

Commit fcaca3f

Browse files
authored
Merge pull request #11 from particuleio/feat/add-managed-dbs-support
feat/add managed dbs support
2 parents 3a1bcba + 768f440 commit fcaca3f

File tree

7 files changed

+101
-8
lines changed

7 files changed

+101
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
Terraform module to create Databases using Scaleway's [Managed Databases][scw-db].
66

77
Create and configure the following:
8-
- Database Instance
8+
- Managed RDB Instances
99
- Database Users
1010
- Database ACLs
11+
- Managed Databases
1112

1213
[scw-db]: https://www.scaleway.com/en/database/
1314

@@ -41,6 +42,7 @@ Multiple examples are available in the [`./examples`](./examples) directory.
4142
- A [simple database setup](./examples/simple/)
4243
- [Multiple database creation with users](./examples/users/)
4344
- [Database creation with ACL support](./examples/acls/)
45+
- [RDB instance with multiple databases](./examples/databases/)
4446

4547
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4648
## Requirements

dbs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "scaleway_rdb_database" "this" {
2+
for_each = local.dbs_by_database
3+
4+
instance_id = scaleway_rdb_instance.this[each.value.database].id
5+
name = each.value.db
6+
}
7+

examples/databases/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Databases Example
2+
3+
Managed Database creation with database support.
4+
5+
```hcl
6+
module "rdb" {
7+
source = "../../"
8+
9+
databases = {
10+
main = {
11+
name = "database-with-multiple-dbs"
12+
node_type = "DB-DEV-S"
13+
engine = "PostgreSQL-11"
14+
dbs = [
15+
"default",
16+
"admin",
17+
"internal",
18+
]
19+
}
20+
}
21+
}
22+
```
23+
24+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
25+
## Requirements
26+
27+
No requirements.
28+
29+
## Providers
30+
31+
No providers.
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| <a name="module_rdb"></a> [rdb](#module\_rdb) | ../../ | n/a |
38+
39+
## Resources
40+
41+
No resources.
42+
43+
## Inputs
44+
45+
No inputs.
46+
47+
## Outputs
48+
49+
| Name | Description |
50+
|------|-------------|
51+
| <a name="output_rdb"></a> [rdb](#output\_rdb) | n/a |
52+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/databases/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module "rdb" {
2+
source = "../../"
3+
4+
databases = {
5+
main = {
6+
name = "database-with-multiple-dbs"
7+
node_type = "DB-DEV-S"
8+
engine = "PostgreSQL-11"
9+
dbs = [
10+
"default",
11+
"admin",
12+
"internal",
13+
]
14+
}
15+
}
16+
}
17+
18+
output "rdb" {
19+
value = module.rdb.this
20+
sensitive = true
21+
}

locals.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ locals {
77
}
88
]
99
])
10+
db_configs = flatten([
11+
for database, config in local.databases : [
12+
for db in config.dbs : {
13+
database = database
14+
db = db
15+
}
16+
]
17+
])
1018
acl_configs = flatten([
1119
for database, config in local.databases : [
1220
for acl in config.acls : {
@@ -23,12 +31,17 @@ locals {
2331
for config in local.user_configs :
2432
"${config.database}_${config.user.username}" => config
2533
}
34+
dbs_by_database = {
35+
for config in local.db_configs :
36+
"${config.database}_${config.db}" => config
37+
}
2638
default_database = {
2739
name = "default"
2840
node_type = "DB-DEV-S"
2941
users = []
3042
settings = {}
3143
acls = []
44+
dbs = []
3245
}
3346
databases = {
3447
for database_name, config in var.databases :

main.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
resource "scaleway_rdb_database" "this" {
2-
for_each = local.databases
3-
instance_id = scaleway_rdb_instance.this[each.key].id
4-
name = each.key
5-
}
6-
71
resource "scaleway_rdb_instance" "this" {
82
for_each = local.databases
93

outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ output "this" {
22
value = {
33
for name in keys(var.databases) : name => {
44
"instance" = scaleway_rdb_instance.this[name],
5-
"database" = scaleway_rdb_database.this[name],
65
"acls" = lookup(scaleway_rdb_acl.this, name, []),
76
"users" = [
87
for identifier, config in local.user_by_database : {
@@ -12,6 +11,11 @@ output "this" {
1211
"identifier" : identifier
1312
} if config.database == name
1413
],
14+
"dbs" = [
15+
for identifier, config in local.dbs_by_database :
16+
scaleway_rdb_database.this[identifier]
17+
if config.database == name
18+
]
1519
}
1620
}
1721
description = "A map of the scaleway_rdb_database (including their users) and scaleway_rdb_instance resources grouped by databases definitions"

0 commit comments

Comments
 (0)