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_custom_role.go
More file actions
91 lines (79 loc) · 2.13 KB
/
data_source_github_organization_custom_role.go
File metadata and controls
91 lines (79 loc) · 2.13 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
package github
import (
"context"
"fmt"
"log"
"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubOrganizationCustomRole() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "This data source is deprecated and will be removed in a future release. Use the github_organization_repository_role data source instead.",
Read: dataSourceGithubOrganizationCustomRoleRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"base_role": {
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceGithubOrganizationCustomRoleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name
err := checkOrganization(meta)
if err != nil {
return err
}
// ListCustomRepoRoles returns a list of all custom repository roles for an organization.
// There is an API endpoint for getting a single custom repository role, but is not
// implemented in the go-github library.
roleList, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
if err != nil {
return fmt.Errorf("error querying GitHub custom repository roles %s: %s", orgName, err)
}
var role *github.CustomRepoRoles
for _, r := range roleList.CustomRepoRoles {
if fmt.Sprint(*r.Name) == d.Get("name").(string) {
role = r
break
}
}
if role == nil {
log.Printf("[WARN] GitHub custom repository role (%s) not found.", d.Get("name").(string))
d.SetId("")
return nil
}
d.SetId(fmt.Sprint(*role.ID))
err = d.Set("name", role.Name)
if err != nil {
return err
}
err = d.Set("description", role.Description)
if err != nil {
return err
}
err = d.Set("base_role", role.BaseRole)
if err != nil {
return err
}
err = d.Set("permissions", role.Permissions)
if err != nil {
return err
}
return nil
}