Skip to content

Commit 99407b9

Browse files
authored
[OP-2308] Added mongodb_role resource (#30)
* Added role commands to mongodb pkg * tmp * Implemented role resource * go get -u * Added role resource example * Added more docs and validators * Removed test code * Check error from applyRole * Added acceptance tests * Run tests with MongoDB * go fmt * Check error * Use localhost
1 parent 069cf63 commit 99407b9

26 files changed

+1697
-330
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ jobs:
6969
matrix:
7070
# list whatever Terraform versions here you would like to support
7171
tofu:
72-
- '1.6.0-alpha5'
72+
- '1.6.0'
73+
services:
74+
mongodb:
75+
image: mongo:7
76+
ports:
77+
- "27017:27017"
7378
steps:
7479
- uses: actions/checkout@v4
7580
- uses: actions/setup-go@v5

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ generate:
1010
test:
1111
go test ./...
1212

13+
# Run acceptance tests
14+
.PHONY: testacc
15+
testacc:
16+
TF_ACC=1 go test -count=1 ./...
17+
1318
.PHONY: deps
1419
deps: deps-npm deps-pip
1520

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,41 @@ To run the tests, run `make test`.
3939
2. Enter the repository directory
4040
3. Build the provider using the Go `install` command:
4141

42+
```shell
43+
go install
44+
```
45+
46+
4. Tell Terraform via `~/.terraformrc` to use the locally built version of the
47+
provider: (must use absolute path, Terraform does not understand `~/go/bin`)
48+
49+
```terraform
50+
// File: ~/.terraformrc
51+
provider_installation {
52+
dev_overrides {
53+
"registry.terraform.io/RiskIdent/mongodb-driver" = "/home/<your username>/go/bin"
54+
"registry.opentofu.org/RiskIdent/mongodb-driver" = "/home/<your username>/go/bin"
55+
}
56+
direct {}
57+
}
58+
```
59+
60+
### Testing
61+
62+
First start MongoDB locally, such as via Podman:
63+
4264
```shell
43-
go install
65+
podman run -d --rm -p 27017:27017 --name mongo mongo
4466
```
4567

68+
Then run the Go tests with the `TF_ACC=1` environment variable set:
69+
70+
```shell
71+
TF_ACC=1 go test -count=1 ./...
72+
```
73+
74+
The MongoDB URI that the tests try to access can be overridden with
75+
the `MONGODB_URI` environment variable.
76+
4677
## License
4778

4879
This repository complies with the [REUSE recommendations](https://reuse.software/).

docs/resources/mongodb_role.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "mongodb_role Resource - terraform-provider-mongodb-driver"
4+
subcategory: ""
5+
description: |-
6+
Role resource
7+
---
8+
9+
# mongodb_role (Resource)
10+
11+
Role resource
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "mongodb_role" "example" {
17+
role = "myClusterwideAdmin"
18+
db = "admin"
19+
privileges = [
20+
{
21+
resource = { cluster = true }
22+
actions = ["addShard"]
23+
},
24+
{
25+
resource = { db = "config", collection = "" }
26+
actions = ["find", "update", "insert", "remove"]
27+
},
28+
{
29+
resource = { db = "users", collection = "usersCollection" },
30+
actions = ["update", "insert", "remove"]
31+
},
32+
{
33+
resource = { db = "", collection = "" },
34+
actions = ["find"]
35+
}
36+
]
37+
roles = [
38+
{ role = "read", db = "admin" },
39+
]
40+
}
41+
```
42+
43+
<!-- schema generated by tfplugindocs -->
44+
## Schema
45+
46+
### Required
47+
48+
- `db` (String) Database this MongoDB role belongs to.
49+
50+
MongoDB has some restrictions on database names. Such as:
51+
52+
- Cannot contain any of the following characters (we're following Windows limits): `/\. "$*<>:|?`
53+
- Cannot create roles in the `local` database.
54+
- Cannot be empty.
55+
- Cannot be longer than 64 characters.
56+
57+
See documentation:
58+
59+
- <https://www.mongodb.com/docs/manual/reference/command/createRole/#local-database>
60+
- <https://www.mongodb.com/docs/v6.0/reference/limits/#naming-restrictions>
61+
- `role` (String) Rolename for this MongoDB role.
62+
63+
### Optional
64+
65+
- `privileges` (Attributes Set) Privileges this role has. (see [below for nested schema](#nestedatt--privileges))
66+
- `roles` (Attributes Set) Roles this role inherits privileges from. (see [below for nested schema](#nestedatt--roles))
67+
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))
68+
69+
### Read-Only
70+
71+
- `id` (String) Role unique ID in MongoDB. Is composed from the `db` and `role` fields.
72+
73+
<a id="nestedatt--privileges"></a>
74+
### Nested Schema for `privileges`
75+
76+
Required:
77+
78+
- `actions` (Set of String) Database this role belongs to. Leave unset to target same database as role.
79+
See: <https://www.mongodb.com/docs/manual/reference/privilege-actions/>
80+
- `resource` (Attributes) A document that specifies the resources upon which the privilege `actions` apply.
81+
82+
Can only supply one of the following attribute combinations: - only `cluster` attribute, must be set to `true` - only `any_resource` attribute, must be set to `true` - only `db` and `collection` attributes (see [below for nested schema](#nestedatt--privileges--resource))
83+
84+
<a id="nestedatt--privileges--resource"></a>
85+
### Nested Schema for `privileges.resource`
86+
87+
Optional:
88+
89+
- `any_resource` (Boolean) Set to true to target every resource in the system. Intended for internal use. **Do not** use this resource, other than in exceptional circumstances.
90+
- `cluster` (Boolean) Set to true to target the MongoDB cluster as the resource.
91+
- `collection` (String) Specify which collection to target. Must be paired with the `db` attribute.
92+
- `db` (String) Specify which database to target. Must be paired with the `collection` attribute. If both the `db` and `collections` are empty strings (`""`), the resource is all collections, excluding the system collections, in all the databases. If only the `db` attribute is an empty string (`""`), the resource is all collections with the specified `collection` name across all databases.If only the `collection` attribute is an empty string (`""`), the resource is the specified database, excluding the system collections.
93+
94+
95+
96+
<a id="nestedatt--roles"></a>
97+
### Nested Schema for `roles`
98+
99+
Required:
100+
101+
- `role` (String) Role name
102+
103+
Optional:
104+
105+
- `db` (String) Database this role belongs to. Leave unset to target same database as role.
106+
107+
108+
<a id="nestedatt--timeouts"></a>
109+
### Nested Schema for `timeouts`
110+
111+
Optional:
112+
113+
- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
114+
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
115+
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
116+
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).

docs/resources/mongodb_user.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ resource "mongodb_user" "example" {
4444
db = "my-db"
4545
pwd = "super-secret-password"
4646
47-
customData = {
47+
custom_data = {
4848
"my-custom-field" = "my-custom-value"
4949
}
5050
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "mongodb_role" "example" {
2+
role = "myClusterwideAdmin"
3+
db = "admin"
4+
privileges = [
5+
{
6+
resource = { cluster = true }
7+
actions = ["addShard"]
8+
},
9+
{
10+
resource = { db = "config", collection = "" }
11+
actions = ["find", "update", "insert", "remove"]
12+
},
13+
{
14+
resource = { db = "users", collection = "usersCollection" },
15+
actions = ["update", "insert", "remove"]
16+
},
17+
{
18+
resource = { db = "", collection = "" },
19+
actions = ["find"]
20+
}
21+
]
22+
roles = [
23+
{ role = "read", db = "admin" },
24+
]
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2023 Risk.Ident GmbH <[email protected]>
2+
3+
SPDX-License-Identifier: CC-BY-4.0

examples/resources/mongodb_user/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "mongodb_user" "example" {
2929
db = "my-db"
3030
pwd = "super-secret-password"
3131

32-
customData = {
32+
custom_data = {
3333
"my-custom-field" = "my-custom-value"
3434
}
3535
}

go.mod

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,79 @@ require (
1111
github.com/hashicorp/terraform-plugin-framework v1.5.0
1212
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
1313
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
14+
github.com/hashicorp/terraform-plugin-go v0.20.0
1415
github.com/hashicorp/terraform-plugin-log v0.9.0
16+
github.com/hashicorp/terraform-plugin-testing v1.6.0
1517
go.mongodb.org/mongo-driver v1.13.1
1618
)
1719

1820
require (
1921
github.com/Masterminds/goutils v1.1.1 // indirect
2022
github.com/Masterminds/semver/v3 v3.1.1 // indirect
2123
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
22-
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
23-
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
24+
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
25+
github.com/agext/levenshtein v1.2.2 // indirect
26+
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
2427
github.com/armon/go-radix v1.0.0 // indirect
2528
github.com/bgentry/speakeasy v0.1.0 // indirect
2629
github.com/cloudflare/circl v1.3.7 // indirect
2730
github.com/fatih/color v1.16.0 // indirect
2831
github.com/golang/protobuf v1.5.3 // indirect
2932
github.com/golang/snappy v0.0.4 // indirect
33+
github.com/google/go-cmp v0.6.0 // indirect
3034
github.com/google/uuid v1.3.1 // indirect
3135
github.com/hashicorp/errwrap v1.1.0 // indirect
3236
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
3337
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
34-
github.com/hashicorp/go-hclog v1.5.0 // indirect
38+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
39+
github.com/hashicorp/go-hclog v1.6.2 // indirect
3540
github.com/hashicorp/go-multierror v1.1.1 // indirect
3641
github.com/hashicorp/go-plugin v1.6.0 // indirect
3742
github.com/hashicorp/go-uuid v1.0.3 // indirect
3843
github.com/hashicorp/go-version v1.6.0 // indirect
39-
github.com/hashicorp/hc-install v0.5.2 // indirect
40-
github.com/hashicorp/terraform-exec v0.18.1 // indirect
41-
github.com/hashicorp/terraform-json v0.17.1 // indirect
42-
github.com/hashicorp/terraform-plugin-go v0.20.0 // indirect
44+
github.com/hashicorp/hc-install v0.6.1 // indirect
45+
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
46+
github.com/hashicorp/logutils v1.0.0 // indirect
47+
github.com/hashicorp/terraform-exec v0.19.0 // indirect
48+
github.com/hashicorp/terraform-json v0.18.0 // indirect
49+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
4350
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
4451
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4552
github.com/hashicorp/yamux v0.1.1 // indirect
4653
github.com/huandu/xstrings v1.3.2 // indirect
47-
github.com/imdario/mergo v0.3.13 // indirect
48-
github.com/klauspost/compress v1.17.3 // indirect
54+
github.com/imdario/mergo v0.3.15 // indirect
55+
github.com/klauspost/compress v1.17.4 // indirect
4956
github.com/mattn/go-colorable v0.1.13 // indirect
5057
github.com/mattn/go-isatty v0.0.20 // indirect
5158
github.com/mitchellh/cli v1.1.5 // indirect
5259
github.com/mitchellh/copystructure v1.2.0 // indirect
5360
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
61+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
62+
github.com/mitchellh/mapstructure v1.5.0 // indirect
5463
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5564
github.com/montanaflynn/stats v0.7.1 // indirect
5665
github.com/oklog/run v1.1.0 // indirect
5766
github.com/posener/complete v1.2.3 // indirect
5867
github.com/russross/blackfriday v1.6.0 // indirect
5968
github.com/shopspring/decimal v1.3.1 // indirect
6069
github.com/spf13/cast v1.5.0 // indirect
70+
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
6171
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
6272
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
6373
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
6474
github.com/xdg-go/scram v1.1.2 // indirect
6575
github.com/xdg-go/stringprep v1.0.4 // indirect
6676
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
67-
github.com/zclconf/go-cty v1.13.2 // indirect
68-
golang.org/x/crypto v0.17.0 // indirect
69-
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
70-
golang.org/x/mod v0.11.0 // indirect
71-
golang.org/x/net v0.18.0 // indirect
72-
golang.org/x/sync v0.5.0 // indirect
73-
golang.org/x/sys v0.15.0 // indirect
77+
github.com/zclconf/go-cty v1.14.1 // indirect
78+
golang.org/x/crypto v0.18.0 // indirect
79+
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
80+
golang.org/x/mod v0.13.0 // indirect
81+
golang.org/x/net v0.20.0 // indirect
82+
golang.org/x/sync v0.6.0 // indirect
83+
golang.org/x/sys v0.16.0 // indirect
7484
golang.org/x/text v0.14.0 // indirect
75-
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
76-
google.golang.org/grpc v1.60.0 // indirect
77-
google.golang.org/protobuf v1.31.0 // indirect
85+
google.golang.org/appengine v1.6.8 // indirect
86+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
87+
google.golang.org/grpc v1.60.1 // indirect
88+
google.golang.org/protobuf v1.32.0 // indirect
7889
)

0 commit comments

Comments
 (0)