DetectTenant reports the CloudQuery Platform tenant a sync would auto-inject into — for commands (e.g. init) that want to skip the destination and tell the user where data lands. ok is true when a tenant is found; apiURL is its base URL (host only, no /api), which may be empty if a directly supplied
(ctx context.Context, token, teamName string)
| 93 | // CQ_PLATFORM_TOKEN predates url-carrying tokens. Best-effort: any lookup |
| 94 | // failure returns ("", false) so callers fall back to normal behavior. |
| 95 | func DetectTenant(ctx context.Context, token, teamName string) (apiURL string, ok bool) { |
| 96 | if os.Getenv(envDisable) == "1" { |
| 97 | return "", false |
| 98 | } |
| 99 | // A directly supplied cqpd_ token already identifies the tenant; read its URL. |
| 100 | if t := platformToken(); t != "" { |
| 101 | return apiURLFromToken(t), true |
| 102 | } |
| 103 | if token == "" || teamName == "" { |
| 104 | return "", false |
| 105 | } |
| 106 | cl, err := api.NewClient(token) |
| 107 | if err != nil { |
| 108 | return "", false |
| 109 | } |
| 110 | tenants, err := activeTenants(ctx, cl, teamName) |
| 111 | if err != nil || len(tenants) == 0 { |
| 112 | return "", false |
| 113 | } |
| 114 | // Use the same selection as auto-injection (resolveTenant): the only active |
| 115 | // tenant, or the CQ_PLATFORM_TENANT_ID match. None or ambiguous (several |
| 116 | // active, no override) → report nothing; init is informational, so it never |
| 117 | // errors here, and it won't point at a tenant a real sync would skip. |
| 118 | tenant, err := resolveTenant(tenants) |
| 119 | if err != nil { |
| 120 | return "", false |
| 121 | } |
| 122 | return "https://" + tenant.Host, true |
| 123 | } |
| 124 | |
| 125 | // apiURLFromToken reads the api_url (`u`) claim from a cqpd_ token's payload |
| 126 | // without verifying the signature. Returns "" for a malformed token or one that |