Skip to content

Commit 87db18d

Browse files
oarbusilantoli
andauthored
chore: migrate database user resource to new SDK (#1723)
* migrate database user resource to new SDK * fix * getter instead of pointer values * fix * don't use getter for non pointer attributes * add planModifier --------- Co-authored-by: Leo Antoli <[email protected]>
1 parent 5c1bebd commit 87db18d

7 files changed

+147
-135
lines changed

internal/service/databaseuser/data_source_database_user.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/diag"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
12-
matlas "go.mongodb.org/atlas/mongodbatlas"
12+
"go.mongodb.org/atlas-sdk/v20231115002/admin"
1313
)
1414

1515
type databaseUserDS struct {
@@ -131,8 +131,8 @@ func (d *databaseUserDS) Read(ctx context.Context, req datasource.ReadRequest, r
131131
projectID := databaseDSUserModel.ProjectID.ValueString()
132132
authDatabaseName := databaseDSUserModel.AuthDatabaseName.ValueString()
133133

134-
conn := d.Client.Atlas
135-
dbUser, _, err := conn.DatabaseUsers.Get(ctx, authDatabaseName, projectID, username)
134+
connV2 := d.Client.AtlasV2
135+
dbUser, _, err := connV2.DatabaseUsersApi.GetDatabaseUser(ctx, projectID, authDatabaseName, username).Execute()
136136
if err != nil {
137137
resp.Diagnostics.AddError("error getting database user information", err.Error())
138138
return
@@ -150,18 +150,18 @@ func (d *databaseUserDS) Read(ctx context.Context, req datasource.ReadRequest, r
150150
}
151151
}
152152

153-
func newTFDatabaseDSUserModel(ctx context.Context, dbUser *matlas.DatabaseUser) (*tfDatabaseUserDSModel, diag.Diagnostics) {
154-
id := fmt.Sprintf("%s-%s-%s", dbUser.GroupID, dbUser.Username, dbUser.DatabaseName)
153+
func newTFDatabaseDSUserModel(ctx context.Context, dbUser *admin.CloudDatabaseUser) (*tfDatabaseUserDSModel, diag.Diagnostics) {
154+
id := fmt.Sprintf("%s-%s-%s", dbUser.GroupId, dbUser.Username, dbUser.DatabaseName)
155155
databaseUserModel := &tfDatabaseUserDSModel{
156156
ID: types.StringValue(id),
157-
ProjectID: types.StringValue(dbUser.GroupID),
157+
ProjectID: types.StringValue(dbUser.GroupId),
158158
AuthDatabaseName: types.StringValue(dbUser.DatabaseName),
159159
Username: types.StringValue(dbUser.Username),
160-
Password: types.StringValue(dbUser.Password),
161-
X509Type: types.StringValue(dbUser.X509Type),
162-
OIDCAuthType: types.StringValue(dbUser.OIDCAuthType),
163-
LDAPAuthType: types.StringValue(dbUser.LDAPAuthType),
164-
AWSIAMType: types.StringValue(dbUser.AWSIAMType),
160+
Password: types.StringValue(dbUser.GetPassword()),
161+
X509Type: types.StringValue(dbUser.GetX509Type()),
162+
OIDCAuthType: types.StringValue(dbUser.GetOidcAuthType()),
163+
LDAPAuthType: types.StringValue(dbUser.GetLdapAuthType()),
164+
AWSIAMType: types.StringValue(dbUser.GetAwsIAMType()),
165165
Roles: newTFRolesModel(dbUser.Roles),
166166
Labels: newTFLabelsModel(dbUser.Labels),
167167
Scopes: newTFScopesModel(dbUser.Scopes),

internal/service/databaseuser/data_source_database_user_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1010
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
11-
matlas "go.mongodb.org/atlas/mongodbatlas"
11+
"go.mongodb.org/atlas-sdk/v20231115002/admin"
1212
)
1313

1414
func TestAccConfigDSDatabaseUser_basic(t *testing.T) {
15-
var dbUser matlas.DatabaseUser
15+
var dbUser admin.CloudDatabaseUser
1616

1717
resourceName := "data.mongodbatlas_database_user.test"
1818
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")

internal/service/databaseuser/data_source_database_users.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/types"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
12-
matlas "go.mongodb.org/atlas/mongodbatlas"
12+
"go.mongodb.org/atlas-sdk/v20231115002/admin"
1313
)
1414

1515
const (
@@ -137,14 +137,14 @@ func (d *DatabaseUsersDS) Read(ctx context.Context, req datasource.ReadRequest,
137137
}
138138

139139
projectID := databaseUsersModel.ProjectID.ValueString()
140-
conn := d.Client.Atlas
141-
dbUser, _, err := conn.DatabaseUsers.List(ctx, projectID, nil)
140+
connV2 := d.Client.AtlasV2
141+
dbUser, _, err := connV2.DatabaseUsersApi.ListDatabaseUsers(ctx, projectID).Execute()
142142
if err != nil {
143143
resp.Diagnostics.AddError("error getting database user information", err.Error())
144144
return
145145
}
146146

147-
dbUserModel, diagnostic := newTFDatabaseUsersMode(ctx, projectID, dbUser)
147+
dbUserModel, diagnostic := newTFDatabaseUsersMode(ctx, projectID, dbUser.GetResults())
148148
resp.Diagnostics.Append(diagnostic...)
149149
if resp.Diagnostics.HasError() {
150150
return
@@ -156,7 +156,7 @@ func (d *DatabaseUsersDS) Read(ctx context.Context, req datasource.ReadRequest,
156156
}
157157
}
158158

159-
func newTFDatabaseUsersMode(ctx context.Context, projectID string, dbUsers []matlas.DatabaseUser) (*tfDatabaseUsersDSModel, diag.Diagnostics) {
159+
func newTFDatabaseUsersMode(ctx context.Context, projectID string, dbUsers []admin.CloudDatabaseUser) (*tfDatabaseUsersDSModel, diag.Diagnostics) {
160160
results := make([]*tfDatabaseUserDSModel, len(dbUsers))
161161
for i := range dbUsers {
162162
dbUserModel, d := newTFDatabaseDSUserModel(ctx, &dbUsers[i])

internal/service/databaseuser/resource_database_user.go

+57-46
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import (
1414
"github.com/hashicorp/terraform-plugin-framework/resource"
1515
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1616
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1920
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2021
"github.com/hashicorp/terraform-plugin-framework/types"
2122
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
2223
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
23-
matlas "go.mongodb.org/atlas/mongodbatlas"
24+
"go.mongodb.org/atlas-sdk/v20231115002/admin"
2425
)
2526

2627
const (
@@ -191,6 +192,9 @@ func (r *databaseUserRS) Schema(ctx context.Context, req resource.SchemaRequest,
191192
},
192193
},
193194
"scopes": schema.SetNestedBlock{
195+
PlanModifiers: []planmodifier.Set{
196+
setplanmodifier.RequiresReplace(),
197+
},
194198
NestedObject: schema.NestedBlockObject{
195199
Attributes: map[string]schema.Attribute{
196200
"name": schema.StringAttribute{
@@ -221,8 +225,8 @@ func (r *databaseUserRS) Create(ctx context.Context, req resource.CreateRequest,
221225
return
222226
}
223227

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()
226230
if err != nil {
227231
resp.Diagnostics.AddError("error during database user creation", err.Error())
228232
return
@@ -261,8 +265,8 @@ func (r *databaseUserRS) Read(ctx context.Context, req resource.ReadRequest, res
261265
}
262266
}
263267

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()
266270
if err != nil {
267271
// case 404
268272
// deleted in the backend case
@@ -302,8 +306,11 @@ func (r *databaseUserRS) Update(ctx context.Context, req resource.UpdateRequest,
302306
return
303307
}
304308

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()
307314
if err != nil {
308315
resp.Diagnostics.AddError("error during database user creation", err.Error())
309316
return
@@ -329,8 +336,12 @@ func (r *databaseUserRS) Delete(ctx context.Context, req resource.DeleteRequest,
329336
return
330337
}
331338

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()
334345
if err != nil {
335346
resp.Diagnostics.AddError("error when destroying the database user resource", err.Error())
336347
return
@@ -357,7 +368,7 @@ func SplitDatabaseUserImportID(id string) (projectID, username, authDatabaseName
357368
return
358369
}
359370

360-
func newMongoDBDatabaseUser(ctx context.Context, dbUserModel *tfDatabaseUserModel) (*matlas.DatabaseUser, diag.Diagnostics) {
371+
func newMongoDBDatabaseUser(ctx context.Context, dbUserModel *tfDatabaseUserModel) (*admin.CloudDatabaseUser, diag.Diagnostics) {
361372
var rolesModel []*tfRoleModel
362373
var labelsModel []*tfLabelModel
363374
var scopesModel []*tfScopeModel
@@ -377,22 +388,22 @@ func newMongoDBDatabaseUser(ctx context.Context, dbUserModel *tfDatabaseUserMode
377388
return nil, diags
378389
}
379390

380-
return &matlas.DatabaseUser{
381-
GroupID: dbUserModel.ProjectID.ValueString(),
391+
return &admin.CloudDatabaseUser{
392+
GroupId: dbUserModel.ProjectID.ValueString(),
382393
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(),
388399
DatabaseName: dbUserModel.AuthDatabaseName.ValueString(),
389400
Roles: newMongoDBAtlasRoles(rolesModel),
390401
Labels: newMongoDBAtlasLabels(labelsModel),
391402
Scopes: newMongoDBAtlasScopes(scopesModel),
392403
}, nil
393404
}
394405

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) {
396407
rolesSet, diagnostic := types.SetValueFrom(ctx, RoleObjectType, newTFRolesModel(dbUser.Roles))
397408
if diagnostic.HasError() {
398409
return nil, diagnostic
@@ -410,19 +421,19 @@ func newTFDatabaseUserModel(ctx context.Context, model *tfDatabaseUserModel, dbU
410421

411422
// ID is encoded to preserve format defined in previous versions.
412423
encodedID := conversion.EncodeStateID(map[string]string{
413-
"project_id": dbUser.GroupID,
424+
"project_id": dbUser.GroupId,
414425
"username": dbUser.Username,
415426
"auth_database_name": dbUser.DatabaseName,
416427
})
417428
databaseUserModel := &tfDatabaseUserModel{
418429
ID: types.StringValue(encodedID),
419-
ProjectID: types.StringValue(dbUser.GroupID),
430+
ProjectID: types.StringValue(dbUser.GroupId),
420431
AuthDatabaseName: types.StringValue(dbUser.DatabaseName),
421432
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()),
426437
Roles: rolesSet,
427438
Labels: labelsSet,
428439
Scopes: scopesSet,
@@ -436,7 +447,7 @@ func newTFDatabaseUserModel(ctx context.Context, model *tfDatabaseUserModel, dbU
436447
return databaseUserModel, nil
437448
}
438449

439-
func newTFScopesModel(scopes []matlas.Scope) []tfScopeModel {
450+
func newTFScopesModel(scopes []admin.UserScope) []tfScopeModel {
440451
if len(scopes) == 0 {
441452
return nil
442453
}
@@ -452,23 +463,23 @@ func newTFScopesModel(scopes []matlas.Scope) []tfScopeModel {
452463
return out
453464
}
454465

455-
func newTFLabelsModel(labels []matlas.Label) []tfLabelModel {
466+
func newTFLabelsModel(labels []admin.ComponentLabel) []tfLabelModel {
456467
if len(labels) == 0 {
457468
return nil
458469
}
459470

460471
out := make([]tfLabelModel, len(labels))
461472
for i, v := range labels {
462473
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()),
465476
}
466477
}
467478

468479
return out
469480
}
470481

471-
func newTFRolesModel(roles []matlas.Role) []tfRoleModel {
482+
func newTFRolesModel(roles []admin.DatabaseUserRole) []tfRoleModel {
472483
if len(roles) == 0 {
473484
return nil
474485
}
@@ -480,55 +491,55 @@ func newTFRolesModel(roles []matlas.Role) []tfRoleModel {
480491
DatabaseName: types.StringValue(v.DatabaseName),
481492
}
482493

483-
if v.CollectionName != "" {
484-
out[i].CollectionName = types.StringValue(v.CollectionName)
494+
if v.GetCollectionName() != "" {
495+
out[i].CollectionName = types.StringValue(v.GetCollectionName())
485496
}
486497
}
487498

488499
return out
489500
}
490501

491-
func newMongoDBAtlasRoles(roles []*tfRoleModel) []matlas.Role {
502+
func newMongoDBAtlasRoles(roles []*tfRoleModel) []admin.DatabaseUserRole {
492503
if len(roles) == 0 {
493-
return []matlas.Role{}
504+
return []admin.DatabaseUserRole{}
494505
}
495506

496-
out := make([]matlas.Role, len(roles))
507+
out := make([]admin.DatabaseUserRole, len(roles))
497508
for i, v := range roles {
498-
out[i] = matlas.Role{
509+
out[i] = admin.DatabaseUserRole{
499510
RoleName: v.RoleName.ValueString(),
500511
DatabaseName: v.DatabaseName.ValueString(),
501-
CollectionName: v.CollectionName.ValueString(),
512+
CollectionName: v.CollectionName.ValueStringPointer(),
502513
}
503514
}
504515

505516
return out
506517
}
507518

508-
func newMongoDBAtlasLabels(labels []*tfLabelModel) []matlas.Label {
519+
func newMongoDBAtlasLabels(labels []*tfLabelModel) []admin.ComponentLabel {
509520
if len(labels) == 0 {
510-
return []matlas.Label{}
521+
return []admin.ComponentLabel{}
511522
}
512523

513-
out := make([]matlas.Label, len(labels))
524+
out := make([]admin.ComponentLabel, len(labels))
514525
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(),
518529
}
519530
}
520531

521532
return out
522533
}
523534

524-
func newMongoDBAtlasScopes(scopes []*tfScopeModel) []matlas.Scope {
535+
func newMongoDBAtlasScopes(scopes []*tfScopeModel) []admin.UserScope {
525536
if len(scopes) == 0 {
526-
return []matlas.Scope{}
537+
return []admin.UserScope{}
527538
}
528539

529-
out := make([]matlas.Scope, len(scopes))
540+
out := make([]admin.UserScope, len(scopes))
530541
for i, v := range scopes {
531-
out[i] = matlas.Scope{
542+
out[i] = admin.UserScope{
532543
Name: v.Name.ValueString(),
533544
Type: v.Type.ValueString(),
534545
}

0 commit comments

Comments
 (0)