MCPcopy Index your code
hub / github.com/cloudquery/cloudquery / recommendedVersionFromWhoami

Function recommendedVersionFromWhoami

cli/internal/platform/inject.go:166–210  ·  view source on GitHub ↗

recommendedVersionFromWhoami asks the tenant's platform — whoami, reached via the token's api_url (`u`) and authed with the token — for the recommended destination plugin version. It lets the headless flow pin the right plugin without a session mint (which is where the non-headless flow gets it). Be

(ctx context.Context, logger zerolog.Logger, cqpdToken string)

Source from the content-addressed store, hash-verified

164// without a session mint (which is where the non-headless flow gets it).
165// Best-effort: "" on any failure, so the caller falls back to the CLI default.
166func recommendedVersionFromWhoami(ctx context.Context, logger zerolog.Logger, cqpdToken string) string {
167 apiURL := apiURLFromToken(cqpdToken)
168 if apiURL == "" {
169 logger.Debug().Msg("platform destination: token carries no api_url; skipping whoami version lookup, using default")
170 return ""
171 }
172 base := strings.TrimRight(apiURL, "/")
173 if !strings.HasSuffix(base, "/api") { // /external-syncs/* is served under /api
174 base += "/api"
175 }
176 url := base + "/external-syncs/whoami"
177 logger.Debug().Str("url", url).Msg("platform destination: looking up recommended plugin version via whoami")
178
179 ctx, cancel := context.WithTimeout(ctx, requestTimeout)
180 defer cancel()
181 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
182 if err != nil {
183 logger.Debug().Err(err).Str("url", url).Msg("platform destination: failed to build whoami request; using default")
184 return ""
185 }
186 req.Header.Set("Authorization", "Bearer "+cqpdToken)
187 resp, err := http.DefaultClient.Do(req)
188 if err != nil {
189 logger.Debug().Err(err).Str("url", url).Msg("platform destination: whoami lookup for recommended plugin version failed; using default")
190 return ""
191 }
192 defer resp.Body.Close()
193 if resp.StatusCode != http.StatusOK {
194 logger.Debug().Int("status", resp.StatusCode).Str("url", url).Msg("platform destination: whoami returned non-200 for version lookup; using default")
195 return ""
196 }
197 var body struct {
198 PluginVersion *string `json:"plugin_version"`
199 }
200 if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
201 logger.Debug().Err(err).Msg("platform destination: failed to decode whoami response; using default")
202 return ""
203 }
204 if body.PluginVersion == nil {
205 logger.Debug().Msg("platform destination: whoami returned no plugin_version; using default")
206 return ""
207 }
208 logger.Debug().Str("plugin_version", *body.PluginVersion).Msg("platform destination: pinning recommended plugin version from whoami")
209 return *body.PluginVersion
210}
211
212// DownloadAuth resolves the credential and team used to download (and meter)
213// plugins. In the headless platform-destination flow — a cqpd_ token in

Callers 1

MaybeInjectDestinationFunction · 0.85

Calls 3

apiURLFromTokenFunction · 0.85
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected