Skip to content

Commit fe47373

Browse files
committed
feat(mongodb): refacto update snapshot and instance
1 parent c6a3c00 commit fe47373

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

internal/services/mongodb/instance.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ func ResourceInstance() *schema.Resource {
5151
},
5252
},
5353
"node_number": {
54-
Type: schema.TypeInt,
55-
Required: true,
56-
Description: "Number of nodes in the instance",
54+
Type: schema.TypeInt,
55+
Required: true,
56+
ValidateFunc: validation.IntAtLeast(1),
57+
Description: "Number of nodes in the instance",
5758
},
5859
"node_type": {
5960
Type: schema.TypeString,
@@ -324,22 +325,29 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
324325
}
325326
}
326327

328+
////////////////////
329+
// Update instance
330+
////////////////////
331+
332+
shouldUpdateInstance := false
327333
req := &mongodb.UpdateInstanceRequest{
328334
Region: region,
329335
InstanceID: ID,
330336
}
331337

332338
if d.HasChange("name") {
333339
req.Name = types.ExpandStringPtr(d.Get("name"))
340+
shouldUpdateInstance = true
334341
}
335342

336343
if d.HasChange("tags") {
337344
if tags := types.ExpandUpdatedStringsPtr(d.Get("tags")); tags != nil {
338345
req.Tags = tags
346+
shouldUpdateInstance = true
339347
}
340348
}
341349

342-
if req.Name != nil || req.Tags != nil {
350+
if shouldUpdateInstance {
343351
_, err = mongodbAPI.UpdateInstance(req, scw.WithContext(ctx))
344352
if err != nil {
345353
return diag.FromErr(err)
@@ -350,6 +358,7 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
350358
// Update user
351359
////////////////////
352360

361+
shouldUpdateUser := false
353362
updateUserRequest := mongodb.UpdateUserRequest{
354363
Name: d.Get("user_name").(string),
355364
Region: region,
@@ -359,9 +368,10 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
359368
if d.HasChange("password") {
360369
password := d.Get("password").(string)
361370
updateUserRequest.Password = &password
371+
shouldUpdateUser = true
362372
}
363373

364-
if updateUserRequest.Password != nil {
374+
if shouldUpdateUser {
365375
_, err = mongodbAPI.UpdateUser(&updateUserRequest, scw.WithContext(ctx))
366376
if err != nil {
367377
return diag.FromErr(err)

internal/services/mongodb/snapshot.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,22 @@ func ResourceSnapshot() *schema.Resource {
3232
},
3333
SchemaVersion: 0,
3434
Schema: map[string]*schema.Schema{
35-
"id": {
35+
"name": {
3636
Type: schema.TypeString,
37+
Optional: true,
3738
Computed: true,
38-
Description: "Unique identifier of the snapshot",
39+
Description: "Name of the snapshot",
3940
},
4041
"instance_id": {
4142
Type: schema.TypeString,
4243
Required: true,
4344
Description: "The ID of the instance from which the snapshot was created",
4445
},
45-
"name": {
46-
Type: schema.TypeString,
47-
Optional: true,
48-
Computed: true,
49-
Description: "Name of the snapshot",
50-
},
5146
"instance_name": {
5247
Type: schema.TypeString,
5348
Computed: true,
5449
Description: "Name of the instance from which the snapshot was created",
5550
},
56-
5751
"size": {
5852
Type: schema.TypeInt,
5953
Computed: true,
@@ -134,7 +128,6 @@ func ResourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m interfa
134128
if err != nil {
135129
return diag.FromErr(err)
136130
}
137-
_ = d.Set("id", snapshot.ID)
138131
_ = d.Set("instance_id", zonal.NewIDString(zone, snapshot.InstanceID))
139132
_ = d.Set("name", snapshot.Name)
140133
_ = d.Set("instance_name", snapshot.InstanceName)
@@ -164,19 +157,26 @@ func ResourceSnapshotUpdate(ctx context.Context, d *schema.ResourceData, m inter
164157
Region: region,
165158
}
166159

160+
hasChanged := false
161+
167162
if d.HasChange("name") {
168163
newName := types.ExpandOrGenerateString(d.Get("name"), "snapshot")
169164
updateReq.Name = &newName
165+
hasChanged = true
170166
}
171167

172168
if d.HasChange("expires_at") {
173169
updateReq.ExpiresAt = types.ExpandTimePtr(d.Get("expires_at"))
170+
hasChanged = true
174171
}
175172

176-
_, err = mongodbAPI.UpdateSnapshot(updateReq)
177-
if err != nil {
178-
return diag.FromErr(err)
173+
if hasChanged {
174+
_, err = mongodbAPI.UpdateSnapshot(updateReq)
175+
if err != nil {
176+
return diag.FromErr(err)
177+
}
179178
}
179+
180180
instanceID := locality.ExpandID(d.Get("instance_id").(string))
181181

182182
_, err = waitForSnapshot(ctx, mongodbAPI, region, instanceID, snapshotID, d.Timeout(schema.TimeoutUpdate))

0 commit comments

Comments
 (0)