Skip to content

Commit c48fddf

Browse files
committed
data_source hosted runner: fix review
1 parent 16b5787 commit c48fddf

4 files changed

Lines changed: 83 additions & 145 deletions

github/data_source_github_enterprise_actions_hosted_runner.go

Lines changed: 35 additions & 91 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,100 +150,44 @@ 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
157+
runnerID := int64(d.Get("runner_id").(int))
163158

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-
}
182-
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.Errorf("error reading enterprise hosted runner: %s", err.Error())
185163
}
186164

187165
// Set the ID as enterprise_slug/runner_id
188-
d.SetId(fmt.Sprintf("%s/%d", enterpriseSlug, *foundRunner.ID))
189-
190-
if foundRunner.ID != nil {
191-
if err := d.Set("runner_id", int(*foundRunner.ID)); err != nil {
192-
return err
193-
}
166+
id, err := buildID(enterpriseSlug, strconv.FormatInt(runner.GetID(), 10))
167+
if err != nil {
168+
return diag.FromErr(err)
194169
}
195-
196-
if foundRunner.RunnerGroupID != nil {
197-
if err := d.Set("runner_group_id", int(*foundRunner.RunnerGroupID)); err != nil {
198-
return err
199-
}
200-
}
201-
202-
if foundRunner.Platform != nil {
203-
if err := d.Set("platform", *foundRunner.Platform); err != nil {
204-
return err
205-
}
206-
}
207-
208-
if foundRunner.Status != nil {
209-
if err := d.Set("status", *foundRunner.Status); err != nil {
210-
return err
211-
}
212-
}
213-
214-
if foundRunner.MaximumRunners != nil {
215-
if err := d.Set("maximum_runners", int(*foundRunner.MaximumRunners)); err != nil {
216-
return err
217-
}
170+
d.SetId(id)
171+
172+
runnerData := map[string]any{
173+
"name": runner.GetName(),
174+
"runner_group_id": int(runner.GetRunnerGroupID()),
175+
"platform": runner.GetPlatform(),
176+
"status": runner.GetStatus(),
177+
"maximum_runners": int(runner.GetMaximumRunners()),
178+
"public_ip_enabled": runner.GetPublicIPEnabled(),
179+
"image_details": flattenHostedRunnerImage(runner.ImageDetails),
180+
"machine_size_details": flattenHostedRunnerMachineSpec(runner.MachineSizeDetails),
181+
"public_ips": flattenHostedRunnerPublicIPs(runner.PublicIPs),
218182
}
219183

220-
if foundRunner.PublicIPEnabled != nil {
221-
if err := d.Set("public_ip_enabled", *foundRunner.PublicIPEnabled); err != nil {
222-
return err
223-
}
224-
}
225-
226-
if foundRunner.LastActiveOn != nil {
227-
if err := d.Set("last_active_on", foundRunner.LastActiveOn.Format(time.RFC3339)); err != nil {
228-
return err
229-
}
230-
}
231-
232-
if foundRunner.ImageDetails != nil {
233-
if err := d.Set("image_details", flattenHostedRunnerImage(foundRunner.ImageDetails)); err != nil {
234-
return err
235-
}
236-
}
237-
238-
if foundRunner.MachineSizeDetails != nil {
239-
if err := d.Set("machine_size_details", flattenHostedRunnerMachineSpec(foundRunner.MachineSizeDetails)); err != nil {
240-
return err
241-
}
184+
if runner.LastActiveOn != nil {
185+
runnerData["last_active_on"] = runner.LastActiveOn.Format(time.RFC3339)
242186
}
243187

244-
if foundRunner.PublicIPs != nil {
245-
if err := d.Set("public_ips", flattenHostedRunnerPublicIPs(foundRunner.PublicIPs)); err != nil {
246-
return err
188+
for k, v := range runnerData {
189+
if err := d.Set(k, v); err != nil {
190+
return diag.FromErr(err)
247191
}
248192
}
249193

github/data_source_github_enterprise_actions_hosted_runner_test.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,11 @@ 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

10-
func TestAccGithubEnterpriseActionsHostedRunnersDataSource(t *testing.T) {
11-
t.Run("lists all enterprise hosted runners", func(t *testing.T) {
12-
config := fmt.Sprintf(`
13-
data "github_enterprise_actions_hosted_runners" "test" {
14-
enterprise_slug = "%s"
15-
}
16-
`, testAccConf.enterpriseSlug)
17-
18-
resource.Test(t, resource.TestCase{
19-
PreCheck: func() { skipUnlessMode(t, enterprise) },
20-
ProviderFactories: providerFactories,
21-
Steps: []resource.TestStep{
22-
{
23-
Config: config,
24-
Check: resource.ComposeTestCheckFunc(
25-
resource.TestCheckResourceAttrSet("data.github_enterprise_actions_hosted_runners.test", "runners.#"),
26-
),
27-
},
28-
},
29-
})
30-
})
31-
}
32-
3310
func TestAccGithubEnterpriseActionsHostedRunnerDataSource(t *testing.T) {
34-
t.Run("gets a specific enterprise hosted runner by name", func(t *testing.T) {
11+
t.Run("gets a specific enterprise hosted runner by ID", func(t *testing.T) {
3512
config := fmt.Sprintf(`
3613
data "github_enterprise" "enterprise" {
3714
slug = "%s"
@@ -58,7 +35,7 @@ func TestAccGithubEnterpriseActionsHostedRunnerDataSource(t *testing.T) {
5835
5936
data "github_enterprise_actions_hosted_runner" "test" {
6037
enterprise_slug = data.github_enterprise.enterprise.slug
61-
name = github_enterprise_actions_hosted_runner.test.name
38+
runner_id = github_enterprise_actions_hosted_runner.test.runner_id
6239
}
6340
`, testAccConf.enterpriseSlug)
6441

github/data_source_github_enterprise_actions_hosted_runners.go

Lines changed: 14 additions & 28 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.Errorf("error listing enterprise hosted runners: %s", err.Error())
151151
}
152152

153153
allRunners = append(allRunners, runners.Runners...)
@@ -161,9 +161,8 @@ func dataSourceGithubEnterpriseActionsHostedRunnersRead(d *schema.ResourceData,
161161
// Set the ID as the enterprise slug
162162
d.SetId(enterpriseSlug)
163163

164-
// Flatten runners data
165164
if err := d.Set("runners", flattenHostedRunners(allRunners)); err != nil {
166-
return err
165+
return diag.FromErr(err)
167166
}
168167

169168
return nil
@@ -182,27 +181,14 @@ func flattenHostedRunners(runners []*github.HostedRunner) []any {
182181

183182
runnerMap := make(map[string]any)
184183

185-
if runner.ID != nil {
186-
runnerMap["id"] = int(*runner.ID)
187-
}
188-
if runner.Name != nil {
189-
runnerMap["name"] = *runner.Name
190-
}
191-
if runner.RunnerGroupID != nil {
192-
runnerMap["runner_group_id"] = int(*runner.RunnerGroupID)
193-
}
194-
if runner.Platform != nil {
195-
runnerMap["platform"] = *runner.Platform
196-
}
197-
if runner.Status != nil {
198-
runnerMap["status"] = *runner.Status
199-
}
200-
if runner.MaximumRunners != nil {
201-
runnerMap["maximum_runners"] = int(*runner.MaximumRunners)
202-
}
203-
if runner.PublicIPEnabled != nil {
204-
runnerMap["public_ip_enabled"] = *runner.PublicIPEnabled
205-
}
184+
runnerMap["id"] = int(runner.GetID())
185+
runnerMap["name"] = runner.GetName()
186+
runnerMap["runner_group_id"] = int(runner.GetRunnerGroupID())
187+
runnerMap["platform"] = runner.GetPlatform()
188+
runnerMap["status"] = runner.GetStatus()
189+
runnerMap["maximum_runners"] = int(runner.GetMaximumRunners())
190+
runnerMap["public_ip_enabled"] = runner.GetPublicIPEnabled()
191+
206192
if runner.LastActiveOn != nil {
207193
runnerMap["last_active_on"] = runner.LastActiveOn.Format(time.RFC3339)
208194
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
)
9+
10+
func TestAccGithubEnterpriseActionsHostedRunnersDataSource(t *testing.T) {
11+
t.Run("lists all enterprise hosted runners", func(t *testing.T) {
12+
config := fmt.Sprintf(`
13+
data "github_enterprise_actions_hosted_runners" "test" {
14+
enterprise_slug = "%s"
15+
}
16+
`, testAccConf.enterpriseSlug)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { skipUnlessMode(t, enterprise) },
20+
ProviderFactories: providerFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: config,
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttrSet("data.github_enterprise_actions_hosted_runners.test", "runners.#"),
26+
),
27+
},
28+
},
29+
})
30+
})
31+
}

0 commit comments

Comments
 (0)