Skip to content

Commit 5817807

Browse files
authored
feat(mongodb): add support for v1alpha1 (#2777)
* feat(mongodb): resource instance * feat(mongodb): add test for instance * feat(mongodb): add basic test for instance * feat(mongodb): add snapshot and test * feat(mongodb): with private network * feat(mongodb): delete private network * t * feat(mongodb): add data source and docs * feat(mongodb): add cassette with sdk * feat(mongodb): fix tflint * feat(mongodb): add datasource doc * feat(mongodb): fix lint * feat(mongodb): fix lint * feat(mongodb): fix lint * feat(mongodb): fix lint * feat(mongodb): fix lint * feat(mongodb): fix lint * feat(mongodb): add mongodb to workflow * feat(mongodb): add test for update tag and password * fix(mongodb): add trademark in docs * feat(mongodb): add getsnapshot in test * feat(mongodb): refacto update snapshot and instance * feat(mongodb): replace TM by ® in docs * feat(mongodb): remove id from snapshot doc * feat(mongodb): add warning when user_name change * feat(mongodb): add id doc snapshot
1 parent 50bfea5 commit 5817807

25 files changed

+59581
-1
lines changed

.github/workflows/acceptance-tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- lb
3232
- marketplace
3333
- mnq
34+
- mongodb
3435
- object
3536
- rdb
3637
- redis

.github/workflows/nightly.yml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
- lb
3333
- marketplace
3434
- mnq
35+
- mongodb
3536
- object
3637
- rdb
3738
- redis

docs/data-sources/mongodb_instance.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
subcategory: "MongoDB®"
3+
page_title: "Scaleway: scaleway_mongodb_instance"
4+
---
5+
6+
# scaleway_mongodb_instance
7+
8+
Gets information about a MongoDB® Instance.
9+
10+
For further information refer to the Managed Databases for MongoDB® [API documentation](https://developers.scaleway.com/en/products/mongodb/api/)
11+
12+
## Example Usage
13+
14+
```hcl
15+
# Get info by name
16+
data "scaleway_mongodb_instance" "my_instance" {
17+
name = "foobar"
18+
}
19+
20+
# Get info by instance ID
21+
data "scaleway_mongodb_instance" "my_instance" {
22+
instance_id = "11111111-1111-1111-1111-111111111111"
23+
}
24+
25+
# Get other attributes
26+
output "mongodb_version" {
27+
description = "Version of the MongoDB instance"
28+
value = data.scaleway_mongodb_instance.my_instance.version
29+
}
30+
```
31+
32+
## Argument Reference
33+
34+
- `name` - (Optional) The name of the MongoDB® instance.
35+
36+
- `instance_id` - (Optional) The MongoDB® instance ID.
37+
38+
-> **Note** You must specify at least one: `name` or `instance_id`.
39+
40+
- `project_id` - (Optional) The ID of the project the MongoDB® instance is in. Can be used to filter instances when using `name`.
41+
42+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#zones) in which the MongoDB® Instance exists.
43+
44+
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the MongoDB® instance is in.
45+
46+
## Attributes Reference
47+
48+
In addition to all above arguments, the following attributes are exported:
49+
50+
- `id` - The ID of the MongoDB® Instance.
51+
- `name` - The name of the MongoDB® instance.
52+
- `version` - The version of MongoDB® running on the instance.
53+
- `node_type` - The type of MongoDB® node.
54+
- `node_number` - The number of nodes in the MongoDB® cluster.
55+
- `created_at` - The date and time the MongoDB® instance was created.
56+
- `project_id` - The ID of the project the instance belongs to.
57+
- `tags` - A list of tags attached to the MongoDB® instance.
58+
- `volume_type` - The type of volume attached to the MongoDB® instance.
59+
- `volume_size_in_gb` - The size of the attached volume, in GB.
60+
- `public_network` - The details of the public network configuration, if applicable.
61+
62+
## Import
63+
64+
MongoDB® instance can be imported using the `id`, e.g.
65+
66+
```bash
67+
terraform import scaleway_mongodb_instance.main fr-par-1/11111111-1111-1111-1111-111111111111
68+
```

docs/resources/mongodb_instance.md

+70
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 all 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

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
subcategory: "MongoDB®"
3+
page_title: "Scaleway: scaleway_mongodb_snapshot"
4+
---
5+
6+
# Resource: scaleway_mongodb_snapshot
7+
8+
Creates and manages Scaleway MongoDB® snapshots.
9+
For more information refer to [the API documentation](https://www.scaleway.com/en/docs/managed-databases/mongodb/).
10+
11+
## Example Usage
12+
13+
14+
```terraform
15+
16+
resource "scaleway_mongodb_snapshot" "main" {
17+
instance_id = "${scaleway_mongodb_instance.main.id}"
18+
name = "name-snapshot"
19+
expires_at = "2024-12-31T23:59:59Z"
20+
}
21+
```
22+
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
- `instance_id` - (Required) The ID of the MongoDB® instance from which the snapshot was created.
29+
30+
- `name` - (Optional) The name of the MongoDB® snapshot.
31+
32+
- `expires_at` - (Required) The expiration date of the MongoDB® snapshot in ISO 8601 format (e.g. `2024-12-31T23:59:59Z`).
33+
34+
~> **Important:** Once set, `expires_at` cannot be removed.
35+
36+
- `region` - (Defaults to [provider](../index.md) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® snapshot should be created.
37+
38+
## Attributes Reference
39+
40+
In addition to all arguments above, the following attributes are exported:
41+
42+
- `id` - The ID of the snapshot.
43+
44+
- `instance_name` - The name of the MongoDB® instance from which the snapshot was created.
45+
46+
- `size` - The size of the MongoDB® snapshot in bytes.
47+
48+
- `node_type` - The type of node associated with the MongoDB® snapshot.
49+
50+
- `volume_type` - The type of volume used for the MongoDB® snapshot.
51+
52+
- `created_at` - The date and time when the MongoDB® snapshot was created.
53+
54+
- `updated_at` - The date and time of the last update of the MongoDB® snapshot.
55+
56+
## Import
57+
58+
MongoDB® snapshots can be imported using the `{region}/{id}`, e.g.
59+
60+
```bash
61+
terraform import scaleway_mongodb_snapshot.main fr-par-1/11111111-1111-1111-1111-111111111111
62+
```

internal/provider/provider.go

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/lb"
3333
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/marketplace"
3434
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/mnq"
35+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/mongodb"
3536
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/object"
3637
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/rdb"
3738
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/redis"
@@ -188,6 +189,8 @@ func Provider(config *Config) plugin.ProviderFunc {
188189
"scaleway_mnq_sqs": mnq.ResourceSQS(),
189190
"scaleway_mnq_sqs_credentials": mnq.ResourceSQSCredentials(),
190191
"scaleway_mnq_sqs_queue": mnq.ResourceSQSQueue(),
192+
"scaleway_mongodb_instance": mongodb.ResourceInstance(),
193+
"scaleway_mongodb_snapshot": mongodb.ResourceSnapshot(),
191194
"scaleway_object": object.ResourceObject(),
192195
"scaleway_object_bucket": object.ResourceBucket(),
193196
"scaleway_object_bucket_acl": object.ResourceBucketACL(),
@@ -281,6 +284,7 @@ func Provider(config *Config) plugin.ProviderFunc {
281284
"scaleway_marketplace_image": marketplace.DataSourceImage(),
282285
"scaleway_mnq_sqs": mnq.DataSourceSQS(),
283286
"scaleway_mnq_sns": mnq.DataSourceSNS(),
287+
"scaleway_mongodb_instance": mongodb.DataSourceInstance(),
284288
"scaleway_object_bucket": object.DataSourceBucket(),
285289
"scaleway_object_bucket_policy": object.DataSourceBucketPolicy(),
286290
"scaleway_rdb_acl": rdb.DataSourceACL(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
mongodb "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+
dsSchema := datasource.SchemaFromResourceSchema(ResourceInstance().Schema)
19+
20+
datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")
21+
22+
dsSchema["name"].ConflictsWith = []string{"instance_id"}
23+
dsSchema["instance_id"] = &schema.Schema{
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Description: "instance id",
27+
ConflictsWith: []string{"name"},
28+
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
29+
}
30+
31+
return &schema.Resource{
32+
ReadContext: DataSourceInstanceRead,
33+
Schema: dsSchema,
34+
}
35+
}
36+
37+
func DataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
38+
mongodbAPI, zone, region, err := newAPIWithZoneAndRegion(d, m)
39+
if err != nil {
40+
return diag.FromErr(err)
41+
}
42+
43+
instanceID, ok := d.GetOk("instance_id")
44+
if !ok {
45+
instanceName := d.Get("name").(string)
46+
res, err := mongodbAPI.ListInstances(&mongodb.ListInstancesRequest{
47+
Region: region,
48+
Name: types.ExpandStringPtr(instanceName),
49+
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
50+
}, scw.WithContext(ctx))
51+
if err != nil {
52+
return diag.FromErr(err)
53+
}
54+
55+
foundInstance, err := datasource.FindExact(
56+
res.Instances,
57+
func(s *mongodb.Instance) bool { return s.Name == instanceName },
58+
instanceName,
59+
)
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
instanceID = foundInstance.ID
65+
}
66+
67+
zonedID := datasource.NewZonedID(instanceID, zone)
68+
d.SetId(zonedID)
69+
err = d.Set("instance_id", zonedID)
70+
if err != nil {
71+
return diag.FromErr(err)
72+
}
73+
74+
getReq := &mongodb.GetInstanceRequest{
75+
Region: region,
76+
InstanceID: locality.ExpandID(instanceID.(string)),
77+
}
78+
instance, err := mongodbAPI.GetInstance(getReq, scw.WithContext(ctx))
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
83+
_ = d.Set("name", instance.Name)
84+
_ = d.Set("version", instance.Version)
85+
_ = d.Set("node_number", int(instance.NodeNumber))
86+
_ = d.Set("node_type", instance.NodeType)
87+
_ = d.Set("project_id", instance.ProjectID)
88+
_ = d.Set("tags", instance.Tags)
89+
_ = d.Set("created_at", instance.CreatedAt.Format(time.RFC3339))
90+
_ = d.Set("region", instance.Region.String())
91+
92+
if instance.Volume != nil {
93+
_ = d.Set("volume_type", instance.Volume.Type)
94+
_ = d.Set("volume_size_in_gb", int(instance.Volume.Size/scw.GB))
95+
}
96+
97+
publicNetworkEndpoint, publicNetworkExists := flattenPublicNetwork(instance.Endpoints)
98+
if publicNetworkExists {
99+
_ = d.Set("public_network", publicNetworkEndpoint)
100+
}
101+
102+
if len(instance.Settings) > 0 {
103+
settingsMap := make(map[string]string)
104+
for _, setting := range instance.Settings {
105+
settingsMap[setting.Name] = setting.Value
106+
}
107+
_ = d.Set("settings", settingsMap)
108+
}
109+
110+
return nil
111+
}

0 commit comments

Comments
 (0)