CreateApplicationWithRetry creates an application, retrying with a different region if the selected one has no available cluster. The optional tracker (nil-safe) records which step the flow is in, so the telemetry of the calling flow can tell where the user stopped.
( io *iostreams.IOStreams, client *dashboard.Client, accessToken string, region string, appName string, tracker *telemetry.FlowTracker, )
| 20 | // The optional tracker (nil-safe) records which step the flow is in, so the |
| 21 | // telemetry of the calling flow can tell where the user stopped. |
| 22 | func CreateApplicationWithRetry( |
| 23 | io *iostreams.IOStreams, |
| 24 | client *dashboard.Client, |
| 25 | accessToken string, |
| 26 | region string, |
| 27 | appName string, |
| 28 | tracker *telemetry.FlowTracker, |
| 29 | ) (*dashboard.Application, string, error) { |
| 30 | cs := io.ColorScheme() |
| 31 | |
| 32 | for { |
| 33 | if region == "" { |
| 34 | tracker.SetStep(telemetry.StepRegion) |
| 35 | var err error |
| 36 | region, err = PromptRegion(io, client, accessToken) |
| 37 | if err != nil { |
| 38 | return nil, "", err |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | tracker.SetStep(telemetry.StepAPICall) |
| 43 | io.StartProgressIndicatorWithLabel("Creating application") |
| 44 | app, err := client.CreateApplication(accessToken, region, appName) |
| 45 | io.StopProgressIndicator() |
| 46 | |
| 47 | if err == nil { |
| 48 | fmt.Fprintf(io.Out, "%s Application %s created in region %q\n", |
| 49 | cs.SuccessIcon(), cs.Bold(app.ID), region) |
| 50 | return app, region, nil |
| 51 | } |
| 52 | |
| 53 | var clusterErr *dashboard.ErrClusterUnavailable |
| 54 | if errors.As(err, &clusterErr) { |
| 55 | fmt.Fprintf(io.Out, "%s No cluster available in region %q. Please select another region.\n", |
| 56 | cs.WarningIcon(), region) |
| 57 | region = "" |
| 58 | |
| 59 | if !io.CanPrompt() { |
| 60 | return nil, "", fmt.Errorf("no cluster available in region %q — try a different --region", clusterErr.Region) |
| 61 | } |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | return nil, "", fmt.Errorf("application creation failed: %w", err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // PromptRegion fetches regions from the API and prompts the user to select one. |
| 70 | func PromptRegion( |
no test coverage detected