Skip to content

Commit 03c3c25

Browse files
committed
feat: update GitHub Actions runner group functions to use context and improve error handling
1 parent e186f03 commit 03c3c25

5 files changed

Lines changed: 226 additions & 151 deletions

github/resource_github_actions_runner_group.go

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import (
99
"strconv"
1010

1111
"github.com/google/go-github/v83/github"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1415
)
1516

1617
func resourceGithubActionsRunnerGroup() *schema.Resource {
1718
return &schema.Resource{
18-
Create: resourceGithubActionsRunnerGroupCreate,
19-
Read: resourceGithubActionsRunnerGroupRead,
20-
Update: resourceGithubActionsRunnerGroupUpdate,
21-
Delete: resourceGithubActionsRunnerGroupDelete,
19+
CreateContext: resourceGithubActionsRunnerGroupCreate,
20+
ReadContext: resourceGithubActionsRunnerGroupRead,
21+
UpdateContext: resourceGithubActionsRunnerGroupUpdate,
22+
DeleteContext: resourceGithubActionsRunnerGroupDelete,
2223
Importer: &schema.ResourceImporter{
2324
StateContext: schema.ImportStatePassthroughContext,
2425
},
@@ -93,10 +94,10 @@ func resourceGithubActionsRunnerGroup() *schema.Resource {
9394
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'.",
9495
},
9596
"network_configuration_id": {
96-
Type: schema.TypeString,
97-
Optional: true,
98-
ValidateFunc: validation.StringLenBetween(1, 255),
99-
Description: "The identifier of the hosted compute network configuration to associate with this runner group for GitHub-hosted private networking.",
97+
Type: schema.TypeString,
98+
Optional: true,
99+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(1, 255)),
100+
Description: "The identifier of the hosted compute network configuration to associate with this runner group for GitHub-hosted private networking.",
100101
},
101102
},
102103
}
@@ -108,7 +109,7 @@ func getOrganizationRunnerGroup(client *github.Client, ctx context.Context, org
108109
var ghErr *github.ErrorResponse
109110
if errors.As(err, &ghErr) && ghErr.Response != nil && ghErr.Response.StatusCode == http.StatusNotModified {
110111
// ignore error StatusNotModified
111-
return runnerGroup, resp, nil
112+
return nil, resp, nil
112113
}
113114
}
114115
return runnerGroup, resp, err
@@ -155,10 +156,9 @@ func setGithubActionsRunnerGroupState(d *schema.ResourceData, runnerGroup *githu
155156
return nil
156157
}
157158

158-
func resourceGithubActionsRunnerGroupCreate(d *schema.ResourceData, meta any) error {
159-
err := checkOrganization(meta)
160-
if err != nil {
161-
return err
159+
func resourceGithubActionsRunnerGroupCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
160+
if err := checkOrganization(meta); err != nil {
161+
return diag.FromErr(err)
162162
}
163163

164164
client := meta.(*Owner).v3client
@@ -177,7 +177,7 @@ func resourceGithubActionsRunnerGroupCreate(d *schema.ResourceData, meta any) er
177177
}
178178

179179
if visibility != "selected" && hasSelectedRepositories {
180-
return fmt.Errorf("cannot use selected_repository_ids without visibility being set to selected")
180+
return diag.FromErr(fmt.Errorf("cannot use selected_repository_ids without visibility being set to selected"))
181181
}
182182

183183
selectedRepositoryIDs := []int64{}
@@ -190,7 +190,7 @@ func resourceGithubActionsRunnerGroupCreate(d *schema.ResourceData, meta any) er
190190
}
191191
}
192192

193-
ctx := context.Background()
193+
ctx = context.WithValue(ctx, ctxId, d.Id())
194194

195195
runnerGroup, resp, err := client.Actions.CreateOrganizationRunnerGroup(ctx,
196196
orgName,
@@ -204,45 +204,49 @@ func resourceGithubActionsRunnerGroupCreate(d *schema.ResourceData, meta any) er
204204
},
205205
)
206206
if err != nil {
207-
return err
207+
return diag.FromErr(err)
208208
}
209209
d.SetId(strconv.FormatInt(runnerGroup.GetID(), 10))
210+
ctx = context.WithValue(ctx, ctxId, d.Id())
210211
if err = setGithubActionsRunnerGroupState(d, runnerGroup, normalizeEtag(resp.Header.Get("ETag")), selectedRepositoryIDs); err != nil {
211-
return err
212+
return diag.FromErr(err)
212213
}
213214

