Skip to content

Commit cead9b1

Browse files
committed
feat(mongodb): add basic test for instance
1 parent 8b2ea3c commit cead9b1

File tree

5 files changed

+77
-217
lines changed

5 files changed

+77
-217
lines changed

internal/services/mongodb/cluster_data_source.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

internal/services/mongodb/cluster_data_source_test.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

internal/services/mongodb/instance.go

Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1717
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1818
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
19+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1920
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
2021
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
2122
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
@@ -51,7 +52,7 @@ func ResourceInstance() *schema.Resource {
5152
Description: "Mongodb version of the instance",
5253
},
5354
"node_number": {
54-
Type: schema.TypeString,
55+
Type: schema.TypeInt,
5556
Required: true,
5657
Description: "number of node in the instance",
5758
},
@@ -74,9 +75,9 @@ func ResourceInstance() *schema.Resource {
7475
},
7576
// volume
7677
"volume_type": {
77-
Type: schema.TypeInt,
78+
Type: schema.TypeString,
79+
Default: mongodb.VolumeTypeSbs5k,
7880
Optional: true,
79-
Computed: true,
8081
Description: "Volume size of instance.",
8182
},
8283
"volume_size_in_gb": {
@@ -123,7 +124,7 @@ func ResourceInstance() *schema.Resource {
123124
Computed: true,
124125
Description: "The port of your load balancer service",
125126
},
126-
"dns_record": {
127+
"dns_records": {
127128
Type: schema.TypeString,
128129
Computed: true,
129130
Description: "The dns_record of your endpoint",
@@ -156,13 +157,6 @@ func ResourceInstance() *schema.Resource {
156157
Computed: true,
157158
Description: "TCP port of the endpoint",
158159
},
159-
"ips": {
160-
Type: schema.TypeList,
161-
Elem: &schema.Schema{
162-
Type: schema.TypeString,
163-
},
164-
Computed: true,
165-
},
166160
"dns_record": {
167161
Type: schema.TypeString,
168162
Computed: true,
@@ -177,7 +171,7 @@ func ResourceInstance() *schema.Resource {
177171
Elem: &schema.Schema{
178172
Type: schema.TypeString,
179173
},
180-
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a redis cluster",
174+
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a Mongodb instance",
181175
},
182176
"settings": {
183177
Type: schema.TypeMap,
@@ -190,61 +184,55 @@ func ResourceInstance() *schema.Resource {
190184
"created_at": {
191185
Type: schema.TypeString,
192186
Computed: true,
193-
Description: "The date and time of the creation of the Redis cluster",
187+
Description: "The date and time of the creation of the Mongodb instance",
194188
},
195189
"updated_at": {
196190
Type: schema.TypeString,
197191
Computed: true,
198192
Description: "The date and time of the last update of the Redis cluster",
199193
},
200194
// Common
201-
"zone": zonal.Schema(),
195+
"region": regional.Schema(),
202196
"project_id": account.ProjectIDSchema(),
203197
},
204198
CustomizeDiff: customdiff.All(
205199
cdf.LocalityCheck("private_network.#.id"),
206-
customizeDiffMigrateClusterSize(),
207200
),
208201
}
209202
}
210203

211-
// to modify
212-
func customizeDiffMigrateClusterSize() schema.CustomizeDiffFunc {
213-
return func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
214-
oldSize, newSize := diff.GetChange("cluster_size")
215-
if newSize == 2 {
216-
return errors.New("cluster_size can be either 1 (standalone) ou >3 (cluster mode), not 2")
217-
}
218-
if oldSize == 1 && newSize != 1 || newSize.(int) < oldSize.(int) {
219-
return diff.ForceNew("cluster_size")
220-
}
221-
return nil
222-
}
223-
}
224-
225204
func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
226205
mongodbAPI, zone, err := newAPIWithZone(d, m)
227206

228207
if err != nil {
229208
return diag.FromErr(err)
230209
}
231210

211+
nodeNumber := scw.Uint32Ptr(uint32(d.Get("node_number").(int)))
232212
createReq := &mongodb.CreateInstanceRequest{
233-
ProjectID: d.Get("project_id").(string),
234-
Name: types.ExpandOrGenerateString(d.Get("name"), "redis"),
235-
Version: d.Get("version").(string),
236-
NodeType: d.Get("node_type").(string),
237-
UserName: d.Get("user_name").(string),
238-
Password: d.Get("password").(string),
213+
ProjectID: d.Get("project_id").(string),
214+
Name: types.ExpandOrGenerateString(d.Get("name"), "mongodb"),
215+
Version: d.Get("version").(string),
216+
NodeType: d.Get("node_type").(string),
217+
NodeNumber: *nodeNumber,
218+
UserName: d.Get("user_name").(string),
219+
Password: d.Get("password").(string),
239220
}
240221

241-
tags, tagsExist := d.GetOk("tags")
242-
if tagsExist {
243-
createReq.Tags = types.ExpandStrings(tags)
222+
volumeRequestDetails := &mongodb.CreateInstanceRequestVolumeDetails{
223+
VolumeType: mongodb.VolumeType(d.Get("volume_type").(string)),
244224
}
245225
volumeSize, volumeSizeExist := d.GetOk("volume_size_in_gb")
246226
if volumeSizeExist {
247-
createReq.Volume.VolumeSize = scw.Size(uint64(volumeSize.(int)))
227+
volumeRequestDetails.VolumeSize = scw.Size(uint64(volumeSize.(int)) * uint64(scw.GB))
228+
} else {
229+
volumeRequestDetails.VolumeSize = scw.Size(defaultVolumeSize * uint64(scw.GB))
230+
}
231+
createReq.Volume = volumeRequestDetails
232+
233+
tags, tagsExist := d.GetOk("tags")
234+
if tagsExist {
235+
createReq.Tags = types.ExpandStrings(tags)
248236
}
249237

250238
pn, pnExists := d.GetOk("private_network")
@@ -254,6 +242,10 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
254242
return diag.FromErr(err)
255243
}
256244
createReq.Endpoints = pnSpecs
245+
} else {
246+
epSpecs := make([]*mongodb.EndpointSpec, 0, 1)
247+
spec := &mongodb.EndpointSpecPublicDetails{}
248+
createReq.Endpoints = append(epSpecs, &mongodb.EndpointSpec{Public: spec})
257249
}
258250

259251
res, err := mongodbAPI.CreateInstance(createReq, scw.WithContext(ctx))
@@ -305,11 +297,14 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
305297
_ = d.Set("volume_size_in_gb", instance.Volume.Size)
306298
}
307299

