bmc-toolbox/bmclib

support changing a user's name with the new interfaces

jacobweinstock opened this issue · 0 comments

with the new interfaces in ./bmc, In order to update an existing user's name, the client would need to call a combination of CreateUser and DeleteUser. I propose we add a helper function in user.go to handle this for the client.

I think the code below will cover it. I just need to do and add some tests and then I'll open a PR. Also, trying to get better at creating an issue before a PR :)

// UpdateUserNameFromInterfaces is a helper function that will update a user's name by calling delete and create
func UpdateUserNameFromInterfaces(ctx context.Context, currentUsername, newUsername, pass, role string, generic []interface{}) (ok bool, err error) {
	ok, err = CreateUserFromInterfaces(ctx, newUsername, pass, role, generic)
	if err != nil {
		return false, err
	}
	if !ok {
		return false, fmt.Errorf("updating username was NOT successful: creating user: %v, failed: reason unknown", newUsername)
	}
	ok, err = DeleteUserFromInterfaces(ctx, currentUsername, generic)
	if err != nil {
		ok, err = DeleteUserFromInterfaces(ctx, newUsername, generic)
		if err != nil {
			return false, multierror.Append(err, err)
		}
		if !ok {
			err = multierror.Append(err, fmt.Errorf("updating username was NOT successful: delete newly created user: %v, failed: reason unknown", newUsername))
		}
		return false, err
	}
	if !ok {
		return false, fmt.Errorf("updating username was NOT successful: delete user: %v, failed: reason unknown", currentUsername)
	}
	return ok, nil
}