@@ -2,33 +2,33 @@ package github
22
33import (
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
1212func 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
0 commit comments