queryCAPIStatus checks if the Central API is reachable, and if the credentials are correct. It then checks if the instance is enrolled in the console.
(ctx context.Context, db *database.Client, hub *cwhub.Hub, credURL string, login string, password string)
| 166 | |
| 167 | // queryCAPIStatus checks if the Central API is reachable, and if the credentials are correct. It then checks if the instance is enrolled in the console. |
| 168 | func queryCAPIStatus(ctx context.Context, db *database.Client, hub *cwhub.Hub, credURL string, login string, password string) (capiStatus, error) { |
| 169 | apiURL, err := url.Parse(credURL) |
| 170 | if err != nil { |
| 171 | return capiStatus{}, err |
| 172 | } |
| 173 | |
| 174 | itemsForAPI := hub.GetInstalledListForAPI() |
| 175 | |
| 176 | passwd := strfmt.Password(password) |
| 177 | |
| 178 | client := apiclient.NewClient(&apiclient.Config{ |
| 179 | MachineID: login, |
| 180 | Password: passwd, |
| 181 | URL: apiURL, |
| 182 | // I don't believe papi is needed to check enrollement |
| 183 | // PapiURL: papiURL, |
| 184 | VersionPrefix: "v3", |
| 185 | UpdateScenario: func(_ context.Context) ([]string, error) { |
| 186 | return itemsForAPI, nil |
| 187 | }, |
| 188 | }) |
| 189 | |
| 190 | pw := strfmt.Password(password) |
| 191 | |
| 192 | t := models.WatcherAuthRequest{ |
| 193 | MachineID: &login, |
| 194 | Password: &pw, |
| 195 | Scenarios: itemsForAPI, |
| 196 | } |
| 197 | |
| 198 | authResp, _, err := client.Auth.AuthenticateWatcher(ctx, t) |
| 199 | if err != nil { |
| 200 | return capiStatus{}, err |
| 201 | } |
| 202 | |
| 203 | if err := db.SaveAPICToken(ctx, authResp.Token); err != nil { |
| 204 | return capiStatus{}, err |
| 205 | } |
| 206 | |
| 207 | client.GetClient().Transport.(*apiclient.JWTTransport).Token = authResp.Token |
| 208 | |
| 209 | if client.IsEnrolled() { |
| 210 | return capiStatus{true, true, client.GetSubscriptionType()}, nil |
| 211 | } |
| 212 | |
| 213 | return capiStatus{true, false, ""}, nil |
| 214 | } |
| 215 | |
| 216 | func (cli *cliCapi) Status(ctx context.Context, db *database.Client, out io.Writer, hub *cwhub.Hub) error { |
| 217 | cfg := cli.cfg() |
no test coverage detected
searching dependent graphs…