Skip to content

Commit

Permalink
Fix: cannot create env0_user_team_assignment: validation error (#682)
Browse files Browse the repository at this point in the history
* Fix: cannot create env0_user_team_assignment: validation error

* added a comment

* fixed some minor issues based on CR comments
  • Loading branch information
TomerHeber committed Jul 24, 2023
1 parent 9a65c08 commit 5e8a7a1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions env0/resource_user_team_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"strings"
"sync"

"github.com/env0/terraform-provider-env0/client"
Expand Down Expand Up @@ -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 <user_id>_<team_id>", id)
}
Expand Down
30 changes: 30 additions & 0 deletions env0/resource_user_team_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions env0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]}
}
8 changes: 8 additions & 0 deletions env0/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "_"))
}

0 comments on commit 5e8a7a1

Please sign in to comment.