Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions mongodb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,26 @@ func (resource Resource) String() string {
}

func createUser(client *mongo.Client, user DbUser, roles []Role, database string) error {
return writeUser(client, user, roles, database, "createUser")
}

func updateUser(client *mongo.Client, user DbUser, roles []Role, database string) error {
return writeUser(client, user, roles, database, "updateUser")
}

func writeUser(client *mongo.Client, user DbUser, roles []Role, database string, command string) error {
var result *mongo.SingleResult
var args = bson.D{}
args = append(args, bson.E{Key: command, Value: user.Name})
if user.Password != "" {
args = append(args, bson.E{Key: "pwd", Value: user.Password})
}
if len(roles) != 0 {
result = client.Database(database).RunCommand(context.Background(), bson.D{{Key: "createUser", Value: user.Name},
{Key: "pwd", Value: user.Password}, {Key: "roles", Value: roles}})
args = append(args, bson.E{Key: "roles", Value: roles})
} else {
result = client.Database(database).RunCommand(context.Background(), bson.D{{Key: "createUser", Value: user.Name},
{Key: "pwd", Value: user.Password}, {Key: "roles", Value: []bson.M{}}})
args = append(args, bson.E{Key: "roles", Value: []bson.M{}})
}

result = client.Database(database).RunCommand(context.Background(), args)
if result.Err() != nil {
return result.Err()
}
Expand Down
24 changes: 8 additions & 16 deletions mongodb/resource_db_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,22 @@ func resourceDatabaseUserUpdate(ctx context.Context, data *schema.ResourceData,
var database = data.Get("auth_database").(string)
var userPassword = data.Get("password").(string)

adminDB := client.Database(database)

result := adminDB.RunCommand(context.Background(), bson.D{{Key: "dropUser", Value: userName}})
if result.Err() != nil {
return diag.Errorf("%s", result.Err())
}
var roleList []Role
var user = DbUser{
Name: userName,
Password: userPassword,
}
roles := data.Get("role").(*schema.Set).List()
roleMapErr := mapstructure.Decode(roles, &roleList)
if roleMapErr != nil {
return diag.Errorf("Error decoding map : %s ", roleMapErr)
}
err2 := createUser(client, user, roleList, database)
if err2 != nil {
return diag.Errorf("Could not create the user : %s ", err2)

var user = DbUser{
Name: userName,
Password: userPassword,
}

newId := database + "." + userName
encoded := base64.StdEncoding.EncodeToString([]byte(newId))
data.SetId(encoded)
err := updateUser(client, user, roleList, database)
if err != nil {
return diag.Errorf("Could not update the user : %s ", err)
}
return resourceDatabaseUserRead(ctx, data, i)
}

Expand Down