Skip to content

Commit

Permalink
datasource/gitlab_group_membership: support pagination. Closes #466
Browse files Browse the repository at this point in the history
Closes #466
  • Loading branch information
timofurrer committed Feb 9, 2022
1 parent 88f82db commit 2358c79
Show file tree
Hide file tree
Showing 2 changed files with 95 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
74 changes: 74 additions & 0 deletions internal/provider/data_source_gitlab_group_membership_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package provider

import (
"bytes"
"fmt"
"strings"
"testing"
"text/template"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -44,6 +47,26 @@ func TestAccDataSourceGitlabMembership_basic(t *testing.T) {
})
}

func TestAccDataSourceGitlabMembership_pagination(t *testing.T) {
rInt := acctest.RandInt()
userCount := 21

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
// Create the group and one member
{
Config: testAccDataSourceGitlabGroupMembershipPagination(rInt, userCount),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("gitlab_group.this", "name", fmt.Sprintf("foo-%d", rInt)),
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 +112,54 @@ data "gitlab_group_membership" "foomaintainers" {
access_level = "maintainer"
}`, rInt, rInt)
}

func testAccDataSourceGitlabGroupMembershipPagination(rInt int, userCount int) string {
t, err := template.New("users").Parse(fmt.Sprintf(`
{{ range $val := . }}
resource "gitlab_user" "test_{{ $val }}" {
name = "foo-%d-{{ $val }}"
username = "foo-%d-{{ $val }}"
password = "foo-%d-{{ $val }}"
email = "foo-%d-{{ $val }}@test.com"
}
resource "gitlab_group_membership" "this_{{ $val }}" {
group_id = "${gitlab_group.this.id}"
user_id = gitlab_user.test_{{ $val }}.id
access_level = "developer"
}
{{ end }}`, rInt, rInt, rInt, rInt))

if err != nil {
// lintignore: R009 // absolutely fine here in the tests
panic(err)
}

var users []int
var usersDependsOnList []string
for i := 1; i <= userCount; i++ {
users = append(users, i)
usersDependsOnList = append(usersDependsOnList, fmt.Sprintf("gitlab_group_membership.this_%d", i))
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, users); err != nil {
// lintignore: R009 // absolutely fine here in the tests
panic(err)
}

return fmt.Sprintf(`
resource "gitlab_group" "this" {
name = "foo-%d"
path = "foo-%d"
}
%s
data "gitlab_group_membership" "this" {
group_id = "${gitlab_group.this.id}"
access_level = "developer"
depends_on = [%s]
}`, rInt, rInt, tpl.String(), strings.Join(usersDependsOnList, ", "))
}

0 comments on commit 2358c79

Please sign in to comment.