GetTeamForToken returns the team for the given token If the token is a bearer token, we need to get the team from the configuration. For api keys the team name is not required as the key is bound to a team name already.
(ctx context.Context, token auth.Token)
| 33 | // If the token is a bearer token, we need to get the team from the configuration. |
| 34 | // For api keys the team name is not required as the key is bound to a team name already. |
| 35 | func GetTeamForToken(ctx context.Context, token auth.Token) (string, error) { |
| 36 | switch token.Type { |
| 37 | case auth.BearerToken: |
| 38 | team, err := config.GetValue("team") |
| 39 | if configFileMissing(err) || emptyTeam(err, team) { |
| 40 | teams := getAvailableUserTeams(ctx, token) |
| 41 | if len(teams) > 0 { |
| 42 | return "", fmt.Errorf("team is not set.\n\nAvailable teams: %s.\n\nTo set your team, run `cloudquery switch <team>`", strings.Join(teams, ", ")) |
| 43 | } |
| 44 | return "", errors.New("team is not set.\n\n. Hint: use `cloudquery login` and/or `cloudquery switch <team>`") |
| 45 | } |
| 46 | if err != nil { |
| 47 | return "", fmt.Errorf("failed to get team name from config: %w. Hint: use `cloudquery login` and/or `cloudquery switch <team>`", err) |
| 48 | } |
| 49 | return team, nil |
| 50 | case auth.APIKey: |
| 51 | cl, err := teamapi.NewClient(token.Value) |
| 52 | if err != nil { |
| 53 | return "", fmt.Errorf("failed to create API client for api key: %w", err) |
| 54 | } |
| 55 | teams, err := cl.ListAllTeams(ctx) |
| 56 | if err != nil { |
| 57 | return "", fmt.Errorf("failed to list teams for api key: %w", err) |
| 58 | } |
| 59 | switch l := len(teams); l { |
| 60 | case 0: |
| 61 | return "", errors.New("team api key has no assigned team") |
| 62 | case 1: |
| 63 | return teams[0].Name, nil |
| 64 | default: |
| 65 | return "", fmt.Errorf("team api key has more than one team: %s", teamapi.Names(teams)) |
| 66 | } |
| 67 | default: |
| 68 | return os.Getenv("_CQ_TEAM_NAME"), nil |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // IsTeamInternal checks if the team has the internal flag set |
| 73 | // We store this in the configuration on team switch. |
nothing calls this directly
no test coverage detected