Skip to content

Commit 3d574f8

Browse files
committed
feat(mongodb): implement user resource with role management
1 parent ae2f384 commit 3d574f8

File tree

9 files changed

+12635
-0
lines changed

9 files changed

+12635
-0
lines changed

docs/resources/mongodb_user.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
subcategory: "MongoDB®"
3+
page_title: "Scaleway: scaleway_mongodb_user"
4+
---
5+
6+
# Resource: scaleway_mongodb_user
7+
8+
Creates and manages Scaleway MongoDB® users.
9+
For more information refer to the [product documentation](https://www.scaleway.com/en/docs/managed-mongodb-databases/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_mongodb_instance" "main" {
17+
name = "test-mongodb-user"
18+
version = "7.0.12"
19+
node_type = "MGDB-PLAY2-NANO"
20+
node_number = 1
21+
user_name = "initial_user"
22+
password = "initial_password123"
23+
volume_size_in_gb = 5
24+
}
25+
26+
resource "scaleway_mongodb_user" "main" {
27+
instance_id = scaleway_mongodb_instance.main.id
28+
name = "my_user"
29+
password = "my_password123"
30+
31+
roles {
32+
role = "read_write"
33+
database_name = "my_database"
34+
}
35+
}
36+
```
37+
38+
### With Multiple Users
39+
40+
```terraform
41+
resource "scaleway_mongodb_instance" "main" {
42+
name = "test-mongodb-multi-user"
43+
version = "7.0.12"
44+
node_type = "MGDB-PLAY2-NANO"
45+
node_number = 1
46+
user_name = "admin_user"
47+
password = "admin_password123"
48+
volume_size_in_gb = 5
49+
}
50+
51+
resource "scaleway_mongodb_user" "app_user" {
52+
instance_id = scaleway_mongodb_instance.main.id
53+
name = "app_user"
54+
password = "app_password123"
55+
56+
roles {
57+
role = "read_write"
58+
database_name = "app_database"
59+
}
60+
61+
roles {
62+
role = "read"
63+
database_name = "logs_database"
64+
}
65+
}
66+
67+
resource "scaleway_mongodb_user" "admin_user" {
68+
instance_id = scaleway_mongodb_instance.main.id
69+
name = "admin_user"
70+
password = "admin_password123"
71+
72+
roles {
73+
role = "db_admin"
74+
database_name = "admin"
75+
}
76+
77+
roles {
78+
role = "read"
79+
any_database = true
80+
}
81+
}
82+
```
83+
84+
## Argument Reference
85+
86+
The following arguments are supported:
87+
88+
- `instance_id` - (Required) The ID of the MongoDB® instance.
89+
90+
- `name` - (Required) The name of the MongoDB® user.
91+
92+
- `password` - (Required) The password of the MongoDB® user.
93+
94+
- `roles` - (Optional) List of roles assigned to the user. Each role block supports:
95+
- `role` - (Required) The role name. Valid values are `read`, `read_write`, `db_admin`, `sync`.
96+
- `database_name` - (Optional) The database name for the role. Cannot be used with `any_database`.
97+
- `any_database` - (Optional) Apply the role to all databases. Cannot be used with `database_name`.
98+
99+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® user should be created.
100+
101+
## Attributes Reference
102+
103+
In addition to all arguments above, the following attributes are exported:
104+
105+
- `id` - The ID of the MongoDB® user.
106+
107+
- `roles` - The list of roles assigned to the user.
108+
109+
## Import
110+
111+
MongoDB® users can be imported using the `{region}/{instance_id}/{name}`, e.g.
112+
113+
```bash
114+
terraform import scaleway_mongodb_user.main fr-par/11111111-1111-1111-1111-111111111111/my_user
115+
```

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ func Provider(config *Config) plugin.ProviderFunc {
215215
"scaleway_mnq_sqs_queue": mnq.ResourceSQSQueue(),
216216
"scaleway_mongodb_instance": mongodb.ResourceInstance(),
217217
"scaleway_mongodb_snapshot": mongodb.ResourceSnapshot(),
218+
"scaleway_mongodb_user": mongodb.ResourceUser(),
218219
"scaleway_object": object.ResourceObject(),
219220
"scaleway_object_bucket": object.ResourceBucket(),
220221
"scaleway_object_bucket_acl": object.ResourceBucketACL(),

internal/services/mongodb/helpers.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1314
)
1415

1516
const (
@@ -73,3 +74,59 @@ func waitForSnapshot(ctx context.Context, api *mongodb.API, region scw.Region, i
7374
RetryInterval: &retryInterval,
7475
}, scw.WithContext(ctx))
7576
}
77+
78+
// expandUserRoles converts Terraform roles to SDK UserRole slice
79+
func expandUserRoles(rolesSet *schema.Set) []*mongodb.UserRole {
80+
if rolesSet == nil || rolesSet.Len() == 0 {
81+
return nil
82+
}
83+
84+
roles := make([]*mongodb.UserRole, 0, rolesSet.Len())
85+
86+
for _, roleInterface := range rolesSet.List() {
87+
roleMap := roleInterface.(map[string]any)
88+
89+
userRole := &mongodb.UserRole{
90+
Role: mongodb.UserRoleRole(roleMap["role"].(string)),
91+
}
92+
93+
if dbName, ok := roleMap["database_name"]; ok && dbName.(string) != "" {
94+
userRole.DatabaseName = types.ExpandStringPtr(dbName)
95+
}
96+
97+
if anyDB, ok := roleMap["any_database"]; ok && anyDB.(bool) {
98+
userRole.AnyDatabase = scw.BoolPtr(true)
99+
}
100+
101+
roles = append(roles, userRole)
102+
}
103+
104+
return roles
105+
}
106+
107+
// flattenUserRoles converts SDK UserRole slice to Terraform roles
108+
func flattenUserRoles(roles []*mongodb.UserRole) []any {
109+
if len(roles) == 0 {
110+
return nil
111+
}
112+
113+
result := make([]any, 0, len(roles))
114+
115+
for _, role := range roles {
116+
roleMap := map[string]any{
117+
"role": string(role.Role),
118+
}
119+
120+
if role.DatabaseName != nil {
121+
roleMap["database_name"] = *role.DatabaseName
122+
}
123+
124+
if role.AnyDatabase != nil && *role.AnyDatabase {
125+
roleMap["any_database"] = true
126+
}
127+
128+
result = append(result, roleMap)
129+
}
130+
131+
return result
132+
}

0 commit comments

Comments
 (0)