Skip to content

Commit

Permalink
fix: Adds pagination in mongodbatlas_team resource & data source (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
maastha authored Dec 27, 2024
1 parent 768daba commit ff3064c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changelog/2919.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
data-source/mongodbatlas_team: Fixes pagination logic when retrieved users of a team
```

```release-note:bug
resource/mongodbatlas_team: Fixes pagination logic when retrieved users of a team
```
14 changes: 8 additions & 6 deletions internal/service/team/data_source_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"errors"
"fmt"

"go.mongodb.org/atlas-sdk/v20241113004/admin"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"

"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

func DataSource() *schema.Resource {
Expand Down Expand Up @@ -77,21 +78,22 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
}

if err := d.Set("team_id", team.GetId()); err != nil {
return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", d.Id(), err))
return diag.FromErr(fmt.Errorf(errorTeamSetting, "team_id", d.Id(), err))
}

if err := d.Set("name", team.GetName()); err != nil {
return diag.FromErr(fmt.Errorf(errorTeamSetting, "name", d.Id(), err))
}

users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, team.GetId()).Execute()
teamUsers, err := listAllTeamUsers(ctx, connV2, orgID, team.GetId())

if err != nil {
return diag.FromErr(fmt.Errorf(errorTeamRead, err))
}

usernames := []string{}
for i := range users.GetResults() {
usernames = append(usernames, users.GetResults()[i].GetUsername())
for i := range teamUsers {
usernames = append(usernames, teamUsers[i].GetUsername())
}

if err := d.Set("usernames", usernames); err != nil {
Expand Down
23 changes: 18 additions & 5 deletions internal/service/team/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"strings"
"time"

"go.mongodb.org/atlas-sdk/v20241113004/admin"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

const (
Expand Down Expand Up @@ -114,14 +117,15 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
return diag.FromErr(fmt.Errorf(errorTeamSetting, "team_id", teamID, err))
}

users, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute()
teamUsers, err := listAllTeamUsers(ctx, connV2, orgID, team.GetId())

if err != nil {
return diag.FromErr(fmt.Errorf(errorTeamRead, err))
}

usernames := []string{}
for i := range users.GetResults() {
usernames = append(usernames, users.GetResults()[i].GetUsername())
for i := range teamUsers {
usernames = append(usernames, teamUsers[i].GetUsername())
}

if err := d.Set("usernames", usernames); err != nil {
Expand All @@ -148,7 +152,8 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
}

if d.HasChange("usernames") {
existingUsers, _, err := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID).Execute()
existingUsers, err := listAllTeamUsers(ctx, connV2, orgID, teamID)

if err != nil {
return diag.FromErr(fmt.Errorf(errorTeamRead, err))
}
Expand Down Expand Up @@ -247,3 +252,11 @@ func getProjectIDByTeamID(ctx context.Context, connV2 *admin.APIClient, teamID s

return "", nil
}

func listAllTeamUsers(ctx context.Context, connV2 *admin.APIClient, orgID, teamID string) ([]admin.CloudAppUser, error) {
return dsschema.AllPages(ctx, func(ctx context.Context, pageNum int) (dsschema.PaginateResponse[admin.CloudAppUser], *http.Response, error) {
request := connV2.TeamsApi.ListTeamUsers(ctx, orgID, teamID)
request = request.PageNum(pageNum)
return request.Execute()
})
}
4 changes: 2 additions & 2 deletions internal/service/team/update_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

func UpdateTeamUsers(teamsAPI admin.TeamsApi, usersAPI admin.MongoDBCloudUsersApi, existingTeamUsers *admin.PaginatedAppUser, newUsernames []string, orgID, teamID string) error {
func UpdateTeamUsers(teamsAPI admin.TeamsApi, usersAPI admin.MongoDBCloudUsersApi, existingTeamUsers []admin.CloudAppUser, newUsernames []string, orgID, teamID string) error {
validNewUsers, err := ValidateUsernames(usersAPI, newUsernames)
if err != nil {
return err
}
usersToAdd, usersToRemove, err := GetChangesForTeamUsers(existingTeamUsers.GetResults(), validNewUsers)
usersToAdd, usersToRemove, err := GetChangesForTeamUsers(existingTeamUsers, validNewUsers)
if err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions internal/service/team/update_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"testing"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/team"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
"go.mongodb.org/atlas-sdk/v20241113004/mockadmin"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
"go.mongodb.org/atlas-sdk/v20241113004/mockadmin"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/team"
)

func TestGetChangesForTeamUsers(t *testing.T) {
Expand Down Expand Up @@ -96,7 +98,7 @@ func TestUpdateTeamUsers(t *testing.T) {
mockUsersApi.EXPECT().GetUserByUsername(mock.Anything, invaliduser1).Return(admin.GetUserByUsernameApiRequest{ApiService: mockUsersApi})
mockUsersApi.EXPECT().GetUserByUsernameExecute(mock.Anything).Return(nil, nil, errors.New("invalid username"))
},
existingTeamUsers: nil,
existingTeamUsers: &admin.PaginatedAppUser{Results: nil},
usernames: []string{invaliduser1},
expectError: require.Error,
},
Expand Down Expand Up @@ -153,7 +155,7 @@ func TestUpdateTeamUsers(t *testing.T) {
mockTeamsAPI := mockadmin.NewTeamsApi(t)
mockUsersAPI := mockadmin.NewMongoDBCloudUsersApi(t)
testCase.mockFuncExpectations(mockTeamsAPI, mockUsersAPI)
testCase.expectError(t, team.UpdateTeamUsers(mockTeamsAPI, mockUsersAPI, testCase.existingTeamUsers, testCase.usernames, "orgID", "teamID"))
testCase.expectError(t, team.UpdateTeamUsers(mockTeamsAPI, mockUsersAPI, testCase.existingTeamUsers.GetResults(), testCase.usernames, "orgID", "teamID"))
})
}
}

0 comments on commit ff3064c

Please sign in to comment.