Skip to content

Commit f8873df

Browse files
committed
Add new data source gitlab_group_membership
1 parent f60dc34 commit f8873df

File tree

4 files changed

+272
-5
lines changed

4 files changed

+272
-5
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func dataSourceGitlabGroupMembership() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGitlabGroupMembershipRead,
14+
Schema: map[string]*schema.Schema{
15+
"group_id": {
16+
Type: schema.TypeInt,
17+
Computed: true,
18+
Optional: true,
19+
ConflictsWith: []string{
20+
"full_path",
21+
},
22+
},
23+
"full_path": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
Optional: true,
27+
ConflictsWith: []string{
28+
"group_id",
29+
},
30+
},
31+
"members": {
32+
Type: schema.TypeList,
33+
Computed: true,
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
"id": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
},
40+
"username": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
44+
"name": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"state": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"avatar_url": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"web_url": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
"access_level": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
},
64+
"expires_at": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
},
69+
},
70+
},
71+
},
72+
}
73+
}
74+
75+
func dataSourceGitlabGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
76+
client := meta.(*gitlab.Client)
77+
78+
var gm []*gitlab.GroupMember
79+
var group *gitlab.Group
80+
var err error
81+
82+
log.Printf("[INFO] Reading Gitlab group")
83+
84+
groupIDData, groupIDOk := d.GetOk("group_id")
85+
fullPathData, fullPathOk := d.GetOk("full_path")
86+
87+
if groupIDOk {
88+
// Get group by id
89+
group, _, err = client.Groups.GetGroup(groupIDData.(int))
90+
if err != nil {
91+
return err
92+
}
93+
} else if fullPathOk {
94+
// Get group by full path
95+
group, _, err = client.Groups.GetGroup(fullPathData.(string))
96+
if err != nil {
97+
return err
98+
}
99+
} else {
100+
return fmt.Errorf("one and only one of group_id or full_path must be set")
101+
}
102+
103+
log.Printf("[INFO] Reading Gitlab group memberships")
104+
105+
// Get group memberships
106+
gm, _, err = client.Groups.ListGroupMembers(group.ID, &gitlab.ListGroupMembersOptions{})
107+
if err != nil {
108+
return err
109+
}
110+
111+
d.Set("group_id", group.ID)
112+
d.Set("full_path", group.FullPath)
113+
114+
d.Set("members", flattenGitlabMembers(gm))
115+
d.SetId(fmt.Sprintf("%d", group.ID))
116+
117+
return nil
118+
}
119+
120+
func flattenGitlabMembers(members []*gitlab.GroupMember) []interface{} {
121+
membersList := []interface{}{}
122+
123+
for _, member := range members {
124+
values := map[string]interface{}{
125+
"id": member.ID,
126+
"username": member.Username,
127+
"name": member.Name,
128+
"state": member.State,
129+
"avatar_url": member.AvatarURL,
130+
"web_url": member.WebURL,
131+
"access_level": accessLevel[gitlab.AccessLevelValue(member.AccessLevel)],
132+
}
133+
134+
if member.ExpiresAt != nil {
135+
values["expires_at"] = member.ExpiresAt.String()
136+
}
137+
138+
membersList = append(membersList, values)
139+
}
140+
141+
return membersList
142+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccDataSourceGitlabMembership_basic(t *testing.T) {
12+
rInt := acctest.RandInt()
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
// Create the group and one member
19+
{
20+
Config: testAccDataSourceGitlabGroupMembershipConfig(rInt),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr("gitlab_group.foo", "name", fmt.Sprintf("foo%d", rInt)),
23+
resource.TestCheckResourceAttr("gitlab_user.test", "name", fmt.Sprintf("foo%d", rInt)),
24+
resource.TestCheckResourceAttr("gitlab_group_membership.foo", "access_level", "developer"),
25+
),
26+
},
27+
{
28+
Config: testAccDataSourceGitlabGroupMembershipConfig_basic(rInt),
29+
Check: resource.ComposeTestCheckFunc(
30+
// Members is 2 because the user owning the token is always added to the group
31+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.#", "2"),
32+
resource.TestCheckResourceAttr("data.gitlab_group_membership.foo", "members.1.username", fmt.Sprintf("listest%d", rInt)),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccDataSourceGitlabGroupMembershipConfig(rInt int) string {
40+
return fmt.Sprintf(`
41+
resource "gitlab_group" "foo" {
42+
name = "foo%d"
43+
path = "foo%d"
44+
}
45+
46+
resource "gitlab_user" "test" {
47+
name = "foo%d"
48+
username = "listest%d"
49+
password = "test%dtt"
50+
email = "listest%[email protected]"
51+
}
52+
53+
resource "gitlab_group_membership" "foo" {
54+
group_id = "${gitlab_group.foo.id}"
55+
user_id = "${gitlab_user.test.id}"
56+
access_level = "developer"
57+
}`, rInt, rInt, rInt, rInt, rInt, rInt)
58+
}
59+
60+
func testAccDataSourceGitlabGroupMembershipConfig_basic(rInt int) string {
61+
return fmt.Sprintf(`
62+
resource "gitlab_group" "foo" {
63+
name = "foo%d"
64+
path = "foo%d"
65+
}
66+
67+
data "gitlab_group_membership" "foo" {
68+
group_id = "${gitlab_group.foo.id}"
69+
}`, rInt, rInt)
70+
}

gitlab/provider.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ func Provider() terraform.ResourceProvider {
5454
},
5555

5656
DataSourcesMap: map[string]*schema.Resource{
57-
"gitlab_group": dataSourceGitlabGroup(),
58-
"gitlab_project": dataSourceGitlabProject(),
59-
"gitlab_projects": dataSourceGitlabProjects(),
60-
"gitlab_user": dataSourceGitlabUser(),
61-
"gitlab_users": dataSourceGitlabUsers(),
57+
"gitlab_group": dataSourceGitlabGroup(),
58+
"gitlab_group_membership": dataSourceGitlabGroupMembership(),
59+
"gitlab_project": dataSourceGitlabProject(),
60+
"gitlab_projects": dataSourceGitlabProjects(),
61+
"gitlab_user": dataSourceGitlabUser(),
62+
"gitlab_users": dataSourceGitlabUsers(),
6263
},
6364

6465
ResourcesMap: map[string]*schema.Resource{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
layout: "gitlab"
3+
page_title: "GitLab: gitlab_group_membership"
4+
sidebar_current: "docs-gitlab-data-source-group-membership"
5+
description: |-
6+
Looks up gitlab group membership
7+
---
8+
9+
# gitlab\_group\_membership
10+
11+
Provides details about a list of group members in the gitlab provider. The results include id, username, name and more about the requested members.
12+
13+
## Example Usage
14+
15+
**By group's ID**
16+
17+
```hcl
18+
data "gitlab_group_membership" "example" {
19+
group_id = 123
20+
}
21+
```
22+
23+
**By group's full path**
24+
25+
```hcl
26+
data "gitlab_group_membership" "example" {
27+
full_path = "foo/bar"
28+
}
29+
```
30+
31+
## Argument Reference
32+
33+
The following arguments are supported:
34+
35+
* `group_id` - (Optional) The ID of the group.
36+
37+
* `full_path` - (Optional) The full path of the group.
38+
39+
**Note**: exactly one of group_id or full_path must be provided.
40+
41+
## Attributes Reference
42+
43+
The following attributes are exported:
44+
45+
* `members` - The list of group members.
46+
* `id` - The unique id assigned to the user by the gitlab server.
47+
* `username` - The username of the user.
48+
* `name` - The name of the user.
49+
* `state` - Whether the user is active or blocked.
50+
* `avatar_url` - The avatar URL of the user.
51+
* `web_url` - User's website URL.
52+
* `access_level` - One of five levels of access to the group.
53+
* `expires_at` - Expiration date for the group membership.
54+

0 commit comments

Comments
 (0)