Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix databricks_user creation with force on account #1577

Merged
merged 2 commits into from
Sep 2, 2022
Merged
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
12 changes: 9 additions & 3 deletions scim/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func userExistsErrorMessage(userName string, isAccount bool) string {
if isAccount {
return fmt.Sprintf("User with email %s already exists in this account", userName)
} else {
return fmt.Sprintf("User with username %s already exists.", userName)
}
}

// ResourceUser manages users within workspace
func ResourceUser() *schema.Resource {
type entity struct {
Expand Down Expand Up @@ -85,9 +93,7 @@ func createForceOverridesManuallyAddedUser(err error, d *schema.ResourceData, us
}
// corner-case for overriding manually provisioned users
userName := strings.ReplaceAll(u.UserName, "'", "")
force := fmt.Sprintf("User with username %s already exists.", userName)
force_account := "User already exists in another account"
if (err.Error() != force) && (err.Error() != force_account) {
if (err.Error() != userExistsErrorMessage(userName, false)) && (err.Error() != userExistsErrorMessage(userName, true)) {
return err
}
userList, err := usersAPI.Filter(fmt.Sprintf("userName eq '%s'", userName))
Expand Down
8 changes: 4 additions & 4 deletions scim/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func TestCreateForceOverwriteCannotListUsers(t *testing.T) {
d := ResourceUser().TestResourceData()
d.Set("force", true)
err := createForceOverridesManuallyAddedUser(
fmt.Errorf("User with username me@example.com already exists."),
fmt.Errorf(userExistsErrorMessage("me@example.com", false)),
d, NewUsersAPI(ctx, client), User{
UserName: "me@example.com",
})
Expand All @@ -480,7 +480,7 @@ func TestCreateForceOverwriteCannotListAccUsers(t *testing.T) {
d := ResourceUser().TestResourceData()
d.Set("force", true)
err := createForceOverridesManuallyAddedUser(
fmt.Errorf("User already exists in another account"),
fmt.Errorf(userExistsErrorMessage("me@example.com", true)),
d, NewUsersAPI(ctx, client), User{
UserName: "me@example.com",
})
Expand Down Expand Up @@ -521,7 +521,7 @@ func TestCreateForceOverwriteFindsAndSetsID(t *testing.T) {
d.Set("force", true)
d.Set("user_name", "me@example.com")
err := createForceOverridesManuallyAddedUser(
fmt.Errorf("User with username me@example.com already exists."),
fmt.Errorf(userExistsErrorMessage("me@example.com", false)),
d, NewUsersAPI(ctx, client), User{
UserName: "me@example.com",
})
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestCreateForceOverwriteFindsAndSetsAccID(t *testing.T) {
d.Set("force", true)
d.Set("user_name", "me@example.com")
err := createForceOverridesManuallyAddedUser(
fmt.Errorf("User already exists in another account"),
fmt.Errorf(userExistsErrorMessage("me@example.com", true)),
d, NewUsersAPI(ctx, client), User{
UserName: "me@example.com",
})
Expand Down