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_repository_role.go
More file actions
95 lines (81 loc) · 2.39 KB
/
data_source_github_organization_repository_role.go
File metadata and controls
95 lines (81 loc) · 2.39 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
package github
import (
"context"
"fmt"
"strconv"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
return &schema.Resource{
Description: "Lookup a custom organization repository role.",
ReadContext: dataSourceGithubOrganizationRepositoryRoleRead,
Schema: map[string]*schema.Schema{
"role_id": {
Description: "The ID of the organization repository role.",
Type: schema.TypeInt,
Required: true,
},
"name": {
Description: "The name of the organization repository role.",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "The description of the organization repository role.",
Type: schema.TypeString,
Computed: true,
},
"base_role": {
Description: "The system role from which this role inherits permissions.",
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Description: "The permissions included in this role.",
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}
func dataSourceGithubOrganizationRepositoryRoleRead(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))
// TODO: Use this code when go-github is at v68+
// role, _, err := client.Organizations.GetCustomRepoRole(ctx, orgName, roleId)
// if err != nil {
// return diag.FromErr(err)
// }
roles, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
if err != nil {
return diag.FromErr(err)
}
var role *github.CustomRepoRoles
for _, r := range roles.CustomRepoRoles {
if r.GetID() == roleId {
role = r
break
}
}
if role == nil {
return diag.FromErr(fmt.Errorf("custom organization repo role with ID %d not found", roleId))
}
r := map[string]any{
"role_id": role.GetID(),
"name": role.GetName(),
"description": role.GetDescription(),
"base_role": role.GetBaseRole(),
"permissions": role.Permissions,
}
d.SetId(strconv.FormatInt(role.GetID(), 10))
for k, v := range r {
if err := d.Set(k, v); err != nil {
return diag.FromErr(err)
}
}
return nil
}