Skip to content

Commit c66128d

Browse files
committed
feat(mongodb): with private network
1 parent c830cde commit c66128d

9 files changed

+45016
-48
lines changed

internal/services/mongodb/instance.go

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ package mongodb
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"strings"
87
"time"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1311
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1412
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
1513
"github.com/scaleway/scaleway-sdk-go/scw"
16-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
1714
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1815
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1916
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
@@ -45,17 +42,21 @@ func ResourceInstance() *schema.Resource {
4542
Type: schema.TypeString,
4643
Optional: true,
4744
Computed: true,
48-
Description: "Name of the mongoDB cluster",
45+
Description: "Name of the MongoDB cluster",
4946
},
5047
"version": {
5148
Type: schema.TypeString,
5249
Optional: true,
53-
Description: "Mongodb version of the instance",
50+
Computed: true,
51+
Description: "MongoDB version of the instance",
52+
ConflictsWith: []string{
53+
"snapshot_id",
54+
},
5455
},
5556
"node_number": {
5657
Type: schema.TypeInt,
57-
Optional: true,
58-
Description: "number of node in the instance",
58+
Required: true,
59+
Description: "Number of nodes in the instance",
5960
},
6061
"node_type": {
6162
Type: schema.TypeString,
@@ -67,19 +68,25 @@ func ResourceInstance() *schema.Resource {
6768
Type: schema.TypeString,
6869
Optional: true,
6970
Description: "Name of the user created when the cluster is created",
71+
ConflictsWith: []string{
72+
"snapshot_id",
73+
},
7074
},
7175
"password": {
7276
Type: schema.TypeString,
7377
Sensitive: true,
7478
Optional: true,
7579
Description: "Password of the user",
80+
ConflictsWith: []string{
81+
"snapshot_id",
82+
},
7683
},
7784
// volume
7885
"volume_type": {
7986
Type: schema.TypeString,
8087
Default: mongodb.VolumeTypeSbs5k,
8188
Optional: true,
82-
Description: "Volume size of instance.",
89+
Description: "Volume type of the instance",
8390
},
8491
"volume_size_in_gb": {
8592
Type: schema.TypeInt,
@@ -92,7 +99,12 @@ func ResourceInstance() *schema.Resource {
9299
Type: schema.TypeString,
93100
Optional: true,
94101
ForceNew: true,
95-
Description: "Snapshot id",
102+
Description: "Snapshot ID to restore the MongoDB instance from",
103+
ConflictsWith: []string{
104+
"user_name",
105+
"password",
106+
"version",
107+
},
96108
},
97109
//endpoint
98110
"private_network": {
@@ -134,7 +146,7 @@ func ResourceInstance() *schema.Resource {
134146
"dns_records": {
135147
Type: schema.TypeString,
136148
Computed: true,
137-
Description: "The dns_record of your endpoint",
149+
Description: "The DNS record of your endpoint",
138150
},
139151
// computed
140152
"endpoint_id": {
@@ -167,7 +179,7 @@ func ResourceInstance() *schema.Resource {
167179
"dns_record": {
168180
Type: schema.TypeString,
169181
Computed: true,
170-
Description: "The dns_record of your endpoint",
182+
Description: "The DNS record of your endpoint",
171183
},
172184
},
173185
},
@@ -178,7 +190,7 @@ func ResourceInstance() *schema.Resource {
178190
Elem: &schema.Schema{
179191
Type: schema.TypeString,
180192
},
181-
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a Mongodb instance",
193+
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a MongoDB instance",
182194
},
183195
"settings": {
184196
Type: schema.TypeMap,
@@ -191,39 +203,17 @@ func ResourceInstance() *schema.Resource {
191203
"created_at": {
192204
Type: schema.TypeString,
193205
Computed: true,
194-
Description: "The date and time of the creation of the Mongodb instance",
206+
Description: "The date and time of the creation of the MongoDB instance",
195207
},
196208
"updated_at": {
197209
Type: schema.TypeString,
198210
Computed: true,
199-
Description: "The date and time of the last update of the Mongodb instance",
211+
Description: "The date and time of the last update of the MongoDB instance",
200212
},
201213
// Common
202214
"region": regional.Schema(),
203215
"project_id": account.ProjectIDSchema(),
204216
},
205-
CustomizeDiff: customdiff.All(
206-
cdf.LocalityCheck("private_network.#.id"),
207-
func(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
208-
snapshotID := diff.Get("snapshot_id")
209-
210-
if snapshotID == nil || snapshotID == "" {
211-
if diff.Get("user_name") == nil || diff.Get("user_name") == "" {
212-
return fmt.Errorf("`user_name` must be provided when `snapshot_id` is not set")
213-
}
214-
if diff.Get("password") == nil || diff.Get("password") == "" {
215-
return fmt.Errorf("`password` must be provided when `snapshot_id` is not set")
216-
}
217-
if diff.Get("version") == nil || diff.Get("version") == "" {
218-
return fmt.Errorf("`version` must be provided when `snapshot_id` is not set")
219-
}
220-
if diff.Get("node_number") == nil || diff.Get("node_number") == "" {
221-
return fmt.Errorf("`node_number` must be provided when `snapshot_id` is not set")
222-
}
223-
}
224-
return nil
225-
},
226-
),
227217
}
228218
}
229219

@@ -242,9 +232,11 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
242232
volume := &mongodb.RestoreSnapshotRequestVolumeDetails{
243233
VolumeType: mongodb.VolumeType(d.Get("volume_type").(string)),
244234
}
235+
id := regional.ExpandID(snapshotID.(string))
245236
restoreSnapshotRequest := &mongodb.RestoreSnapshotRequest{
246-
SnapshotID: snapshotID.(string),
237+
SnapshotID: id.ID,
247238
InstanceName: types.ExpandOrGenerateString(d.Get("name"), "mongodb"),
239+
NodeNumber: *nodeNumber,
248240
NodeType: d.Get("node_type").(string),
249241
Volume: volume,
250242
}

internal/services/mongodb/instance_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ func TestAccMongoDBInstance_FromSnapshot(t *testing.T) {
102102
ProviderFactories: tt.ProviderFactories,
103103
CheckDestroy: IsInstanceDestroyed(tt),
104104
Steps: []resource.TestStep{
105-
// Step 1: Create a MongoDB instance and a snapshot
106105
{
107106
Config: `
108107
resource "scaleway_mongodb_instance" "main" {
@@ -117,6 +116,7 @@ func TestAccMongoDBInstance_FromSnapshot(t *testing.T) {
117116
resource "scaleway_mongodb_snapshot" "main_snapshot" {
118117
instance_id = scaleway_mongodb_instance.main.id
119118
name = "test-snapshot"
119+
expires_at = "2024-12-31T23:59:59Z"
120120
depends_on = [
121121
scaleway_mongodb_instance.main
122122
]
@@ -128,6 +128,7 @@ func TestAccMongoDBInstance_FromSnapshot(t *testing.T) {
128128
node_type = "MGDB-PLAY2-NANO"
129129
node_number = 1
130130
depends_on = [
131+
scaleway_mongodb_instance.main,
131132
scaleway_mongodb_snapshot.main_snapshot
132133
]
133134
}

internal/services/mongodb/snapshot.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ func ResourceSnapshot() *schema.Resource {
8282
"expires_at": {
8383
Type: schema.TypeString,
8484
Description: "Expiration date (Format ISO 8601). Cannot be removed.",
85-
Optional: true,
86-
Computed: true,
85+
Required: true,
8786
ValidateDiagFunc: verify.IsDate(),
8887
},
8988
"region": regional.Schema(),

internal/services/mongodb/snapshot_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
99
mongodbSDK "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1113
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/mongodb"
1214
)
1315

@@ -34,6 +36,7 @@ func TestAccMongoDBSnapshot_Basic(t *testing.T) {
3436
resource "scaleway_mongodb_snapshot" "main" {
3537
instance_id = scaleway_mongodb_instance.main.id
3638
name = "test-snapshot"
39+
expires_at = "2024-12-31T23:59:59Z"
3740
}
3841
`,
3942
Check: resource.ComposeTestCheckFunc(
@@ -57,8 +60,8 @@ func TestAccMongoDBSnapshot_Update(t *testing.T) {
5760
Config: `
5861
resource "scaleway_mongodb_instance" "main" {
5962
name = "test-mongodb-instance"
60-
version = "7.0.11"
61-
node_type = "MGDB-PRO2-XXS"
63+
version = "7.0.12"
64+
node_type = "MGDB-PLAY2-NANO"
6265
node_number = 1
6366
user_name = "my_initial_user"
6467
password = "thiZ_is_v&ry_s3cret"
@@ -78,8 +81,8 @@ func TestAccMongoDBSnapshot_Update(t *testing.T) {
7881
Config: `
7982
resource "scaleway_mongodb_instance" "main" {
8083
name = "test-mongodb-instance"
81-
version = "7.0.11"
82-
node_type = "MGDB-PRO2-XXS"
84+
version = "7.0.12"
85+
node_type = "MGDB-PLAY2-NANO"
8386
node_number = 1
8487
user_name = "my_initial_user"
8588
password = "thiZ_is_v&ry_s3cret"
@@ -88,12 +91,12 @@ func TestAccMongoDBSnapshot_Update(t *testing.T) {
8891
resource "scaleway_mongodb_snapshot" "main" {
8992
instance_id = scaleway_mongodb_instance.main.id
9093
name = "updated-snapshot"
91-
expires_at = "2025-12-31T23:59:59Z"
94+
expires_at = "2025-09-20T23:59:59Z"
9295
}
9396
`,
9497
Check: resource.ComposeTestCheckFunc(
9598
resource.TestCheckResourceAttr("scaleway_mongodb_snapshot.main", "name", "updated-snapshot"),
96-
resource.TestCheckResourceAttr("scaleway_mongodb_snapshot.main", "expires_at", "2025-12-31T23:59:59Z"),
99+
resource.TestCheckResourceAttr("scaleway_mongodb_snapshot.main", "expires_at", "2025-09-20T23:59:59Z"),
97100
),
98101
},
99102
},
@@ -111,9 +114,10 @@ func isSnapshotDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
111114
if err != nil {
112115
return err
113116
}
114-
instanceID := rs.Primary.Attributes["instance_id"]
117+
instanceID := zonal.ExpandID(regional.ExpandID(rs.Primary.Attributes["instance_id"]).String())
118+
115119
listSnapshots, err := mongodbAPI.ListSnapshots(&mongodbSDK.ListSnapshotsRequest{
116-
InstanceID: &instanceID,
120+
InstanceID: &instanceID.ID,
117121
Region: region,
118122
})
119123
if err != nil {

0 commit comments

Comments
 (0)