forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_organization_role.go
More file actions
191 lines (163 loc) · 5.28 KB
/
resource_github_organization_role.go
File metadata and controls
191 lines (163 loc) · 5.28 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubOrganizationRole() *schema.Resource {
return &schema.Resource{
Description: "Manage a custom organization role.",
CreateContext: resourceGithubOrganizationRoleCreate,
ReadContext: resourceGithubOrganizationRoleRead,
UpdateContext: resourceGithubOrganizationRoleUpdate,
DeleteContext: resourceGithubOrganizationRoleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"role_id": {
Description: "The ID of the organization role.",
Type: schema.TypeInt,
Computed: true,
},
"name": {
Description: "The name of the organization role.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "The description of the organization role.",
Type: schema.TypeString,
Optional: true,
},
"base_role": {
Description: "The system role from which this role inherits permissions.",
Type: schema.TypeString,
Optional: true,
Default: "none",
ValidateDiagFunc: validateValueFunc([]string{"none", "read", "triage", "write", "maintain", "admin"}),
},
"permissions": {
Description: "The permissions for the organization role.",
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceGithubOrganizationRoleCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
permissions := d.Get("permissions").(*schema.Set).List()
permissionsStr := make([]string, len(permissions))
for i, v := range permissions {
permissionsStr[i] = v.(string)
}
role, _, err := client.Organizations.CreateCustomOrgRole(ctx, orgName, &github.CreateOrUpdateOrgRoleOptions{
Name: github.String(d.Get("name").(string)),
Description: github.String(d.Get("description").(string)),
BaseRole: github.String(d.Get("base_role").(string)),
Permissions: permissionsStr,
})
if err != nil {
return diag.FromErr(fmt.Errorf("error creating organization role (%s/%s): %w", orgName, d.Get("name").(string), err))
}
d.SetId(fmt.Sprint(role.GetID()))
return nil
}
func resourceGithubOrganizationRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
roleId, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
role, _, err := client.Organizations.GetOrgRole(ctx, orgName, roleId)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[WARN] organization role (%s/%d) not found, removing from state", orgName, roleId)
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
if err = d.Set("role_id", role.GetID()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("name", role.Name); err != nil {
return diag.FromErr(err)
}
if err = d.Set("description", role.Description); err != nil {
return diag.FromErr(err)
}
if err = d.Set("base_role", role.BaseRole); err != nil {
return diag.FromErr(err)
}
if err = d.Set("permissions", role.Permissions); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubOrganizationRoleUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
roleId, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
permissions := d.Get("permissions").(*schema.Set).List()
permissionsStr := make([]string, len(permissions))
for i, v := range permissions {
permissionsStr[i] = v.(string)
}
update := &github.CreateOrUpdateOrgRoleOptions{
Name: github.String(d.Get("name").(string)),
Description: github.String(d.Get("description").(string)),
BaseRole: github.String(d.Get("base_role").(string)),
Permissions: permissionsStr,
}
_, _, err = client.Organizations.UpdateCustomOrgRole(ctx, orgName, roleId, update)
if err != nil {
return diag.FromErr(fmt.Errorf("error updating organization role (%s/%s): %w", orgName, d.Get("name").(string), err))
}
return nil
}
func resourceGithubOrganizationRoleDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
roleId, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
}
_, err = client.Organizations.DeleteCustomOrgRole(ctx, orgName, roleId)
if err != nil {
return diag.FromErr(fmt.Errorf("Error deleting organization role %d: %w", roleId, err))
}
return nil
}