diff --git a/internal/provider/data_source_gitlab_group_membership.go b/internal/provider/data_source_gitlab_group_membership.go index 351a340f3..05a128e31 100644 --- a/internal/provider/data_source_gitlab_group_membership.go +++ b/internal/provider/data_source_gitlab_group_membership.go @@ -101,7 +101,6 @@ var _ = registerDataSource("gitlab_group_membership", func() *schema.Resource { func dataSourceGitlabGroupMembershipRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*gitlab.Client) - var gm []*gitlab.GroupMember var group *gitlab.Group var err error @@ -129,15 +128,32 @@ func dataSourceGitlabGroupMembershipRead(ctx context.Context, d *schema.Resource log.Printf("[INFO] Reading Gitlab group memberships") // Get group memberships - gm, _, err = client.Groups.ListGroupMembers(group.ID, &gitlab.ListGroupMembersOptions{}, gitlab.WithContext(ctx)) - if err != nil { - return diag.FromErr(err) + listOptions := &gitlab.ListGroupMembersOptions{ + ListOptions: gitlab.ListOptions{ + PerPage: 20, + Page: 1, + }, + } + + var allGms []*gitlab.GroupMember + for { + gms, resp, err := client.Groups.ListGroupMembers(group.ID, listOptions, gitlab.WithContext(ctx)) + if err != nil { + return diag.FromErr(err) + } + + allGms = append(allGms, gms...) + + if resp.NextPage == 0 { + break + } + listOptions.Page = resp.NextPage } d.Set("group_id", group.ID) d.Set("full_path", group.FullPath) - d.Set("members", flattenGitlabMembers(d, gm)) // lintignore: XR004 // TODO: Resolve this tfproviderlint issue + d.Set("members", flattenGitlabMembers(d, allGms)) // lintignore: XR004 // TODO: Resolve this tfproviderlint issue var optionsHash strings.Builder optionsHash.WriteString(strconv.Itoa(group.ID)) diff --git a/internal/provider/data_source_gitlab_group_membership_test.go b/internal/provider/data_source_gitlab_group_membership_test.go index 8f6676181..04cb0deea 100644 --- a/internal/provider/data_source_gitlab_group_membership_test.go +++ b/internal/provider/data_source_gitlab_group_membership_test.go @@ -44,6 +44,27 @@ func TestAccDataSourceGitlabMembership_basic(t *testing.T) { }) } +func TestAccDataSourceGitlabMembership_pagination(t *testing.T) { + testAccCheck(t) + + userCount := 21 + + group := testAccCreateGroups(t, 1)[0] + users := testAccCreateUsers(t, userCount) + testAccAddGroupMembers(t, group.ID, users) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGitlabGroupMembershipPagination(group.ID), + Check: resource.TestCheckResourceAttr("data.gitlab_group_membership.this", "members.#", fmt.Sprintf("%d", userCount)), + }, + }, + }) +} + func testAccDataSourceGitlabGroupMembershipConfig(rInt int) string { return fmt.Sprintf(` resource "gitlab_group" "foo" { @@ -89,3 +110,11 @@ data "gitlab_group_membership" "foomaintainers" { access_level = "maintainer" }`, rInt, rInt) } + +func testAccDataSourceGitlabGroupMembershipPagination(groupId int) string { + return fmt.Sprintf(` +data "gitlab_group_membership" "this" { + group_id = "%d" + access_level = "developer" +}`, groupId) +}