-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test to database_user resource (#1738)
* move model methods to model file * public struct * initial tests * add rest of model tests * move model methods and add tests * move last model methods * typo * use asser
- Loading branch information
Showing
5 changed files
with
583 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
package databaseuser | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
func NewMongoDBDatabaseUser(ctx context.Context, dbUserModel *TfDatabaseUserModel) (*admin.CloudDatabaseUser, diag.Diagnostics) { | ||
var rolesModel []*TfRoleModel | ||
var labelsModel []*TfLabelModel | ||
var scopesModel []*TfScopeModel | ||
|
||
diags := dbUserModel.Roles.ElementsAs(ctx, &rolesModel, false) | ||
if diags.HasError() { | ||
return nil, diags | ||
} | ||
|
||
diags = dbUserModel.Labels.ElementsAs(ctx, &labelsModel, false) | ||
if diags.HasError() { | ||
return nil, diags | ||
} | ||
|
||
diags = dbUserModel.Scopes.ElementsAs(ctx, &scopesModel, false) | ||
if diags.HasError() { | ||
return nil, diags | ||
} | ||
|
||
return &admin.CloudDatabaseUser{ | ||
GroupId: dbUserModel.ProjectID.ValueString(), | ||
Username: dbUserModel.Username.ValueString(), | ||
Password: dbUserModel.Password.ValueStringPointer(), | ||
X509Type: dbUserModel.X509Type.ValueStringPointer(), | ||
AwsIAMType: dbUserModel.AWSIAMType.ValueStringPointer(), | ||
OidcAuthType: dbUserModel.OIDCAuthType.ValueStringPointer(), | ||
LdapAuthType: dbUserModel.LDAPAuthType.ValueStringPointer(), | ||
DatabaseName: dbUserModel.AuthDatabaseName.ValueString(), | ||
Roles: NewMongoDBAtlasRoles(rolesModel), | ||
Labels: NewMongoDBAtlasLabels(labelsModel), | ||
Scopes: NewMongoDBAtlasScopes(scopesModel), | ||
}, nil | ||
} | ||
|
||
func NewTfDatabaseUserModel(ctx context.Context, model *TfDatabaseUserModel, dbUser *admin.CloudDatabaseUser) (*TfDatabaseUserModel, diag.Diagnostics) { | ||
rolesSet, diagnostic := types.SetValueFrom(ctx, RoleObjectType, NewTFRolesModel(dbUser.Roles)) | ||
if diagnostic.HasError() { | ||
return nil, diagnostic | ||
} | ||
|
||
labelsSet, diagnostic := types.SetValueFrom(ctx, LabelObjectType, NewTFLabelsModel(dbUser.Labels)) | ||
if diagnostic.HasError() { | ||
return nil, diagnostic | ||
} | ||
|
||
scopesSet, diagnostic := types.SetValueFrom(ctx, ScopeObjectType, NewTFScopesModel(dbUser.Scopes)) | ||
if diagnostic.HasError() { | ||
return nil, diagnostic | ||
} | ||
|
||
// ID is encoded to preserve format defined in previous versions. | ||
encodedID := conversion.EncodeStateID(map[string]string{ | ||
"project_id": dbUser.GroupId, | ||
"username": dbUser.Username, | ||
"auth_database_name": dbUser.DatabaseName, | ||
}) | ||
databaseUserModel := &TfDatabaseUserModel{ | ||
ID: types.StringValue(encodedID), | ||
ProjectID: types.StringValue(dbUser.GroupId), | ||
AuthDatabaseName: types.StringValue(dbUser.DatabaseName), | ||
Username: types.StringValue(dbUser.Username), | ||
X509Type: types.StringValue(dbUser.GetX509Type()), | ||
OIDCAuthType: types.StringValue(dbUser.GetOidcAuthType()), | ||
LDAPAuthType: types.StringValue(dbUser.GetLdapAuthType()), | ||
AWSIAMType: types.StringValue(dbUser.GetAwsIAMType()), | ||
Roles: rolesSet, | ||
Labels: labelsSet, | ||
Scopes: scopesSet, | ||
} | ||
|
||
if model != nil && model.Password.ValueString() != "" { | ||
// The Password is not retuned from the endpoint so we use the one provided in the model | ||
databaseUserModel.Password = model.Password | ||
} | ||
|
||
return databaseUserModel, nil | ||
} | ||
|
||
func NewTFDatabaseDSUserModel(ctx context.Context, dbUser *admin.CloudDatabaseUser) (*TfDatabaseUserDSModel, diag.Diagnostics) { | ||
databaseID := fmt.Sprintf("%s-%s-%s", dbUser.GroupId, dbUser.Username, dbUser.DatabaseName) | ||
databaseUserModel := &TfDatabaseUserDSModel{ | ||
ID: types.StringValue(databaseID), | ||
ProjectID: types.StringValue(dbUser.GroupId), | ||
AuthDatabaseName: types.StringValue(dbUser.DatabaseName), | ||
Username: types.StringValue(dbUser.Username), | ||
Password: types.StringValue(dbUser.GetPassword()), | ||
X509Type: types.StringValue(dbUser.GetX509Type()), | ||
OIDCAuthType: types.StringValue(dbUser.GetOidcAuthType()), | ||
LDAPAuthType: types.StringValue(dbUser.GetLdapAuthType()), | ||
AWSIAMType: types.StringValue(dbUser.GetAwsIAMType()), | ||
Roles: NewTFRolesModel(dbUser.Roles), | ||
Labels: NewTFLabelsModel(dbUser.Labels), | ||
Scopes: NewTFScopesModel(dbUser.Scopes), | ||
} | ||
|
||
return databaseUserModel, nil | ||
} | ||
|
||
func NewTFDatabaseUsersModel(ctx context.Context, projectID string, dbUsers []admin.CloudDatabaseUser) (*TfDatabaseUsersDSModel, diag.Diagnostics) { | ||
results := make([]*TfDatabaseUserDSModel, len(dbUsers)) | ||
for i := range dbUsers { | ||
dbUserModel, d := NewTFDatabaseDSUserModel(ctx, &dbUsers[i]) | ||
if d.HasError() { | ||
return nil, d | ||
} | ||
results[i] = dbUserModel | ||
} | ||
|
||
return &TfDatabaseUsersDSModel{ | ||
ProjectID: types.StringValue(projectID), | ||
Results: results, | ||
ID: types.StringValue(id.UniqueId()), | ||
}, nil | ||
} | ||
|
||
func NewTFScopesModel(scopes []admin.UserScope) []TfScopeModel { | ||
if len(scopes) == 0 { | ||
return nil | ||
} | ||
|
||
out := make([]TfScopeModel, len(scopes)) | ||
for i, v := range scopes { | ||
out[i] = TfScopeModel{ | ||
Name: types.StringValue(v.Name), | ||
Type: types.StringValue(v.Type), | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func NewMongoDBAtlasLabels(labels []*TfLabelModel) []admin.ComponentLabel { | ||
if len(labels) == 0 { | ||
return []admin.ComponentLabel{} | ||
} | ||
|
||
out := make([]admin.ComponentLabel, len(labels)) | ||
for i, v := range labels { | ||
out[i] = admin.ComponentLabel{ | ||
Key: v.Key.ValueStringPointer(), | ||
Value: v.Value.ValueStringPointer(), | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func NewTFLabelsModel(labels []admin.ComponentLabel) []TfLabelModel { | ||
if len(labels) == 0 { | ||
return nil | ||
} | ||
|
||
out := make([]TfLabelModel, len(labels)) | ||
for i, v := range labels { | ||
out[i] = TfLabelModel{ | ||
Key: types.StringValue(v.GetKey()), | ||
Value: types.StringValue(v.GetValue()), | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func NewMongoDBAtlasScopes(scopes []*TfScopeModel) []admin.UserScope { | ||
if len(scopes) == 0 { | ||
return []admin.UserScope{} | ||
} | ||
|
||
out := make([]admin.UserScope, len(scopes)) | ||
for i, v := range scopes { | ||
out[i] = admin.UserScope{ | ||
Name: v.Name.ValueString(), | ||
Type: v.Type.ValueString(), | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func NewTFRolesModel(roles []admin.DatabaseUserRole) []TfRoleModel { | ||
if len(roles) == 0 { | ||
return nil | ||
} | ||
|
||
out := make([]TfRoleModel, len(roles)) | ||
for i, v := range roles { | ||
out[i] = TfRoleModel{ | ||
RoleName: types.StringValue(v.RoleName), | ||
DatabaseName: types.StringValue(v.DatabaseName), | ||
} | ||
|
||
if v.GetCollectionName() != "" { | ||
out[i].CollectionName = types.StringValue(v.GetCollectionName()) | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func NewMongoDBAtlasRoles(roles []*TfRoleModel) []admin.DatabaseUserRole { | ||
if len(roles) == 0 { | ||
return []admin.DatabaseUserRole{} | ||
} | ||
|
||
out := make([]admin.DatabaseUserRole, len(roles)) | ||
for i, v := range roles { | ||
out[i] = admin.DatabaseUserRole{ | ||
RoleName: v.RoleName.ValueString(), | ||
DatabaseName: v.DatabaseName.ValueString(), | ||
CollectionName: v.CollectionName.ValueStringPointer(), | ||
} | ||
} | ||
|
||
return out | ||
} |
Oops, something went wrong.