Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datasource/gitlab_group_membership: support pagination. Closes #466 #858

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
testAccPreCheck(t)
timofurrer marked this conversation as resolved.
Show resolved Hide resolved

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)
}