308-
privateNetworkEndpoints, privateNetworkExists := flattenPrivateNetwork(m, instance.Endpoints, region)
300+
privateNetworkEndpoints, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)
309301
if privateNetworkExists {
310302
_ = d.Set("private_network", privateNetworkEndpoints)
311303
}
312-
_ = d.Set("public_network", flattenPublicNetwork(instance.Endpoints))
304+
publicNetworkEndpoint, publicNetworkExists := flattenPublicNetwork(instance.Endpoints)
305+
if publicNetworkExists {
306+
_ = d.Set("public_network", publicNetworkEndpoint)
307+
}
313308

314309
if len(instance.Settings) > 0 {
315310
settingsMap := make(map[string]string)
@@ -395,7 +390,6 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
395390
}
396391

397392
_, err = mongodbAPI.UpdateUser(&updateUserRequest, scw.WithContext(ctx))
398-
// set endpoint ?
399393

400394
_, err = waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutCreate))
401395
if err != nil {
@@ -405,34 +399,6 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
405399
return ResourceInstanceRead(ctx, d, m)
406400
}
407401

408-
//func ResourceInstanceUpdateEndpoints(ctx context.Context, d *schema.ResourceData, mongodbAPI *mongodb.API, region scw.Region, instanceID string) diag.Diagnostics {
409-
// // retrieve state
410-
//
411-
// instance, err := waitForInstance(ctx, mongodbAPI, region, instanceID, d.Timeout(schema.TimeoutUpdate))
412-
// if err != nil {
413-
// return diag.FromErr(err)
414-
// }
415-
//
416-
// // get new desired state of endpoints
417-
// rawNewEndpoints := d.Get("private_network")
418-
// newEndpoints, err := expandPrivateNetwork(rawNewEndpoints.(*schema.Set).List())
419-
// if err != nil {
420-
// return diag.FromErr(err)
421-
// }
422-
// if len(newEndpoints) == 0 {
423-
// newEndpoints = append(newEndpoints, &mongodb.EndpointSpec{
424-
// Public: &mongodb.EndpointSpecPublicDetails{},
425-
// })
426-
// }
427-
//
428-
// _, err = waitForInstance(ctx, reAPI, zone, clusterID, d.Timeout(schema.TimeoutUpdate))
429-
// if err != nil {
430-
// return diag.FromErr(err)
431-
// }
432-
//
433-
// return nil
434-
//}
435-
436402
func ResourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
437403
mongodbAPI, region, ID, err := NewAPIWithRegionAndID(m, d.Id())
438404
if err != nil {

0 commit comments

Comments
 (0)