-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathresource_github_actions_runner_group.go
More file actions
384 lines (340 loc) · 11.3 KB
/
resource_github_actions_runner_group.go
File metadata and controls
384 lines (340 loc) · 11.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubActionsRunnerGroup() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsRunnerGroupCreate,
Read: resourceGithubActionsRunnerGroupRead,
Update: resourceGithubActionsRunnerGroupUpdate,
Delete: resourceGithubActionsRunnerGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the runner group.",
},
"allows_public_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether public repositories can be added to the runner group.",
},
"default": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether this is the default runner group.",
},
"etag": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "An etag representing the runner group object",
DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool {
return true
},
DiffSuppressOnRefresh: true,
},
"inherited": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the runner group is inherited from the enterprise level",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the runner group.",
},
"runners_url": {
Type: schema.TypeString,
Computed: true,
Description: "The GitHub API URL for the runner group's runners.",
},
"selected_repository_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: schema.HashInt,
Optional: true,
Description: "List of repository IDs that can access the runner group.",
},
"selected_repositories_url": {
Type: schema.TypeString,
Computed: true,
Description: "GitHub API URL for the runner group's repositories.",
},
"visibility": {
Type: schema.TypeString,
Required: true,
Description: "The visibility of the runner group.",
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"all", "selected", "private"}, false), "visibility"),
},
"restricted_to_workflows": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If 'true', the runner group will be restricted to running only the workflows specified in the 'selected_workflows' array. Defaults to 'false'.",
},
"selected_workflows": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "List of workflows the runner group should be allowed to run. This setting will be ignored unless restricted_to_workflows is set to 'true'.",
},
},
}
}
func resourceGithubActionsRunnerGroupCreate(d *schema.ResourceData, meta any) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
name := d.Get("name").(string)
restrictedToWorkflows := d.Get("restricted_to_workflows").(bool)
visibility := d.Get("visibility").(string)
selectedRepositories, hasSelectedRepositories := d.GetOk("selected_repository_ids")
allowsPublicRepositories := d.Get("allows_public_repositories").(bool)
selectedWorkflows := []string{}
if workflows, ok := d.GetOk("selected_workflows"); ok {
for _, workflow := range workflows.([]any) {
selectedWorkflows = append(selectedWorkflows, workflow.(string))
}
}
if visibility != "selected" && hasSelectedRepositories {
return fmt.Errorf("cannot use selected_repository_ids without visibility being set to selected")
}
selectedRepositoryIDs := []int64{}
if hasSelectedRepositories {
ids := selectedRepositories.(*schema.Set).List()
for _, id := range ids {
selectedRepositoryIDs = append(selectedRepositoryIDs, int64(id.(int)))
}
}
ctx := context.Background()
runnerGroup, resp, err := client.Actions.CreateOrganizationRunnerGroup(ctx,
orgName,
github.CreateRunnerGroupRequest{
Name: &name,
Visibility: &visibility,
RestrictedToWorkflows: &restrictedToWorkflows,
SelectedRepositoryIDs: selectedRepositoryIDs,
SelectedWorkflows: selectedWorkflows,
AllowsPublicRepositories: &allowsPublicRepositories,
},
)
if err != nil {
return err
}
d.SetId(strconv.FormatInt(runnerGroup.GetID(), 10))
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return err
}
if err = d.Set("allows_public_repositories", runnerGroup.GetAllowsPublicRepositories()); err != nil {
return err
}
if err = d.Set("default", runnerGroup.GetDefault()); err != nil {
return err
}
if err = d.Set("id", strconv.FormatInt(runnerGroup.GetID(), 10)); err != nil {
return err
}
if err = d.Set("inherited", runnerGroup.GetInherited()); err != nil {
return err
}
if err = d.Set("name", runnerGroup.GetName()); err != nil {
return err
}
if err = d.Set("runners_url", runnerGroup.GetRunnersURL()); err != nil {
return err
}
if err = d.Set("selected_repositories_url", runnerGroup.GetSelectedRepositoriesURL()); err != nil {
return err
}
if err = d.Set("visibility", runnerGroup.GetVisibility()); err != nil {
return err
}
if err = d.Set("selected_repository_ids", selectedRepositoryIDs); err != nil { // Note: runnerGroup has no method to get selected repository IDs
return err
}
if err = d.Set("restricted_to_workflows", runnerGroup.GetRestrictedToWorkflows()); err != nil {
return err
}
if err = d.Set("selected_workflows", runnerGroup.SelectedWorkflows); err != nil {
return err
}
return resourceGithubActionsRunnerGroupRead(d, meta)
}
func getOrganizationRunnerGroup(client *github.Client, ctx context.Context, org string, groupID int64) (*github.RunnerGroup, *github.Response, error) {
runnerGroup, resp, err := client.Actions.GetOrganizationRunnerGroup(ctx, org, groupID)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
// ignore error StatusNotModified
return runnerGroup, resp, nil
}
}
return runnerGroup, resp, err
}
func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
runnerGroup, resp, err := getOrganizationRunnerGroup(client, ctx, orgName, runnerGroupID)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing organization runner group %s/%s from state because it no longer exists in GitHub",
orgName, d.Id())
d.SetId("")
return nil
}
}
return err
}
// if runner group is nil (typically not modified) we can return early
if runnerGroup == nil {
return nil
}
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return err
}
if err = d.Set("allows_public_repositories", runnerGroup.GetAllowsPublicRepositories()); err != nil {
return err
}
if err = d.Set("default", runnerGroup.GetDefault()); err != nil {
return err
}
if err = d.Set("id", strconv.FormatInt(runnerGroup.GetID(), 10)); err != nil {
return err
}
if err = d.Set("inherited", runnerGroup.GetInherited()); err != nil {
return err
}
if err = d.Set("name", runnerGroup.GetName()); err != nil {
return err
}
if err = d.Set("runners_url", runnerGroup.GetRunnersURL()); err != nil {
return err
}
if err = d.Set("selected_repositories_url", runnerGroup.GetSelectedRepositoriesURL()); err != nil {
return err
}
if err = d.Set("visibility", runnerGroup.GetVisibility()); err != nil {
return err
}
if err = d.Set("restricted_to_workflows", runnerGroup.GetRestrictedToWorkflows()); err != nil {
return err
}
if err = d.Set("selected_workflows", runnerGroup.SelectedWorkflows); err != nil {
return err
}
selectedRepositoryIDs := []int64{}
options := github.ListOptions{
PerPage: maxPerPage,
}
for {
runnerGroupRepositories, resp, err := client.Actions.ListRepositoryAccessRunnerGroup(ctx, orgName, runnerGroupID, &options)
if err != nil {
return err
}
for _, repo := range runnerGroupRepositories.Repositories {
selectedRepositoryIDs = append(selectedRepositoryIDs, *repo.ID)
}
if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}
if err = d.Set("selected_repository_ids", selectedRepositoryIDs); err != nil {
return err
}
return nil
}
func resourceGithubActionsRunnerGroupUpdate(d *schema.ResourceData, meta any) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
name := d.Get("name").(string)
visibility := d.Get("visibility").(string)
restrictedToWorkflows := d.Get("restricted_to_workflows").(bool)
selectedWorkflows := []string{}
allowsPublicRepositories := d.Get("allows_public_repositories").(bool)
if workflows, ok := d.GetOk("selected_workflows"); ok {
for _, workflow := range workflows.([]any) {
selectedWorkflows = append(selectedWorkflows, workflow.(string))
}
}
options := github.UpdateRunnerGroupRequest{
Name: &name,
Visibility: &visibility,
RestrictedToWorkflows: &restrictedToWorkflows,
SelectedWorkflows: selectedWorkflows,
AllowsPublicRepositories: &allowsPublicRepositories,
}
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if _, _, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, orgName, runnerGroupID, options); err != nil {
return err
}
selectedRepositories, hasSelectedRepositories := d.GetOk("selected_repository_ids")
selectedRepositoryIDs := []int64{}
if hasSelectedRepositories {
ids := selectedRepositories.(*schema.Set).List()
for _, id := range ids {
selectedRepositoryIDs = append(selectedRepositoryIDs, int64(id.(int)))
}
}
reposOptions := github.SetRepoAccessRunnerGroupRequest{SelectedRepositoryIDs: selectedRepositoryIDs}
if _, err := client.Actions.SetRepositoryAccessRunnerGroup(ctx, orgName, runnerGroupID, reposOptions); err != nil {
return err
}
return resourceGithubActionsRunnerGroupRead(d, meta)
}
func resourceGithubActionsRunnerGroupDelete(d *schema.ResourceData, meta any) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
log.Printf("[INFO] Deleting organization runner group: %s (%s)", d.Id(), orgName)
_, err = client.Actions.DeleteOrganizationRunnerGroup(ctx, orgName, runnerGroupID)
return err
}