@@ -14,13 +14,14 @@ import (
14
14
"github.com/hashicorp/terraform-plugin-framework/resource"
15
15
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
16
16
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
17
+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
17
18
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
18
19
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
19
20
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
20
21
"github.com/hashicorp/terraform-plugin-framework/types"
21
22
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
22
23
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
23
- matlas "go.mongodb.org/atlas/mongodbatlas "
24
+ "go.mongodb.org/atlas-sdk/v20231115002/admin "
24
25
)
25
26
26
27
const (
@@ -191,6 +192,9 @@ func (r *databaseUserRS) Schema(ctx context.Context, req resource.SchemaRequest,
191
192
},
192
193
},
193
194
"scopes" : schema.SetNestedBlock {
195
+ PlanModifiers : []planmodifier.Set {
196
+ setplanmodifier .RequiresReplace (),
197
+ },
194
198
NestedObject : schema.NestedBlockObject {
195
199
Attributes : map [string ]schema.Attribute {
196
200
"name" : schema.StringAttribute {
@@ -221,8 +225,8 @@ func (r *databaseUserRS) Create(ctx context.Context, req resource.CreateRequest,
221
225
return
222
226
}
223
227
224
- conn := r .Client .Atlas
225
- dbUser , _ , err := conn . DatabaseUsers . Create (ctx , databaseUserPlan .ProjectID .ValueString (), dbUserReq )
228
+ connV2 := r .Client .AtlasV2
229
+ dbUser , _ , err := connV2 . DatabaseUsersApi . CreateDatabaseUser (ctx , databaseUserPlan .ProjectID .ValueString (), dbUserReq ). Execute ( )
226
230
if err != nil {
227
231
resp .Diagnostics .AddError ("error during database user creation" , err .Error ())
228
232
return
@@ -261,8 +265,8 @@ func (r *databaseUserRS) Read(ctx context.Context, req resource.ReadRequest, res
261
265
}
262
266
}
263
267
264
- conn := r .Client .Atlas
265
- dbUser , httpResponse , err := conn . DatabaseUsers . Get (ctx , authDatabaseName , projectID , username )
268
+ connV2 := r .Client .AtlasV2
269
+ dbUser , httpResponse , err := connV2 . DatabaseUsersApi . GetDatabaseUser (ctx , projectID , authDatabaseName , username ). Execute ( )
266
270
if err != nil {
267
271
// case 404
268
272
// deleted in the backend case
@@ -302,8 +306,11 @@ func (r *databaseUserRS) Update(ctx context.Context, req resource.UpdateRequest,
302
306
return
303
307
}
304
308
305
- conn := r .Client .Atlas
306
- dbUser , _ , err := conn .DatabaseUsers .Update (ctx , databaseUserPlan .ProjectID .ValueString (), databaseUserPlan .Username .ValueString (), dbUserReq )
309
+ connV2 := r .Client .AtlasV2
310
+ dbUser , _ , err := connV2 .DatabaseUsersApi .UpdateDatabaseUser (ctx ,
311
+ databaseUserPlan .ProjectID .ValueString (),
312
+ databaseUserPlan .AuthDatabaseName .ValueString (),
313
+ databaseUserPlan .Username .ValueString (), dbUserReq ).Execute ()
307
314
if err != nil {
308
315
resp .Diagnostics .AddError ("error during database user creation" , err .Error ())
309
316
return
@@ -329,8 +336,12 @@ func (r *databaseUserRS) Delete(ctx context.Context, req resource.DeleteRequest,
329
336
return
330
337
}
331
338
332
- conn := r .Client .Atlas
333
- _ , err := conn .DatabaseUsers .Delete (ctx , databaseUserState .AuthDatabaseName .ValueString (), databaseUserState .ProjectID .ValueString (), databaseUserState .Username .ValueString ())
339
+ connV2 := r .Client .AtlasV2
340
+ _ , _ , err := connV2 .DatabaseUsersApi .DeleteDatabaseUser (
341
+ ctx ,
342
+ databaseUserState .ProjectID .ValueString (),
343
+ databaseUserState .AuthDatabaseName .ValueString (),
344
+ databaseUserState .Username .ValueString ()).Execute ()
334
345
if err != nil {
335
346
resp .Diagnostics .AddError ("error when destroying the database user resource" , err .Error ())
336
347
return
@@ -357,7 +368,7 @@ func SplitDatabaseUserImportID(id string) (projectID, username, authDatabaseName
357
368
return
358
369
}
359
370
360
- func newMongoDBDatabaseUser (ctx context.Context , dbUserModel * tfDatabaseUserModel ) (* matlas. DatabaseUser , diag.Diagnostics ) {
371
+ func newMongoDBDatabaseUser (ctx context.Context , dbUserModel * tfDatabaseUserModel ) (* admin. CloudDatabaseUser , diag.Diagnostics ) {
361
372
var rolesModel []* tfRoleModel
362
373
var labelsModel []* tfLabelModel
363
374
var scopesModel []* tfScopeModel
@@ -377,22 +388,22 @@ func newMongoDBDatabaseUser(ctx context.Context, dbUserModel *tfDatabaseUserMode
377
388
return nil , diags
378
389
}
379
390
380
- return & matlas. DatabaseUser {
381
- GroupID : dbUserModel .ProjectID .ValueString (),
391
+ return & admin. CloudDatabaseUser {
392
+ GroupId : dbUserModel .ProjectID .ValueString (),
382
393
Username : dbUserModel .Username .ValueString (),
383
- Password : dbUserModel .Password .ValueString (),
384
- X509Type : dbUserModel .X509Type .ValueString (),
385
- AWSIAMType : dbUserModel .AWSIAMType .ValueString (),
386
- OIDCAuthType : dbUserModel .OIDCAuthType .ValueString (),
387
- LDAPAuthType : dbUserModel .LDAPAuthType .ValueString (),
394
+ Password : dbUserModel .Password .ValueStringPointer (),
395
+ X509Type : dbUserModel .X509Type .ValueStringPointer (),
396
+ AwsIAMType : dbUserModel .AWSIAMType .ValueStringPointer (),
397
+ OidcAuthType : dbUserModel .OIDCAuthType .ValueStringPointer (),
398
+ LdapAuthType : dbUserModel .LDAPAuthType .ValueStringPointer (),
388
399
DatabaseName : dbUserModel .AuthDatabaseName .ValueString (),
389
400
Roles : newMongoDBAtlasRoles (rolesModel ),
390
401
Labels : newMongoDBAtlasLabels (labelsModel ),
391
402
Scopes : newMongoDBAtlasScopes (scopesModel ),
392
403
}, nil
393
404
}
394
405
395
- func newTFDatabaseUserModel (ctx context.Context , model * tfDatabaseUserModel , dbUser * matlas. DatabaseUser ) (* tfDatabaseUserModel , diag.Diagnostics ) {
406
+ func newTFDatabaseUserModel (ctx context.Context , model * tfDatabaseUserModel , dbUser * admin. CloudDatabaseUser ) (* tfDatabaseUserModel , diag.Diagnostics ) {
396
407
rolesSet , diagnostic := types .SetValueFrom (ctx , RoleObjectType , newTFRolesModel (dbUser .Roles ))
397
408
if diagnostic .HasError () {
398
409
return nil , diagnostic
@@ -410,19 +421,19 @@ func newTFDatabaseUserModel(ctx context.Context, model *tfDatabaseUserModel, dbU
410
421
411
422
// ID is encoded to preserve format defined in previous versions.
412
423
encodedID := conversion .EncodeStateID (map [string ]string {
413
- "project_id" : dbUser .GroupID ,
424
+ "project_id" : dbUser .GroupId ,
414
425
"username" : dbUser .Username ,
415
426
"auth_database_name" : dbUser .DatabaseName ,
416
427
})
417
428
databaseUserModel := & tfDatabaseUserModel {
418
429
ID : types .StringValue (encodedID ),
419
- ProjectID : types .StringValue (dbUser .GroupID ),
430
+ ProjectID : types .StringValue (dbUser .GroupId ),
420
431
AuthDatabaseName : types .StringValue (dbUser .DatabaseName ),
421
432
Username : types .StringValue (dbUser .Username ),
422
- X509Type : types .StringValue (dbUser .X509Type ),
423
- OIDCAuthType : types .StringValue (dbUser .OIDCAuthType ),
424
- LDAPAuthType : types .StringValue (dbUser .LDAPAuthType ),
425
- AWSIAMType : types .StringValue (dbUser .AWSIAMType ),
433
+ X509Type : types .StringValue (dbUser .GetX509Type () ),
434
+ OIDCAuthType : types .StringValue (dbUser .GetOidcAuthType () ),
435
+ LDAPAuthType : types .StringValue (dbUser .GetLdapAuthType () ),
436
+ AWSIAMType : types .StringValue (dbUser .GetAwsIAMType () ),
426
437
Roles : rolesSet ,
427
438
Labels : labelsSet ,
428
439
Scopes : scopesSet ,
@@ -436,7 +447,7 @@ func newTFDatabaseUserModel(ctx context.Context, model *tfDatabaseUserModel, dbU
436
447
return databaseUserModel , nil
437
448
}
438
449
439
- func newTFScopesModel (scopes []matlas. Scope ) []tfScopeModel {
450
+ func newTFScopesModel (scopes []admin. UserScope ) []tfScopeModel {
440
451
if len (scopes ) == 0 {
441
452
return nil
442
453
}
@@ -452,23 +463,23 @@ func newTFScopesModel(scopes []matlas.Scope) []tfScopeModel {
452
463
return out
453
464
}
454
465
455
- func newTFLabelsModel (labels []matlas. Label ) []tfLabelModel {
466
+ func newTFLabelsModel (labels []admin. ComponentLabel ) []tfLabelModel {
456
467
if len (labels ) == 0 {
457
468
return nil
458
469
}
459
470
460
471
out := make ([]tfLabelModel , len (labels ))
461
472
for i , v := range labels {
462
473
out [i ] = tfLabelModel {
463
- Key : types .StringValue (v .Key ),
464
- Value : types .StringValue (v .Value ),
474
+ Key : types .StringValue (v .GetKey () ),
475
+ Value : types .StringValue (v .GetValue () ),
465
476
}
466
477
}
467
478
468
479
return out
469
480
}
470
481
471
- func newTFRolesModel (roles []matlas. Role ) []tfRoleModel {
482
+ func newTFRolesModel (roles []admin. DatabaseUserRole ) []tfRoleModel {
472
483
if len (roles ) == 0 {
473
484
return nil
474
485
}
@@ -480,55 +491,55 @@ func newTFRolesModel(roles []matlas.Role) []tfRoleModel {
480
491
DatabaseName : types .StringValue (v .DatabaseName ),
481
492
}
482
493
483
- if v .CollectionName != "" {
484
- out [i ].CollectionName = types .StringValue (v .CollectionName )
494
+ if v .GetCollectionName () != "" {
495
+ out [i ].CollectionName = types .StringValue (v .GetCollectionName () )
485
496
}
486
497
}
487
498
488
499
return out
489
500
}
490
501
491
- func newMongoDBAtlasRoles (roles []* tfRoleModel ) []matlas. Role {
502
+ func newMongoDBAtlasRoles (roles []* tfRoleModel ) []admin. DatabaseUserRole {
492
503
if len (roles ) == 0 {
493
- return []matlas. Role {}
504
+ return []admin. DatabaseUserRole {}
494
505
}
495
506
496
- out := make ([]matlas. Role , len (roles ))
507
+ out := make ([]admin. DatabaseUserRole , len (roles ))
497
508
for i , v := range roles {
498
- out [i ] = matlas. Role {
509
+ out [i ] = admin. DatabaseUserRole {
499
510
RoleName : v .RoleName .ValueString (),
500
511
DatabaseName : v .DatabaseName .ValueString (),
501
- CollectionName : v .CollectionName .ValueString (),
512
+ CollectionName : v .CollectionName .ValueStringPointer (),
502
513
}
503
514
}
504
515
505
516
return out
506
517
}
507
518
508
- func newMongoDBAtlasLabels (labels []* tfLabelModel ) []matlas. Label {
519
+ func newMongoDBAtlasLabels (labels []* tfLabelModel ) []admin. ComponentLabel {
509
520
if len (labels ) == 0 {
510
- return []matlas. Label {}
521
+ return []admin. ComponentLabel {}
511
522
}
512
523
513
- out := make ([]matlas. Label , len (labels ))
524
+ out := make ([]admin. ComponentLabel , len (labels ))
514
525
for i , v := range labels {
515
- out [i ] = matlas. Label {
516
- Key : v .Key .ValueString (),
517
- Value : v .Value .ValueString (),
526
+ out [i ] = admin. ComponentLabel {
527
+ Key : v .Key .ValueStringPointer (),
528
+ Value : v .Value .ValueStringPointer (),
518
529
}
519
530
}
520
531
521
532
return out
522
533
}
523
534
524
- func newMongoDBAtlasScopes (scopes []* tfScopeModel ) []matlas. Scope {
535
+ func newMongoDBAtlasScopes (scopes []* tfScopeModel ) []admin. UserScope {
525
536
if len (scopes ) == 0 {
526
- return []matlas. Scope {}
537
+ return []admin. UserScope {}
527
538
}
528
539
529
- out := make ([]matlas. Scope , len (scopes ))
540
+ out := make ([]admin. UserScope , len (scopes ))
530
541
for i , v := range scopes {
531
- out [i ] = matlas. Scope {
542
+ out [i ] = admin. UserScope {
532
543
Name : v .Name .ValueString (),
533
544
Type : v .Type .ValueString (),
534
545
}
0 commit comments