forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_organization_role_users.go
More file actions
103 lines (89 loc) · 2.68 KB
/
data_source_github_organization_role_users.go
File metadata and controls
103 lines (89 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package github
import (
"context"
"fmt"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
return &schema.Resource{
Description: "Lookup all users assigned to a custom organization role.",
ReadContext: dataSourceGithubOrganizationRoleUsersRead,
Schema: map[string]*schema.Schema{
"role_id": {
Description: "The ID of the organization role.",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"users": {
Description: "Users assigned to the organization role.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_id": {
Description: "The ID of the user.",
Type: schema.TypeInt,
Computed: true,
},
"login": {
Description: "The login for the GitHub user account.",
Type: schema.TypeString,
Computed: true,
},
// TODO: Add these fields when go-github is v68+
// See https://github.com/google/go-github/issues/3364
// "assignment": {
// Description: "Determines if the team has a direct, indirect, or mixed relationship to a role.",
// Type: schema.TypeString,
// Computed: true,
// },
// "parent_team_id": {
// Description: "The ID of the parent team if this is an indirect assignment.",
// Type: schema.TypeString,
// Computed: true,
// },
// "parent_team_slug": {
// Description: "The slug of the parent team if this is an indirect assignment.",
// Type: schema.TypeString,
// Computed: true,
// },
},
},
},
},
}
}
func dataSourceGithubOrganizationRoleUsersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
roleId := int64(d.Get("role_id").(int))
allUsers := make([]any, 0)
opts := &github.ListOptions{
PerPage: maxPerPage,
}
for {
users, resp, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, orgName, roleId, opts)
if err != nil {
return diag.FromErr(err)
}
for _, user := range users {
u := map[string]any{
"user_id": int(user.GetID()),
"login": user.GetLogin(),
}
allUsers = append(allUsers, u)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
d.SetId(fmt.Sprintf("%d", roleId))
if err := d.Set("users", allUsers); err != nil {
return diag.FromErr(fmt.Errorf("error setting users: %w", err))
}
return nil
}