@@ -168,9 +168,10 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
168168 op (u )
169169 }
170170
171+ tokenClient := auth .NewTokenClient ()
172+
171173 // Create a default api client if none was provided
172174 if u .apiClient == nil {
173- tokenClient := auth .NewTokenClient ()
174175 ac , err := cqapi .NewClientWithResponses (u .url , cqapi .WithRequestEditorFn (func (ctx context.Context , req * http.Request ) error {
175176 token , err := tokenClient .GetToken ()
176177 if err != nil {
@@ -187,14 +188,9 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
187188
188189 // Set team name from configuration if not provided
189190 if u .teamName == "" {
190- teamName , err := config .GetValue ("team" )
191- if errors .Is (err , os .ErrNotExist ) {
192- return nil , fmt .Errorf ("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch <team>`" , err )
193- } else if err != nil {
194- return nil , fmt .Errorf ("failed to get team name from config: %w" , err )
195- }
196- if teamName == "" {
197- return nil , fmt .Errorf ("team name not set. Hint: use `cloudquery switch <team>`" )
191+ teamName , err := u .getTeamNameByTokenType (tokenClient .GetTokenType ())
192+ if err != nil {
193+ return nil , fmt .Errorf ("failed to get team name: %w" , err )
198194 }
199195 u .teamName = teamName
200196 }
@@ -361,3 +357,33 @@ func (u *BatchUpdater) calculateRetryDuration(statusCode int, headers http.Heade
361357func retryableStatusCode (statusCode int ) bool {
362358 return statusCode == http .StatusTooManyRequests || statusCode == http .StatusServiceUnavailable
363359}
360+
361+ func (u * BatchUpdater ) getTeamNameByTokenType (tokenType auth.TokenType ) (string , error ) {
362+ switch tokenType {
363+ case auth .BearerToken :
364+ teamName , err := config .GetValue ("team" )
365+ if errors .Is (err , os .ErrNotExist ) {
366+ return "" , fmt .Errorf ("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch <team>`" , err )
367+ } else if err != nil {
368+ return "" , fmt .Errorf ("failed to get team name from config: %w" , err )
369+ }
370+ if teamName == "" {
371+ return "" , fmt .Errorf ("team name not set. Hint: use `cloudquery switch <team>`" )
372+ }
373+ return teamName , nil
374+ case auth .APIKey :
375+ resp , err := u .apiClient .ListTeamsWithResponse (context .Background (), & cqapi.ListTeamsParams {})
376+ if err != nil {
377+ return "" , fmt .Errorf ("failed to list teams for API key: %w" , err )
378+ }
379+ if resp .StatusCode () != http .StatusOK {
380+ return "" , fmt .Errorf ("failed to list teams for API key, status code: %s" , resp .Status ())
381+ }
382+ if len (resp .JSON200 .Items ) != 1 {
383+ return "" , fmt .Errorf ("expected to find exactly one team for API key, found %d" , len (resp .JSON200 .Items ))
384+ }
385+ return resp .JSON200 .Items [0 ].Name , nil
386+ default :
387+ return "" , fmt .Errorf ("unsupported token type: %v" , tokenType )
388+ }
389+ }
0 commit comments