-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathrestore.go
More file actions
205 lines (176 loc) · 7.37 KB
/
restore.go
File metadata and controls
205 lines (176 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package restore
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client"
mongodbUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/wait"
)
const (
instanceIdFlag = "instance-id"
backupInstanceIdFlag = "backup-instance-id"
backupIdFlag = "backup-id"
timestampFlag = "timestamp"
)
type inputModel struct {
*globalflags.GlobalFlagModel
InstanceId string
BackupInstanceId string
BackupId string
Timestamp string
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "restore",
Short: "Restores a MongoDB Flex instance from a backup",
Long: fmt.Sprintf("%s\n%s\n%s",
"Restores a MongoDB Flex instance from a backup of an instance or clones a MongoDB Flex instance from a point-in-time backup.",
"The backup can be specified by a backup ID or a timestamp.",
"You can specify the instance to which the backup will be applied. If not specified, the backup will be applied to the same instance from which it was taken.",
),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Restore a MongoDB Flex instance with ID "yyy" using backup with ID "zzz"`,
`$ stackit mongodbflex backup restore --instance-id yyy --backup-id zzz`),
examples.NewExample(
`Clone a MongoDB Flex instance with ID "yyy" via point-in-time restore to timestamp "2024-05-14T14:31:48Z"`,
`$ stackit mongodbflex backup restore --instance-id yyy --timestamp 2024-05-14T14:31:48Z`),
examples.NewExample(
`Restore a MongoDB Flex instance with ID "yyy", using backup from instance with ID "zzz" with backup ID "xxx"`,
`$ stackit mongodbflex backup restore --instance-id zzz --backup-instance-id yyy --backup-id xxx`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
instanceLabel, err := mongodbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
instanceLabel = model.ProjectId
}
prompt := fmt.Sprintf("Are you sure you want to restore MongoDB Flex instance %q?", instanceLabel)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
// If backupInstanceId is not provided, the target is the same instance as the backup
if model.BackupInstanceId == "" {
model.BackupInstanceId = model.InstanceId
}
isRestoreOperation := getIsRestoreOperation(model.BackupId, model.Timestamp)
// If backupId is provided, restore the instance from the backup with the backupId
if isRestoreOperation {
req := buildRestoreRequest(ctx, model, apiClient)
_, err = req.Execute()
if err != nil {
return fmt.Errorf("restore MongoDB Flex instance: %w", err)
}
if !model.Async {
s := spinner.New(params.Printer)
s.Start("Restoring instance")
_, err = wait.RestoreInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId, model.BackupId, model.Region).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for MongoDB Flex instance restoration: %w", err)
}
s.Stop()
}
operationState := "Restored"
if model.Async {
operationState = "Triggered restore of"
}
params.Printer.Outputf("%s instance %q with backup %q\n", operationState, model.InstanceId, model.BackupId)
return nil
}
// Else, if timestamp is provided, clone the instance from a point-in-time snapshot
req := buildCloneRequest(ctx, model, apiClient)
_, err = req.Execute()
if err != nil {
return fmt.Errorf("clone MongoDB Flex instance: %w", err)
}
if !model.Async {
s := spinner.New(params.Printer)
s.Start("Cloning instance")
_, err = wait.CloneInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId, model.Region).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for MongoDB Flex instance cloning: %w", err)
}
s.Stop()
}
operationState := "Cloned"
if model.Async {
operationState = "Triggered clone of"
}
params.Printer.Outputf("%s instance %q from backup with timestamp %q\n", operationState, model.InstanceId, model.Timestamp)
return nil
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().Var(flags.UUIDFlag(), backupInstanceIdFlag, "Instance ID of the target instance to restore the backup to")
cmd.Flags().String(backupIdFlag, "", "Backup ID")
cmd.Flags().String(timestampFlag, "", "Timestamp to restore the instance to, in a date-time with the RFC3339 layout format, e.g. 2024-01-01T00:00:00Z")
err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
backupId := flags.FlagToStringValue(p, cmd, backupIdFlag)
timestamp := flags.FlagToStringValue(p, cmd, timestampFlag)
if backupId != "" && timestamp != "" || backupId == "" && timestamp == "" {
return nil, &cliErr.RequiredMutuallyExclusiveFlagsError{
Flags: []string{backupIdFlag, timestampFlag},
}
}
model := inputModel{
GlobalFlagModel: globalFlags,
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
BackupInstanceId: flags.FlagToStringValue(p, cmd, backupInstanceIdFlag),
BackupId: flags.FlagToStringValue(p, cmd, backupIdFlag),
Timestamp: flags.FlagToStringValue(p, cmd, timestampFlag),
}
p.DebugInputModel(model)
return &model, nil
}
func buildRestoreRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex.APIClient) mongodbflex.ApiRestoreInstanceRequest {
req := apiClient.RestoreInstance(ctx, model.ProjectId, model.InstanceId, model.Region)
req = req.RestoreInstancePayload(mongodbflex.RestoreInstancePayload{
BackupId: &model.BackupId,
InstanceId: &model.BackupInstanceId,
})
return req
}
func buildCloneRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex.APIClient) mongodbflex.ApiCloneInstanceRequest {
req := apiClient.CloneInstance(ctx, model.ProjectId, model.InstanceId, model.Region)
req = req.CloneInstancePayload(mongodbflex.CloneInstancePayload{
Timestamp: &model.Timestamp,
InstanceId: &model.BackupInstanceId,
})
return req
}
func getIsRestoreOperation(backupId, timestamp string) bool {
return backupId != "" && timestamp == ""
}