From 5e8a7a189b5b176fedd48917da661b6967ea9864 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Mon, 24 Jul 2023 10:03:46 -0500 Subject: [PATCH] Fix: cannot create env0_user_team_assignment: validation error (#682) * Fix: cannot create env0_user_team_assignment: validation error * added a comment * fixed some minor issues based on CR comments --- env0/resource_user_team_assignment.go | 4 +-- env0/resource_user_team_assignment_test.go | 30 ++++++++++++++++++++++ env0/utils.go | 9 +++++++ env0/utils_test.go | 8 ++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/env0/resource_user_team_assignment.go b/env0/resource_user_team_assignment.go index 52b98f9d..29b253f0 100644 --- a/env0/resource_user_team_assignment.go +++ b/env0/resource_user_team_assignment.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "strings" "sync" "github.com/env0/terraform-provider-env0/client" @@ -34,7 +33,8 @@ func (a *UserTeamAssignment) GetId() string { } func GetUserTeamAssignmentFromId(id string) (*UserTeamAssignment, error) { - splitUserTeam := strings.Split(id, "_") + // lastSplit is used to avoid issues where the user_id has underscores in it. + splitUserTeam := lastSplit(id, "_") if len(splitUserTeam) != 2 { return nil, fmt.Errorf("the id %v is invalid must be _", id) } diff --git a/env0/resource_user_team_assignment_test.go b/env0/resource_user_team_assignment_test.go index 2286c7f5..6a179e60 100644 --- a/env0/resource_user_team_assignment_test.go +++ b/env0/resource_user_team_assignment_test.go @@ -16,6 +16,8 @@ func TestUnitUserTeamAssignmentResource(t *testing.T) { userId := "uid" teamId := "tid" + userIdWithUnderscore := "u_id_c" + GenerateTeam := func(teamId string, userIds []string) client.Team { team := client.Team{ Id: teamId, @@ -198,6 +200,34 @@ func TestUnitUserTeamAssignmentResource(t *testing.T) { }) }) + t.Run("Import Assignment - user id with underscore", func(t *testing.T) { + testCase := resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ + "user_id": userIdWithUnderscore, + "team_id": teamId, + }), + }, + { + ResourceName: resourceType + "." + resourceName, + ImportState: true, + ImportStateId: userIdWithUnderscore + "_" + teamId, + ImportStateVerify: true, + }, + }, + } + + runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + gomock.InOrder( + mock.EXPECT().Team(teamId).Times(1).Return(GenerateTeam(teamId, []string{"otherId"}), nil), + mock.EXPECT().TeamUpdate(teamId, GenerateUpdateTeamPayload([]string{userIdWithUnderscore, "otherId"})).Times(1).Return(client.Team{}, nil), + mock.EXPECT().Team(teamId).Times(4).Return(GenerateTeam(teamId, []string{userIdWithUnderscore, "otherId"}), nil), + mock.EXPECT().TeamUpdate(teamId, GenerateUpdateTeamPayload([]string{"otherId"})).Times(1).Return(client.Team{}, nil), + ) + }) + }) + t.Run("Import Assignment - invalid id", func(t *testing.T) { testCase := resource.TestCase{ Steps: []resource.TestStep{ diff --git a/env0/utils.go b/env0/utils.go index d36a840a..2b95de1d 100644 --- a/env0/utils.go +++ b/env0/utils.go @@ -489,3 +489,12 @@ func ttlToDuration(ttl *string) (time.Duration, error) { return time.ParseDuration(fmt.Sprintf("%dh", hours)) } + +func lastSplit(s string, sep string) []string { + lastIndex := strings.LastIndex(s, sep) + if lastIndex == -1 { + return []string{s} + } + + return []string{s[:lastIndex], s[lastIndex+1:]} +} diff --git a/env0/utils_test.go b/env0/utils_test.go index 7ba631f3..c76f0051 100644 --- a/env0/utils_test.go +++ b/env0/utils_test.go @@ -459,3 +459,11 @@ func TestTTLToDuration(t *testing.T) { require.Error(t, err) }) } + +func TestLastSplit(t *testing.T) { + assert.Equal(t, []string{"a", "b"}, lastSplit("a_b", "_")) + assert.Equal(t, []string{"a__", "b"}, lastSplit("a___b", "_")) + assert.Equal(t, []string{"a_", ""}, lastSplit("a__", "_")) + + assert.Equal(t, []string{"abc"}, lastSplit("abc", "_")) +}