@@ -200,11 +200,68 @@ func (r *RunsCollectorReconciler) SetupWithManager(mgr ctrl.Manager) error {
200200 Complete (r )
201201}
202202
203+ func (r * RunsCollectorReconciler ) getAgentPoolIDByName (ctx context.Context , rc * runsCollectorInstance ) (* tfc.AgentPool , error ) {
204+ name := rc .instance .Spec .AgentPool .Name
205+
206+ listOpts := & tfc.AgentPoolListOptions {
207+ Query : name ,
208+ ListOptions : tfc.ListOptions {
209+ PageSize : maxPageSize ,
210+ },
211+ }
212+ for {
213+ ap , err := rc .tfClient .Client .AgentPools .List (ctx , rc .instance .Spec .Organization , listOpts )
214+ if err != nil {
215+ return nil , err
216+ }
217+ for _ , a := range ap .Items {
218+ if a .Name == name {
219+ return a , nil
220+ }
221+ }
222+ if ap .NextPage == 0 {
223+ break
224+ }
225+ listOpts .PageNumber = ap .NextPage
226+ }
227+
228+ return nil , fmt .Errorf ("agent pool ID not found for agent pool name %q" , name )
229+ }
230+
231+ func (r * RunsCollectorReconciler ) updateStatusAgentPool (ctx context.Context , rc * runsCollectorInstance ) error {
232+ var pool * tfc.AgentPool
233+ var err error
234+ if rc .instance .Spec .AgentPool .Name != "" {
235+ pool , err = r .getAgentPoolIDByName (ctx , rc )
236+ if err != nil {
237+ return err
238+ }
239+
240+ }
241+ if rc .instance .Spec .AgentPool .ID != "" {
242+ pool , err = rc .tfClient .Client .AgentPools .Read (ctx , rc .instance .Spec .AgentPool .ID )
243+ if err != nil {
244+ return err
245+ }
246+
247+ }
248+ rc .instance .Status .AgentPool = & appv1alpha2.AgentPoolRef {
249+ ID : pool .ID ,
250+ Name : pool .Name ,
251+ }
252+
253+ return nil
254+ }
255+
203256func (r * RunsCollectorReconciler ) reconcileRuns (ctx context.Context , rc * runsCollectorInstance ) error {
204257 runs := map [tfc.RunStatus ]float64 {}
205258 var runsTotal float64
259+ if rc .instance .NeedUpdateStatus () {
260+ r .updateStatusAgentPool (ctx , rc )
261+ }
262+
206263 listOpts := & tfc.RunListForOrganizationOptions {
207- AgentPoolNames : rc .instance .Spec .AgentPool .Name ,
264+ AgentPoolNames : rc .instance .Status .AgentPool .Name ,
208265 StatusGroup : "non_final" ,
209266 ListOptions : tfc.ListOptions {
210267 PageSize : maxPageSize ,
0 commit comments