Skip to content

Commit 78ab564

Browse files
committed
feat: Added organization role support
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 1c11053 commit 78ab564

16 files changed

+1026
-2
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubOrganizationRole() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubOrganizationRoleRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"role_id": {
16+
Description: "The ID of the role.",
17+
Type: schema.TypeInt,
18+
Required: true,
19+
},
20+
"name": {
21+
Description: "The name of the role.",
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
25+
"description": {
26+
Description: "A short description about who this role is for or what permissions it grants.",
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
"source": {
31+
Description: "Source answers the question, \"where did this role come from?\"",
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"base_role": {
36+
Description: "The system role from which this role inherits permissions.",
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
"permissions": {
41+
Description: "A list of permissions included in this role.",
42+
Type: schema.TypeSet,
43+
Elem: &schema.Schema{Type: schema.TypeString},
44+
Computed: true,
45+
},
46+
},
47+
}
48+
}
49+
50+
func dataSourceGithubOrganizationRoleRead(d *schema.ResourceData, meta interface{}) error {
51+
client := meta.(*Owner).v3client
52+
ctx := context.Background()
53+
orgName := meta.(*Owner).name
54+
55+
roleId := int64(d.Get("role_id").(int))
56+
57+
role, _, err := client.Organizations.GetOrgRole(ctx, orgName, roleId)
58+
if err != nil {
59+
return err
60+
}
61+
62+
r := map[string]any{
63+
"role_id": role.GetID(),
64+
"name": role.GetName(),
65+
"description": role.GetDescription(),
66+
"source": role.GetSource(),
67+
"base_role": role.GetBaseRole(),
68+
"permissions": role.Permissions,
69+
}
70+
71+
d.SetId(strconv.FormatInt(role.GetID(), 10))
72+
73+
for k, v := range r {
74+
if err := d.Set(k, v); err != nil {
75+
return err
76+
}
77+
}
78+
79+
return nil
80+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/google/go-github/v66/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubOrganizationRoleTeamsRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"teams": {
17+
Description: "List of teams assigned to the organization role.",
18+
Type: schema.TypeList,
19+
Computed: true,
20+
Elem: &schema.Resource{
21+
Schema: map[string]*schema.Schema{
22+
"id": {
23+
Description: "Unique identifier of the team.",
24+
Type: schema.TypeInt,
25+
Computed: true,
26+
},
27+
"slug": {
28+
Description: "Slug of the team name.",
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"name": {
33+
Description: "Name of the team.",
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"permission": {
38+
Description: "Permission that the team will have for its repositories.",
39+
Type: schema.TypeString,
40+
Computed: true,
41+
},
42+
},
43+
},
44+
},
45+
},
46+
}
47+
}
48+
49+
func dataSourceGithubOrganizationRoleTeamsRead(d *schema.ResourceData, meta interface{}) error {
50+
client := meta.(*Owner).v3client
51+
ctx := context.Background()
52+
orgName := meta.(*Owner).name
53+
54+
roleId := int64(d.Get("role_id").(int))
55+
56+
allTeams := make([]any, 0)
57+
58+
opts := &github.ListOptions{
59+
PerPage: maxPerPage,
60+
}
61+
62+
for {
63+
teams, resp, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, orgName, roleId, opts)
64+
if err != nil {
65+
return err
66+
}
67+
68+
for _, team := range teams {
69+
t := map[string]any{
70+
"id": team.GetID(),
71+
"slug": team.GetSlug(),
72+
"name": team.GetName(),
73+
"permission": team.GetPermission(),
74+
}
75+
allTeams = append(allTeams, t)
76+
}
77+
78+
if resp.NextPage == 0 {
79+
break
80+
}
81+
opts.Page = resp.NextPage
82+
}
83+
84+
d.SetId(fmt.Sprintf("%d", roleId))
85+
if err := d.Set("teams", allTeams); err != nil {
86+
return fmt.Errorf("error setting teams: %s", err)
87+
}
88+
89+
return nil
90+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package github
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGithubOrganizationRole(t *testing.T) {
10+
t.Run("get the organization role without error", func(t *testing.T) {
11+
config := `
12+
data "github_organization_role" "test" {
13+
role_id = 138
14+
}
15+
`
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { skipUnlessMode(t, organization) },
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: config,
23+
Check: resource.ComposeTestCheckFunc(
24+
resource.TestCheckResourceAttr("data.github_organization_role.test", "role_id", "138"),
25+
resource.TestCheckResourceAttr("data.github_organization_role.test", "name", "security_manager"),
26+
resource.TestCheckResourceAttr("data.github_organization_role.test", "source", "Predefined"),
27+
resource.TestCheckResourceAttr("data.github_organization_role.test", "base_role", "read"),
28+
resource.TestCheckResourceAttrSet("data.github_organization_role.test", "description"),
29+
resource.TestCheckResourceAttrSet("data.github_organization_role.test", "permissions.#"),
30+
),
31+
},
32+
},
33+
})
34+
})
35+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/google/go-github/v66/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubOrganizationRoleUsersRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"users": {
17+
Description: "List of users assigned to the organization role.",
18+
Type: schema.TypeList,
19+
Computed: true,
20+
Elem: &schema.Resource{
21+
Schema: map[string]*schema.Schema{
22+
"id": {
23+
Description: "Unique identifier of the user.",
24+
Type: schema.TypeInt,
25+
Computed: true,
26+
},
27+
"login": {
28+
Description: "Login for the user.",
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"type": {
33+
Description: "Determines if the user has a direct, indirect, or mixed relationship to a role.",
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceGithubOrganizationRoleUsersRead(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*Owner).v3client
46+
ctx := context.Background()
47+
orgName := meta.(*Owner).name
48+
49+
roleId := int64(d.Get("role_id").(int))
50+
51+
allUsers := make([]any, 0)
52+
53+
opts := &github.ListOptions{
54+
PerPage: maxPerPage,
55+
}
56+
57+
for {
58+
users, resp, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, orgName, roleId, opts)
59+
if err != nil {
60+
return err
61+
}
62+
63+
for _, user := range users {
64+
u := map[string]any{
65+
"id": user.GetID(),
66+
"login": user.GetLogin(),
67+
"type": user.GetType(),
68+
}
69+
allUsers = append(allUsers, u)
70+
}
71+
72+
if resp.NextPage == 0 {
73+
break
74+
}
75+
opts.Page = resp.NextPage
76+
}
77+
78+
d.SetId(fmt.Sprintf("%d", roleId))
79+
if err := d.Set("users", allUsers); err != nil {
80+
return fmt.Errorf("error setting users: %s", err)
81+
}
82+
83+
return nil
84+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package github
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubOrganizationRoles() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubOrganizationRolesRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"roles": {
16+
Description: "The list of organization roles available to the organization.",
17+
Type: schema.TypeList,
18+
Computed: true,
19+
Elem: &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"id": {
22+
Description: "The unique identifier of the role.",
23+
Type: schema.TypeInt,
24+
Computed: true,
25+
},
26+
"name": {
27+
Description: "The name of the role.",
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"description": {
32+
Description: "A short description about who this role is for or what permissions it grants.",
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"source": {
37+
Description: "Source answers the question, \"where did this role come from?\"",
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"base_role": {
42+
Description: "The system role from which this role inherits permissions.",
43+
Type: schema.TypeString,
44+
Computed: true,
45+
},
46+
"permissions": {
47+
Description: "A list of permissions included in this role.",
48+
Type: schema.TypeSet,
49+
Elem: &schema.Schema{Type: schema.TypeString},
50+
Computed: true,
51+
},
52+
},
53+
},
54+
},
55+
},
56+
}
57+
}
58+
59+
func dataSourceGithubOrganizationRolesRead(d *schema.ResourceData, meta interface{}) error {
60+
client := meta.(*Owner).v3client
61+
ctx := context.Background()
62+
orgName := meta.(*Owner).name
63+
64+
ret, _, err := client.Organizations.ListRoles(ctx, orgName)
65+
if err != nil {
66+
return err
67+
}
68+
69+
allRoles := make([]any, ret.GetTotalCount())
70+
for _, role := range ret.CustomRepoRoles {
71+
r := map[string]any{
72+
"id": role.GetID(),
73+
"name": role.GetName(),
74+
"description": role.GetDescription(),
75+
"source": role.GetSource(),
76+
"base_role": role.GetBaseRole(),
77+
"permissions": role.Permissions,
78+
}
79+
allRoles = append(allRoles, r)
80+
}
81+
82+
d.SetId(fmt.Sprintf("%s/github-org-roles", orgName))
83+
if err := d.Set("roles", allRoles); err != nil {
84+
return fmt.Errorf("error setting roles: %s", err)
85+
}
86+
87+
return nil
88+
}

0 commit comments

Comments
 (0)