@@ -20,17 +20,6 @@ func dataSourceOpenAIOrganizationUsers() *schema.Resource {
2020 Optional : true ,
2121 Description : "The ID of a specific user to retrieve. If provided, other filter parameters are ignored." ,
2222 },
23- "after" : {
24- Type : schema .TypeString ,
25- Optional : true ,
26- Description : "A cursor for use in pagination. 'after' is an object ID that defines your place in the list." ,
27- },
28- "limit" : {
29- Type : schema .TypeInt ,
30- Optional : true ,
31- Default : 20 ,
32- Description : "A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20." ,
33- },
3423 "emails" : {
3524 Type : schema .TypeList ,
3625 Optional : true ,
@@ -78,21 +67,6 @@ func dataSourceOpenAIOrganizationUsers() *schema.Resource {
7867 },
7968 Description : "List of users in the organization" ,
8069 },
81- "first_id" : {
82- Type : schema .TypeString ,
83- Computed : true ,
84- Description : "The ID of the first user in the list" ,
85- },
86- "last_id" : {
87- Type : schema .TypeString ,
88- Computed : true ,
89- Description : "The ID of the last user in the list" ,
90- },
91- "has_more" : {
92- Type : schema .TypeBool ,
93- Computed : true ,
94- Description : "Whether there are more users that can be retrieved by paginating" ,
95- },
9670 },
9771 }
9872}
@@ -139,27 +113,9 @@ func dataSourceOpenAIOrganizationUsersRead(ctx context.Context, d *schema.Resour
139113 return diag .FromErr (fmt .Errorf ("error setting users: %s" , err ))
140114 }
141115
142- // For a single user, has_more is always false
143- if err := d .Set ("has_more" , false ); err != nil {
144- return diag .FromErr (fmt .Errorf ("error setting has_more: %s" , err ))
145- }
146-
147- // Set first_id and last_id to the user's ID
148- if err := d .Set ("first_id" , user .ID ); err != nil {
149- return diag .FromErr (fmt .Errorf ("error setting first_id: %s" , err ))
150- }
151-
152- if err := d .Set ("last_id" , user .ID ); err != nil {
153- return diag .FromErr (fmt .Errorf ("error setting last_id: %s" , err ))
154- }
155-
156116 return nil
157117 }
158118
159- // If no specific user ID, list users with filters
160- after := d .Get ("after" ).(string )
161- limit := d .Get ("limit" ).(int )
162-
163119 // Extract the emails filter if provided
164120 var emails []string
165121 if rawEmails , ok := d .GetOk ("emails" ); ok {
@@ -168,46 +124,53 @@ func dataSourceOpenAIOrganizationUsersRead(ctx context.Context, d *schema.Resour
168124 }
169125 }
170126
171- tflog .Debug (ctx , fmt .Sprintf ("Listing organization users with limit: %d" , limit ))
127+ // Automatic pagination - fetch all users with default batch size
128+ const batchSize = 100
129+ tflog .Debug (ctx , fmt .Sprintf ("Fetching all organization users with batch size: %d" , batchSize ))
172130
173- // Call the API using the provider's API key
174- resp , err := c .ListUsers (after , limit , emails )
175- if err != nil {
176- return diag .Errorf ("error listing organization users: %s" , err )
177- }
131+ var allUsers []map [string ]interface {}
132+ var after string
133+ hasMore := true
134+ pageCount := 0
178135
179- // Set a unique ID based on the query parameters
180- d .SetId (fmt .Sprintf ("organization-users-%s-%d" , after , limit ))
181-
182- // Prepare the users list
183- users := make ([]map [string ]interface {}, 0 , len (resp .Data ))
184- for _ , user := range resp .Data {
185- u := map [string ]interface {}{
186- "id" : user .ID ,
187- "object" : user .Object ,
188- "email" : user .Email ,
189- "name" : user .Name ,
190- "role" : user .Role ,
191- "added_at" : user .AddedAt ,
136+ // Paginate through all results
137+ for hasMore {
138+ pageCount ++
139+ tflog .Debug (ctx , fmt .Sprintf ("Fetching page %d with after: %s" , pageCount , after ))
140+
141+ resp , err := c .ListUsers (after , batchSize , emails )
142+ if err != nil {
143+ return diag .Errorf ("error listing organization users (page %d): %s" , pageCount , err )
192144 }
193- users = append (users , u )
194- }
195145
196- // Set the computed values
197- if err := d .Set ("users" , users ); err != nil {
198- return diag .FromErr (fmt .Errorf ("error setting users: %s" , err ))
199- }
146+ // Add users from this page to the collection
147+ for _ , user := range resp .Data {
148+ u := map [string ]interface {}{
149+ "id" : user .ID ,
150+ "object" : user .Object ,
151+ "email" : user .Email ,
152+ "name" : user .Name ,
153+ "role" : user .Role ,
154+ "added_at" : user .AddedAt ,
155+ }
156+ allUsers = append (allUsers , u )
157+ }
200158
201- if err := d .Set ("has_more" , resp .HasMore ); err != nil {
202- return diag .FromErr (fmt .Errorf ("error setting has_more: %s" , err ))
159+ // Check if there are more pages
160+ hasMore = resp .HasMore
161+ if hasMore && resp .LastID != "" {
162+ after = resp .LastID
163+ }
203164 }
204165
205- if err := d .Set ("first_id" , resp .FirstID ); err != nil {
206- return diag .FromErr (fmt .Errorf ("error setting first_id: %s" , err ))
207- }
166+ tflog .Debug (ctx , fmt .Sprintf ("Fetched %d total users across %d pages" , len (allUsers ), pageCount ))
167+
168+ // Set a unique ID for the data source
169+ d .SetId (fmt .Sprintf ("organization-users-all-%d" , len (allUsers )))
208170
209- if err := d .Set ("last_id" , resp .LastID ); err != nil {
210- return diag .FromErr (fmt .Errorf ("error setting last_id: %s" , err ))
171+ // Set the computed values
172+ if err := d .Set ("users" , allUsers ); err != nil {
173+ return diag .FromErr (fmt .Errorf ("error setting users: %s" , err ))
211174 }
212175
213176 return nil
0 commit comments