@@ -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,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
0 commit comments