214215
if networkConfigurationID, ok := d.GetOk("network_configuration_id"); ok {
215216
networkConfigurationIDValue := networkConfigurationID.(string)
216217
// The create endpoint does not accept network_configuration_id, so private networking
217218
// must be attached with a follow-up PATCH after the runner group has been created.
218219
if _, err = updateRunnerGroupNetworking(client, ctx, fmt.Sprintf("orgs/%s/actions/runner-groups/%d", orgName, runnerGroup.GetID()), &networkConfigurationIDValue); err != nil {
219-
return err
220+
return diag.FromErr(err)
220221
}
221222

222-
return resourceGithubActionsRunnerGroupRead(d, meta)
223+
if err = setRunnerGroupNetworkingState(d, &runnerGroupNetworking{NetworkConfigurationID: &networkConfigurationIDValue}); err != nil {
224+
return diag.FromErr(err)
225+
}
226+
227+
return nil
223228
}
224229

225230
if err = setRunnerGroupNetworkingState(d, nil); err != nil {
226-
return err
231+
return diag.FromErr(err)
227232
}
228233

229234
return nil
230235
}
231236

232-
func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) error {
233-
err := checkOrganization(meta)
234-
if err != nil {
235-
return err
237+
func resourceGithubActionsRunnerGroupRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
238+
if err := checkOrganization(meta); err != nil {
239+
return diag.FromErr(err)
236240
}
237241

238242
client := meta.(*Owner).v3client
239243
orgName := meta.(*Owner).name
240244

241245
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
242246
if err != nil {
243-
return err
247+
return diag.FromErr(err)
244248
}
245-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
249+
ctx = context.WithValue(ctx, ctxId, d.Id())
246250
if !d.IsNewResource() {
247251
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
248252
}
@@ -258,7 +262,7 @@ func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) erro
258262
return nil
259263
}
260264
}
261-
return err
265+
return diag.FromErr(err)
262266
}
263267

264268
runnerGroupEtag := normalizeEtag(d.Get("etag").(string))
@@ -268,7 +272,7 @@ func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) erro
268272

269273
runnerGroupNetworking, _, err := getRunnerGroupNetworking(client, ctx, fmt.Sprintf("orgs/%s/actions/runner-groups/%d", orgName, runnerGroupID))
270274
if err != nil {
271-
return err
275+
return diag.FromErr(err)
272276
}
273277

