|
| 1 | +package restore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 17 | + |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + backupIdArg = "BACKUP_ID" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + BackupId string |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: fmt.Sprintf("restore %s", backupIdArg), |
| 34 | + Short: "Restores a backup", |
| 35 | + Long: "Restores a backup by its ID.", |
| 36 | + Args: args.SingleArg(backupIdArg, utils.ValidateUUID), |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `Restore a backup`, |
| 40 | + "$ stackit volume backup restore xxx-xxx-xxx"), |
| 41 | + examples.NewExample( |
| 42 | + `Restore a backup and wait for restore to be completed`, |
| 43 | + "$ stackit volume backup restore xxx-xxx-xxx --async=false"), |
| 44 | + ), |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + ctx := context.Background() |
| 47 | + model, err := parseInput(params.Printer, cmd, args) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + // Configure API client |
| 53 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + // Get backup details for labels |
| 59 | + backup, err := apiClient.GetBackup(ctx, model.ProjectId, model.BackupId).Execute() |
| 60 | + if err != nil { |
| 61 | + params.Printer.Debug(print.ErrorLevel, "get backup details: %v", err) |
| 62 | + } |
| 63 | + backupLabel := model.BackupId |
| 64 | + if backup != nil && backup.Name != nil { |
| 65 | + backupLabel = *backup.Name |
| 66 | + } |
| 67 | + |
| 68 | + // Get source details for labels |
| 69 | + var sourceLabel string |
| 70 | + if backup != nil && backup.VolumeId != nil { |
| 71 | + volume, err := apiClient.GetVolume(ctx, model.ProjectId, *backup.VolumeId).Execute() |
| 72 | + if err != nil { |
| 73 | + params.Printer.Debug(print.ErrorLevel, "get volume details: %v", err) |
| 74 | + sourceLabel = *backup.VolumeId |
| 75 | + } else if volume.Name != nil { |
| 76 | + sourceLabel = *volume.Name |
| 77 | + } else { |
| 78 | + sourceLabel = *backup.VolumeId |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if !model.AssumeYes { |
| 83 | + prompt := fmt.Sprintf("Are you sure you want to restore %q with backup %q? (This cannot be undone)", sourceLabel, backupLabel) |
| 84 | + err = params.Printer.PromptForConfirmation(prompt) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Call API |
| 91 | + req := buildRequest(ctx, model, apiClient) |
| 92 | + err = req.Execute() |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("restore backup: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Wait for async operation, if async mode not enabled |
| 98 | + if !model.Async { |
| 99 | + s := spinner.New(params.Printer) |
| 100 | + s.Start("Restoring backup") |
| 101 | + _, err = wait.RestoreBackupWaitHandler(ctx, apiClient, model.ProjectId, model.BackupId).WaitWithContext(ctx) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("wait for backup restore: %w", err) |
| 104 | + } |
| 105 | + s.Stop() |
| 106 | + } |
| 107 | + |
| 108 | + projectLabel := model.ProjectId |
| 109 | + |
| 110 | + if model.Async { |
| 111 | + params.Printer.Info("Triggered restore of %q with %q in %q\n", sourceLabel, backupLabel, projectLabel) |
| 112 | + } else { |
| 113 | + params.Printer.Info("Restored %q with %q in %q\n", sourceLabel, backupLabel, projectLabel) |
| 114 | + } |
| 115 | + return nil |
| 116 | + }, |
| 117 | + } |
| 118 | + return cmd |
| 119 | +} |
| 120 | + |
| 121 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 122 | + backupId := inputArgs[0] |
| 123 | + |
| 124 | + globalFlags := globalflags.Parse(p, cmd) |
| 125 | + if globalFlags.ProjectId == "" { |
| 126 | + return nil, &errors.ProjectIdError{} |
| 127 | + } |
| 128 | + |
| 129 | + model := inputModel{ |
| 130 | + GlobalFlagModel: globalFlags, |
| 131 | + BackupId: backupId, |
| 132 | + } |
| 133 | + |
| 134 | + if p.IsVerbosityDebug() { |
| 135 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 136 | + if err != nil { |
| 137 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 138 | + } else { |
| 139 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return &model, nil |
| 144 | +} |
| 145 | + |
| 146 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiRestoreBackupRequest { |
| 147 | + req := apiClient.RestoreBackup(ctx, model.ProjectId, model.BackupId) |
| 148 | + return req |
| 149 | +} |
0 commit comments