Skip to content

Commit 2a359b1

Browse files
committed
data_source hosted runner: fix review
1 parent 08e648b commit 2a359b1

3 files changed

Lines changed: 50 additions & 86 deletions

github/data_source_github_enterprise_actions_hosted_runner.go

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ package github
22

33
import (
44
"context"
5-
"fmt"
5+
"strconv"
66
"time"
77

8-
"github.com/google/go-github/v82/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
)
1111

1212
func dataSourceGithubEnterpriseActionsHostedRunner() *schema.Resource {
1313
return &schema.Resource{
14-
Read: dataSourceGithubEnterpriseActionsHostedRunnerRead,
14+
ReadContext: dataSourceGithubEnterpriseActionsHostedRunnerRead,
1515

1616
Schema: map[string]*schema.Schema{
1717
"enterprise_slug": {
1818
Type: schema.TypeString,
1919
Required: true,
2020
Description: "The slug of the enterprise.",
2121
},
22-
"name": {
23-
Type: schema.TypeString,
24-
Required: true,
25-
Description: "The name of the hosted runner to lookup.",
26-
},
2722
"runner_id": {
2823
Type: schema.TypeInt,
29-
Computed: true,
24+
Required: true,
3025
Description: "The numeric ID of the hosted runner.",
3126
},
27+
"name": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: "The name of the hosted runner.",
31+
},
3232
"runner_group_id": {
3333
Type: schema.TypeInt,
3434
Computed: true,
@@ -150,101 +150,65 @@ func dataSourceGithubEnterpriseActionsHostedRunner() *schema.Resource {
150150
}
151151
}
152152

153-
func dataSourceGithubEnterpriseActionsHostedRunnerRead(d *schema.ResourceData, meta any) error {
153+
func dataSourceGithubEnterpriseActionsHostedRunnerRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
154154
client := meta.(*Owner).v3client
155-
ctx := context.Background()
156155

157156
enterpriseSlug := d.Get("enterprise_slug").(string)
158-
runnerName := d.Get("name").(string)
159-
160-
// List all runners and find the one matching the name
161-
opts := &github.ListOptions{PerPage: 100}
162-
var foundRunner *github.HostedRunner
163-
164-
for {
165-
runners, resp, err := client.Enterprise.ListHostedRunners(ctx, enterpriseSlug, opts)
166-
if err != nil {
167-
return err
168-
}
169-
170-
for _, runner := range runners.Runners {
171-
if runner.Name != nil && *runner.Name == runnerName {
172-
foundRunner = runner
173-
break
174-
}
175-
}
176-
177-
if foundRunner != nil || resp.NextPage == 0 {
178-
break
179-
}
180-
opts.Page = resp.NextPage
181-
}
157+
runnerID := int64(d.Get("runner_id").(int))
182158

183-
if foundRunner == nil {
184-
return fmt.Errorf("no hosted runner found with name %q in enterprise %q", runnerName, enterpriseSlug)
159+
// Get the specific runner by ID
160+
runner, _, err := client.Enterprise.GetHostedRunner(ctx, enterpriseSlug, runnerID)
161+
if err != nil {
162+
return diag.FromErr(err)
185163
}
186164

187165
// Set the ID as enterprise_slug/runner_id
188-
d.SetId(fmt.Sprintf("%s/%d", enterpriseSlug, *foundRunner.ID))
166+
id, err := buildID(enterpriseSlug, strconv.FormatInt(runner.GetID(), 10))
167+
if err != nil {
168+
return diag.FromErr(err)
169+
}
170+
d.SetId(id)
189171

190-
if foundRunner.ID != nil {
191-
if err := d.Set("runner_id", int(*foundRunner.ID)); err != nil {
192-
return err
193-
}
172+
if err := d.Set("name", runner.GetName()); err != nil {
173+
return diag.FromErr(err)
194174
}
195175

196-
if foundRunner.RunnerGroupID != nil {
197-
if err := d.Set("runner_group_id", int(*foundRunner.RunnerGroupID)); err != nil {
198-
return err
199-
}
176+
if err := d.Set("runner_group_id", int(runner.GetRunnerGroupID())); err != nil {
177+
return diag.FromErr(err)
200178
}
201179

202-
if foundRunner.Platform != nil {
203-
if err := d.Set("platform", *foundRunner.Platform); err != nil {
204-
return err
205-
}
180+
if err := d.Set("platform", runner.GetPlatform()); err != nil {
181+
return diag.FromErr(err)
206182
}
207183

208-
if foundRunner.Status != nil {
209-
if err := d.Set("status", *foundRunner.Status); err != nil {
210-
return err
211-
}
184+
if err := d.Set("status", runner.GetStatus()); err != nil {
185+
return diag.FromErr(err)
212186
}
213187

214-
if foundRunner.MaximumRunners != nil {
215-
if err := d.Set("maximum_runners", int(*foundRunner.MaximumRunners)); err != nil {
216-
return err
217-
}
188+
if err := d.Set("maximum_runners", int(runner.GetMaximumRunners())); err != nil {
189+
return diag.FromErr(err)
218190
}
219191

220-
if foundRunner.PublicIPEnabled != nil {
221-
if err := d.Set("public_ip_enabled", *foundRunner.PublicIPEnabled); err != nil {
222-
return err
223-
}
192+
if err := d.Set("public_ip_enabled", runner.GetPublicIPEnabled()); err != nil {
193+
return diag.FromErr(err)
224194
}
225195

226-
if foundRunner.LastActiveOn != nil {
227-
if err := d.Set("last_active_on", foundRunner.LastActiveOn.Format(time.RFC3339)); err != nil {
228-
return err
196+
if runner.LastActiveOn != nil {
197+
if err := d.Set("last_active_on", runner.LastActiveOn.Format(time.RFC3339)); err != nil {
198+
return diag.FromErr(err)
229199
}
230200
}
231201

232-
if foundRunner.ImageDetails != nil {
233-
if err := d.Set("image_details", flattenHostedRunnerImage(foundRunner.ImageDetails)); err != nil {
234-
return err
235-
}
202+
if err := d.Set("image_details", flattenHostedRunnerImage(runner.ImageDetails)); err != nil {
203+
return diag.FromErr(err)
236204
}
237205

238-
if foundRunner.MachineSizeDetails != nil {
239-
if err := d.Set("machine_size_details", flattenHostedRunnerMachineSpec(foundRunner.MachineSizeDetails)); err != nil {
240-
return err
241-
}
206+
if err := d.Set("machine_size_details", flattenHostedRunnerMachineSpec(runner.MachineSizeDetails)); err != nil {
207+
return diag.FromErr(err)
242208
}
243209

244-
if foundRunner.PublicIPs != nil {
245-
if err := d.Set("public_ips", flattenHostedRunnerPublicIPs(foundRunner.PublicIPs)); err != nil {
246-
return err
247-
}
210+
if err := d.Set("public_ips", flattenHostedRunnerPublicIPs(runner.PublicIPs)); err != nil {
211+
return diag.FromErr(err)
248212
}
249213

250214
return nil

github/data_source_github_enterprise_actions_hosted_runner_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
88
)
99

1010
func TestAccGithubEnterpriseActionsHostedRunnersDataSource(t *testing.T) {
@@ -31,7 +31,7 @@ func TestAccGithubEnterpriseActionsHostedRunnersDataSource(t *testing.T) {
3131
}
3232

3333
func TestAccGithubEnterpriseActionsHostedRunnerDataSource(t *testing.T) {
34-
t.Run("gets a specific enterprise hosted runner by name", func(t *testing.T) {
34+
t.Run("gets a specific enterprise hosted runner by ID", func(t *testing.T) {
3535
config := fmt.Sprintf(`
3636
data "github_enterprise" "enterprise" {
3737
slug = "%s"
@@ -58,7 +58,7 @@ func TestAccGithubEnterpriseActionsHostedRunnerDataSource(t *testing.T) {
5858
5959
data "github_enterprise_actions_hosted_runner" "test" {
6060
enterprise_slug = data.github_enterprise.enterprise.slug
61-
name = github_enterprise_actions_hosted_runner.test.name
61+
runner_id = github_enterprise_actions_hosted_runner.test.runner_id
6262
}
6363
`, testAccConf.enterpriseSlug)
6464

github/data_source_github_enterprise_actions_hosted_runners.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"time"
66

77
"github.com/google/go-github/v82/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubEnterpriseActionsHostedRunners() *schema.Resource {
1213
return &schema.Resource{
13-
Read: dataSourceGithubEnterpriseActionsHostedRunnersRead,
14+
ReadContext: dataSourceGithubEnterpriseActionsHostedRunnersRead,
1415

1516
Schema: map[string]*schema.Schema{
1617
"enterprise_slug": {
@@ -134,20 +135,19 @@ func dataSourceGithubEnterpriseActionsHostedRunners() *schema.Resource {
134135
}
135136
}
136137

137-
func dataSourceGithubEnterpriseActionsHostedRunnersRead(d *schema.ResourceData, meta any) error {
138+
func dataSourceGithubEnterpriseActionsHostedRunnersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
138139
client := meta.(*Owner).v3client
139-
ctx := context.Background()
140140

141141
enterpriseSlug := d.Get("enterprise_slug").(string)
142142

143143
// List all hosted runners with pagination
144-
opts := &github.ListOptions{PerPage: 100}
144+
opts := &github.ListOptions{PerPage: maxPerPage}
145145
var allRunners []*github.HostedRunner
146146

147147
for {
148148
runners, resp, err := client.Enterprise.ListHostedRunners(ctx, enterpriseSlug, opts)
149149
if err != nil {
150-
return err
150+
return diag.FromErr(err)
151151
}
152152

153153
allRunners = append(allRunners, runners.Runners...)
@@ -163,7 +163,7 @@ func dataSourceGithubEnterpriseActionsHostedRunnersRead(d *schema.ResourceData,
163163

164164
// Flatten runners data
165165
if err := d.Set("runners", flattenHostedRunners(allRunners)); err != nil {
166-
return err
166+
return diag.FromErr(err)
167167
}
168168

169169
return nil

0 commit comments

Comments
 (0)