Skip to content

Commit

Permalink
Merge pull request #858 from timofurrer/datasources/pagination
Browse files Browse the repository at this point in the history
datasource/gitlab_group_membership: support pagination. Closes #466
  • Loading branch information
timofurrer authored Feb 11, 2022
2 parents a4a701a + 308dbb2 commit 2dea817
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/provider/data_source_gitlab_group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down
29 changes: 29 additions & 0 deletions internal/provider/data_source_gitlab_group_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}

0 comments on commit 2dea817

Please sign in to comment.