Skip to content

Commit 81bc31f

Browse files
committed
feat(mongodb): add data source and docs
1 parent c91b137 commit 81bc31f

File tree

7 files changed

+12881
-0
lines changed

7 files changed

+12881
-0
lines changed

docs/resources/mongodb_instance.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
subcategory: "MongoDB"
3+
page_title: "Scaleway: scaleway_mongodb_instance"
4+
---
5+
6+
# Resource: scaleway_mongodb_instance
7+
8+
Creates and manages Scaleway MongoDB™ instance.
9+
For more information refer to [the API documentation](https://www.scaleway.com/en/docs/managed-databases/mongodb/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_mongodb_instance" "main" {
17+
name = "test-mongodb-basic1"
18+
version = "7.0.12"
19+
node_type = "MGDB-PLAY2-NANO"
20+
node_number = 1
21+
user_name = "my_initial_user"
22+
password = "thiZ_is_v&ry_s3cret"
23+
volume_size_in_gb = 5
24+
25+
}
26+
```
27+
28+
29+
### Restore From Snapshot
30+
31+
```terraform
32+
33+
resource "scaleway_mongodb_instance" "restored_instance" {
34+
snapshot_id = "${scaleway_vpc_private_network.pn.idscaleway_mongodb_snapshot.main_snapshot.id}"
35+
name = "restored-mongodb-from-snapshot"
36+
node_type = "MGDB-PLAY2-NANO"
37+
node_number = 1
38+
}
39+
```
40+
41+
## Argument Reference
42+
43+
The following arguments are supported:
44+
45+
- `version` - (Optional) MongoDB version of the instance.
46+
- `node_type` - (Required) The type of MongoDB intance to create.
47+
- `user_name` - (Optional) Name of the user created when the intance is created.
48+
- `password` - (Optional) Password of the user.
49+
- `name` - (Optional) Name of the MongoDB instance.
50+
- `tags` - (Optional) List of tags attached to the MongoDB instance.
51+
- `volume_type` - (Optional) Volume type of the instance.
52+
- `volume_size_in_gb` - (Optional) Volume size in GB.
53+
- `snapshot_id` - (Optional) Snapshot ID to restore the MongoDB instance from.
54+
- `public_network` - (Optional) Public network specs details.
55+
56+
## Attributes Reference
57+
58+
In addition to the arguments above, the following attributes are exported:
59+
60+
- `id` - The ID of the MongoDB instance.
61+
- `created_at` - The date and time of the creation of the MongoDB instance.
62+
- `updated_at` - The date and time of the last update of the MongoDB instance.
63+
64+
## Import
65+
66+
MongoDB™ instance can be imported using the `id`, e.g.
67+
68+
```bash
69+
terraform import scaleway_mongodb_instance.main fr-par-1/11111111-1111-1111-1111-111111111111
70+
```

docs/resources/mongodb_snapshot.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
---
3+
subcategory: "MongoDB"
4+
page_title: "Scaleway: scaleway_mongodb_snapshot"
5+
---
6+
7+
# Resource: scaleway_mongodb_snapshot
8+
9+
Creates and manages Scaleway MongoDB snapshots.
10+
For more information refer to [the API documentation](https://www.scaleway.com/en/docs/managed-databases/mongodb/).
11+
12+
## Example Usage
13+
14+
15+
```terraform
16+
17+
resource "scaleway_mongodb_snapshot" "main" {
18+
instance_id = "${scaleway_mongodb_instance.main.id}"
19+
name = "name-snapshot"
20+
expires_at = "2024-12-31T23:59:59Z"
21+
}
22+
```
23+
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
- `instance_id` - (Required) The ID of the MongoDB instance from which the snapshot was created.
30+
31+
- `name` - (Optional) The name of the MongoDB snapshot.
32+
33+
- `expires_at` - (Required) The expiration date of the MongoDB snapshot in ISO 8601 format (e.g. `2024-12-31T23:59:59Z`).
34+
35+
~> **Important:** Once set, `expires_at` cannot be removed.
36+
37+
- `region` - (Defaults to [provider](../index.md) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB snapshot should be created.
38+
39+
## Attributes Reference
40+
41+
In addition to all arguments above, the following attributes are exported:
42+
43+
- `id` - The unique identifier of the MongoDB snapshot.
44+
45+
- `instance_name` - The name of the MongoDB instance from which the snapshot was created.
46+
47+
- `size` - The size of the MongoDB snapshot in bytes.
48+
49+
- `node_type` - The type of node associated with the MongoDB snapshot.
50+
51+
- `volume_type` - The type of volume used for the MongoDB snapshot.
52+
53+
- `created_at` - The date and time when the MongoDB snapshot was created.
54+
55+
- `updated_at` - The date and time of the last update of the MongoDB snapshot.
56+
57+
## Import
58+
59+
MongoDB snapshots can be imported using the `{region}/{id}`, e.g.
60+
61+
```bash
62+
terraform import scaleway_mongodb_snapshot.main fr-par-1/11111111-1111-1111-1111-111111111111
63+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package mongodb
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
14+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
15+
)
16+
17+
func DataSourceInstance() *schema.Resource {
18+
19+
dsSchema := datasource.SchemaFromResourceSchema(ResourceInstance().Schema)
20+
21+
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")
22+
23+
dsSchema["name"].ConflictsWith = []string{"instance_id"}
24+
dsSchema["instance_id"] = &schema.Schema{
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Description: "instance id",
28+
ConflictsWith: []string{"name"},
29+
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
30+
}
31+
32+
return &schema.Resource{
33+
ReadContext: DataSourceInstanceRead,
34+
Schema: dsSchema,
35+
}
36+
}
37+
38+
func DataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
39+
mongodbAPI, zone, region, err := newAPIWithZoneAndRegion(d, m)
40+
if err != nil {
41+
return diag.FromErr(err)
42+
}
43+
44+
instanceID, ok := d.GetOk("instance_id")
45+
if !ok {
46+
instanceName := d.Get("name").(string)
47+
res, err := mongodbAPI.ListInstances(&mongodb.ListInstancesRequest{
48+
Region: region,
49+
Name: types.ExpandStringPtr(instanceName),
50+
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
51+
}, scw.WithContext(ctx))
52+
if err != nil {
53+
return diag.FromErr(err)
54+
}
55+
56+
foundInstance, err := datasource.FindExact(
57+
res.Instances,
58+
func(s *mongodb.Instance) bool { return s.Name == instanceName },
59+
instanceName,
60+
)
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
instanceID = foundInstance.ID
66+
}
67+
68+
zonedID := datasource.NewZonedID(instanceID, zone)
69+
d.SetId(zonedID)
70+
err = d.Set("instance_id", zonedID)
71+
if err != nil {
72+
return diag.FromErr(err)
73+
}
74+
75+
getReq := &mongodb.GetInstanceRequest{
76+
Region: region,
77+
InstanceID: locality.ExpandID(instanceID.(string)),
78+
}
79+
instance, err := mongodbAPI.GetInstance(getReq, scw.WithContext(ctx))
80+
if err != nil {
81+
return diag.FromErr(err)
82+
}
83+
84+
_ = d.Set("name", instance.Name)
85+
_ = d.Set("version", instance.Version)
86+
_ = d.Set("node_number", instance.NodeNumber)
87+
_ = d.Set("node_type", instance.NodeType)
88+
_ = d.Set("project_id", instance.ProjectID)
89+
_ = d.Set("tags", instance.Tags)
90+
_ = d.Set("created_at", instance.CreatedAt.Format(time.RFC3339))
91+
_ = d.Set("region", instance.Region.String())
92+
93+
if instance.Volume != nil {
94+
_ = d.Set("volume_type", instance.Volume.Type)
95+
_ = d.Set("volume_size_in_gb", int(instance.Volume.Size/scw.GB))
96+
}
97+
98+
publicNetworkEndpoint, publicNetworkExists := flattenPublicNetwork(instance.Endpoints)
99+
if publicNetworkExists {
100+
_ = d.Set("public_network", publicNetworkEndpoint)
101+
}
102+
103+
if len(instance.Settings) > 0 {
104+
settingsMap := make(map[string]string)
105+
for _, setting := range instance.Settings {
106+
settingsMap[setting.Name] = setting.Value
107+
}
108+
_ = d.Set("settings", settingsMap)
109+
}
110+
111+
return nil
112+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package mongodb_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
8+
)
9+
10+
func TestAccDataSourceMongoDBInstance_ByName(t *testing.T) {
11+
tt := acctest.NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { acctest.PreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
CheckDestroy: IsInstanceDestroyed(tt),
18+
Steps: []resource.TestStep{
19+
{
20+
Config: `
21+
resource "scaleway_mongodb_instance" "test" {
22+
name = "test-mongodb-instance-by-name"
23+
version = "7.0.12"
24+
node_type = "MGDB-PLAY2-NANO"
25+
node_number = 1
26+
user_name = "my_initial_user"
27+
password = "thiZ_is_v&ry_s3cret"
28+
}
29+
30+
data "scaleway_mongodb_instance" "test_by_name" {
31+
name = scaleway_mongodb_instance.test.name
32+
}
33+
`,
34+
Check: resource.ComposeTestCheckFunc(
35+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_name", "name", "test-mongodb-instance-by-name"),
36+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_name", "version", "7.0.12"),
37+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_name", "node_type", "mgdb-play2-nano"),
38+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_name", "node_number", "1"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
func TestAccDataSourceMongoDBInstance_ByID(t *testing.T) {
46+
tt := acctest.NewTestTools(t)
47+
defer tt.Cleanup()
48+
49+
resource.ParallelTest(t, resource.TestCase{
50+
PreCheck: func() { acctest.PreCheck(t) },
51+
ProviderFactories: tt.ProviderFactories,
52+
CheckDestroy: IsInstanceDestroyed(tt),
53+
Steps: []resource.TestStep{
54+
{
55+
Config: `
56+
resource "scaleway_mongodb_instance" "test" {
57+
name = "test-mongodb-instance-id"
58+
version = "7.0.12"
59+
node_type = "MGDB-PLAY2-NANO"
60+
node_number = 1
61+
user_name = "my_initial_user"
62+
password = "thiZ_is_v&ry_s3cret"
63+
}
64+
65+
data "scaleway_mongodb_instance" "test_by_id" {
66+
instance_id = scaleway_mongodb_instance.test.id
67+
}
68+
`,
69+
Check: resource.ComposeTestCheckFunc(
70+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_id", "name", "test-mongodb-instance-id"),
71+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_id", "version", "7.0.12"),
72+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_id", "node_type", "mgdb-play2-nano"),
73+
resource.TestCheckResourceAttr("data.scaleway_mongodb_instance.test_by_id", "node_number", "1"),
74+
),
75+
},
76+
},
77+
})
78+
}

internal/services/mongodb/instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
217217
createReq.Tags = types.ExpandStrings(tags)
218218
}
219219

220+
epSpecs := make([]*mongodb.EndpointSpec, 0, 1)
221+
spec := &mongodb.EndpointSpecPublicDetails{}
222+
createReq.Endpoints = append(epSpecs, &mongodb.EndpointSpec{Public: spec})
223+
220224
res, err = mongodbAPI.CreateInstance(createReq, scw.WithContext(ctx))
221225
if err != nil {
222226
return diag.FromErr(err)

0 commit comments

Comments
 (0)