Skip to content

Commit 6baa767

Browse files
committed
Allow to filter members by their access_level
1 parent f8873df commit 6baa767

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

gitlab/data_source_gitlab_group_membership.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ package gitlab
33
import (
44
"fmt"
55
"log"
6+
"strconv"
7+
"strings"
68

79
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
810
"github.com/xanzy/go-gitlab"
911
)
1012

1113
func dataSourceGitlabGroupMembership() *schema.Resource {
14+
acceptedAccessLevels := make([]string, 0, len(accessLevelID))
15+
for k := range accessLevelID {
16+
acceptedAccessLevels = append(acceptedAccessLevels, k)
17+
}
1218
return &schema.Resource{
1319
Read: dataSourceGitlabGroupMembershipRead,
1420
Schema: map[string]*schema.Schema{
@@ -28,6 +34,12 @@ func dataSourceGitlabGroupMembership() *schema.Resource {
2834
"group_id",
2935
},
3036
},
37+
"access_level": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Optional: true,
41+
ValidateFunc: validateValueFunc(acceptedAccessLevels),
42+
},
3143
"members": {
3244
Type: schema.TypeList,
3345
Computed: true,
@@ -111,16 +123,34 @@ func dataSourceGitlabGroupMembershipRead(d *schema.ResourceData, meta interface{
111123
d.Set("group_id", group.ID)
112124
d.Set("full_path", group.FullPath)
113125

114-
d.Set("members", flattenGitlabMembers(gm))
115-
d.SetId(fmt.Sprintf("%d", group.ID))
126+
d.Set("members", flattenGitlabMembers(d, gm))
127+
128+
var optionsHash strings.Builder
129+
optionsHash.WriteString(strconv.Itoa(group.ID))
130+
131+
if data, ok := d.GetOk("access_level"); ok {
132+
optionsHash.WriteString(data.(string))
133+
}
134+
135+
id := schema.HashString(optionsHash.String())
136+
d.SetId(fmt.Sprintf("%d", id))
116137

117138
return nil
118139
}
119140

120-
func flattenGitlabMembers(members []*gitlab.GroupMember) []interface{} {
141+
func flattenGitlabMembers(d *schema.ResourceData, members []*gitlab.GroupMember) []interface{} {
121142
membersList := []interface{}{}
122143

144+
var filterAccessLevel gitlab.AccessLevelValue = gitlab.NoPermissions
145+
if data, ok := d.GetOk("access_level"); ok {
146+
filterAccessLevel = accessLevelID[data.(string)]
147+
}
148+
123149
for _, member := range members {
150+
if filterAccessLevel != gitlab.NoPermissions && filterAccessLevel != member.AccessLevel {
151+
continue
152+
}
153+
124154
values := map[string]interface{}{
125155
"id": member.ID,
126156
"username": member.Username,

gitlab/data_source_gitlab_group_membership_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ func TestAccDataSourceGitlabMembership_basic(t *testing.T) {
3232
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.1.username", fmt.Sprintf("listest%d", rInt)),
3333
),
3434
},
35+
36+
// Get group using its ID, but return maintainers only
37+
{
38+
Config: testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt),
39+
Check: resource.ComposeTestCheckFunc(
40+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foomaintainers", "members.#", "0"),
41+
),
42+
},
3543
},
3644
})
3745
}
@@ -68,3 +76,16 @@ data "gitlab_group_membership" "foo" {
6876
group_id = "${gitlab_group.foo.id}"
6977
}`, rInt, rInt)
7078
}
79+
80+
func testAccDataSourceGitlabGroupMembershipConfigFilterAccessLevel(rInt int) string {
81+
return fmt.Sprintf(`
82+
resource "gitlab_group" "foo" {
83+
name = "foo%d"
84+
path = "foo%d"
85+
}
86+
87+
data "gitlab_group_membership" "foomaintainers" {
88+
group_id = "${gitlab_group.foo.id}"
89+
access_level = "maintainer"
90+
}`, rInt, rInt)
91+
}

website/docs/d/group_membership.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The following arguments are supported:
3636

3737
* `full_path` - (Optional) The full path of the group.
3838

39+
* `access_level` - (Optional) Only return members with the desidered access level. Acceptable values are: `guest`, `reporter`, `developer`, `maintainer`, `owner`.
40+
3941
**Note**: exactly one of group_id or full_path must be provided.
4042

4143
## Attributes Reference

0 commit comments

Comments
 (0)