274278
selectedRepositoryIDs := []int64{}
@@ -279,7 +283,7 @@ func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) erro
279283
for {
280284
runnerGroupRepositories, resp, err := client.Actions.ListRepositoryAccessRunnerGroup(ctx, orgName, runnerGroupID, &options)
281285
if err != nil {
282-
return err
286+
return diag.FromErr(err)
283287
}
284288

285289
for _, repo := range runnerGroupRepositories.Repositories {
@@ -295,29 +299,28 @@ func resourceGithubActionsRunnerGroupRead(d *schema.ResourceData, meta any) erro
295299

296300
if runnerGroup != nil {
297301
if err = setGithubActionsRunnerGroupState(d, runnerGroup, runnerGroupEtag, selectedRepositoryIDs); err != nil {
298-
return err
302+
return diag.FromErr(err)
299303
}
300304
} else {
301305
if err := d.Set("selected_repository_ids", selectedRepositoryIDs); err != nil {
302-
return err
306+
return diag.FromErr(err)
303307
}
304308
if err := d.Set("etag", runnerGroupEtag); err != nil {
305-
return err
309+
return diag.FromErr(err)
306310
}
307311
}
308312
if runnerGroupNetworking != nil {
309313
if err = setRunnerGroupNetworkingState(d, runnerGroupNetworking); err != nil {
310-
return err
314+
return diag.FromErr(err)
311315
}
312316
}
313317

314318
return nil
315319
}
316320

317-
func resourceGithubActionsRunnerGroupUpdate(d *schema.ResourceData, meta any) error {
318-
err := checkOrganization(meta)
319-
if err != nil {
320-
return err
321+
func resourceGithubActionsRunnerGroupUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
322+
if err := checkOrganization(meta); err != nil {
323+
return diag.FromErr(err)
321324
}
322325

323326
client := meta.(*Owner).v3client
@@ -344,26 +347,32 @@ func resourceGithubActionsRunnerGroupUpdate(d *schema.ResourceData, meta any) er
344347

345348
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
346349
if err != nil {
347-
return err
350+
return diag.FromErr(err)
348351
}
349-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
352+
ctx = context.WithValue(ctx, ctxId, d.Id())
350353

351-
if _, _, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, orgName, runnerGroupID, options); err != nil {
352-
return err
354+
runnerGroup, resp, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, orgName, runnerGroupID, options)
355+
if err != nil {
356+
return diag.FromErr(err)
353357
}
354358

355-
if d.HasChange("network_configuration_id") {
356-
var networkConfigurationIDValue *string
357-
if networkConfigurationID, ok := d.GetOk("network_configuration_id"); ok {
358-
value := networkConfigurationID.(string)
359-
networkConfigurationIDValue = &value
360-
}
359+
var networkConfigurationIDValue *string
360+
if networkConfigurationID, ok := d.GetOk("network_configuration_id"); ok {
361+
value := networkConfigurationID.(string)
362+
networkConfigurationIDValue = &value
363+
}
361364

365+
if d.HasChange("network_configuration_id") {
362366
if _, err := updateRunnerGroupNetworking(client, ctx, fmt.Sprintf("orgs/%s/actions/runner-groups/%d", orgName, runnerGroupID), networkConfigurationIDValue); err != nil {
363-
return err
367+
return diag.FromErr(err)
364368
}
365369
}
366370

371+
var networkingState *runnerGroupNetworking
372+
if networkConfigurationIDValue != nil {
373+
networkingState = &runnerGroupNetworking{NetworkConfigurationID: networkConfigurationIDValue}
374+
}
375+
367376
selectedRepositories, hasSelectedRepositories := d.GetOk("selected_repository_ids")
368377
selectedRepositoryIDs := []int64{}
369378

@@ -378,27 +387,42 @@ func resourceGithubActionsRunnerGroupUpdate(d *schema.ResourceData, meta any) er
378387
reposOptions := github.SetRepoAccessRunnerGroupRequest{SelectedRepositoryIDs: selectedRepositoryIDs}
379388

380389
if _, err := client.Actions.SetRepositoryAccessRunnerGroup(ctx, orgName, runnerGroupID, reposOptions); err != nil {
381-
return err
390+
return diag.FromErr(err)
382391
}
383392

384-
return resourceGithubActionsRunnerGroupRead(d, meta)
393+
runnerGroupEtag := normalizeEtag(d.Get("etag").(string))
394+
if resp != nil {
395+
runnerGroupEtag = normalizeEtag(resp.Header.Get("ETag"))
396+
}
397+
398+
if err := setGithubActionsRunnerGroupState(d, runnerGroup, runnerGroupEtag, selectedRepositoryIDs); err != nil {
399+
return diag.FromErr(err)
400+
}
401+
if err := setRunnerGroupNetworkingState(d, networkingState); err != nil {
402+
return diag.FromErr(err)
403+
}
404+
405+
return nil
385406
}
386407

387-
func resourceGithubActionsRunnerGroupDelete(d *schema.ResourceData, meta any) error {
388-
err := checkOrganization(meta)
389-
if err != nil {
390-
return err
408+
func resourceGithubActionsRunnerGroupDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
409+
if err := checkOrganization(meta); err != nil {
410+
return diag.FromErr(err)
391411
}
392412

393413
client := meta.(*Owner).v3client
394414
orgName := meta.(*Owner).name
395415
runnerGroupID, err := strconv.ParseInt(d.Id(), 10, 64)
396416
if err != nil {
397-
return err
417+
return diag.FromErr(err)
398418
}
399-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
419+
ctx = context.WithValue(ctx, ctxId, d.Id())
400420

401421
log.Printf("[INFO] Deleting organization runner group: %s (%s)", d.Id(), orgName)
402422
_, err = client.Actions.DeleteOrganizationRunnerGroup(ctx, orgName, runnerGroupID)
403-
return err
423+
if err != nil {
424+
return diag.FromErr(err)
425+
}
426+
427+
return nil
404428
}

github/resource_github_actions_runner_group_networking.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,10 @@ func updateRunnerGroupNetworking(client *github.Client, ctx context.Context, pat
5252
}
5353

5454
func setRunnerGroupNetworkingState(d *schema.ResourceData, runnerGroup *runnerGroupNetworking) error {
55-
if runnerGroup != nil && runnerGroup.NetworkConfigurationID != nil && *runnerGroup.NetworkConfigurationID != "" {
56-
if err := d.Set("network_configuration_id", *runnerGroup.NetworkConfigurationID); err != nil {
57-
return err
58-
}
59-
} else {
60-
if err := d.Set("network_configuration_id", nil); err != nil {
61-
return err
62-
}
55+
var networkConfigurationID any
56+
if runnerGroup != nil {
57+
networkConfigurationID = runnerGroup.NetworkConfigurationID
6358
}
6459

65-
return nil
60+
return d.Set("network_configuration_id", networkConfigurationID)
6661
}

0 commit comments

Comments
 (0)