-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdelete.go
204 lines (175 loc) · 6.51 KB
/
delete.go
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
package delete
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"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/postgresflex/client"
postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/wait"
)
const (
instanceIdArg = "INSTANCE_ID"
forceDeleteFlag = "force"
)
type inputModel struct {
*globalflags.GlobalFlagModel
InstanceId string
ForceDelete bool
}
func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("delete %s", instanceIdArg),
Short: "Deletes a PostgreSQL Flex instance",
Long: fmt.Sprintf("%s\n%s\n%s",
"Deletes a PostgreSQL Flex instance.",
"By default, instances will be kept in a delayed deleted state for 7 days before being permanently deleted.",
"Use the --force flag to force the immediate deletion of a delayed deleted instance.",
),
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Delete a PostgreSQL Flex instance with ID "xxx"`,
"$ stackit postgresflex instance delete xxx"),
examples.NewExample(
`Force the deletion of a delayed deleted PostgreSQL Flex instance with ID "xxx"`,
"$ stackit postgresflex instance delete xxx --force"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}
instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
if err != nil {
p.Debug(print.ErrorLevel, "get instance name: %v", err)
instanceLabel = model.InstanceId
}
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
}
}
toDelete, toForceDelete, err := getNextOperations(ctx, model, apiClient)
if err != nil {
return err
}
if toDelete {
// Call API
delReq := buildDeleteRequest(ctx, model, apiClient)
err = delReq.Execute()
if err != nil {
return fmt.Errorf("delete PostgreSQL Flex instance: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(p)
s.Start("Deleting instance")
_, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for PostgreSQL Flex instance deletion: %w", err)
}
s.Stop()
}
}
if toForceDelete {
// Call API
forceDelReq := buildForceDeleteRequest(ctx, model, apiClient)
err = forceDelReq.Execute()
if err != nil {
return fmt.Errorf("force delete PostgreSQL Flex instance: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(p)
s.Start("Forcing deletion of instance")
_, err = wait.ForceDeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for PostgreSQL Flex instance force deletion: %w", err)
}
s.Stop()
}
}
operationState := "Deleted"
if toForceDelete {
operationState = "Forcefully deleted"
}
if model.Async {
operationState = "Triggered deletion of"
if toForceDelete {
operationState = "Triggered forced deletion of"
}
}
p.Info("%s instance %q\n", operationState, instanceLabel)
return nil
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().BoolP(forceDeleteFlag, "f", false, "Force deletion of a delayed deleted instance")
}
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
instanceId := inputArgs[0]
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}
model := inputModel{
GlobalFlagModel: globalFlags,
InstanceId: instanceId,
ForceDelete: flags.FlagToBoolValue(p, cmd, forceDeleteFlag),
}
if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}
return &model, nil
}
func buildDeleteRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiDeleteInstanceRequest {
req := apiClient.DeleteInstance(ctx, model.ProjectId, model.InstanceId)
return req
}
func buildForceDeleteRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiForceDeleteInstanceRequest {
req := apiClient.ForceDeleteInstance(ctx, model.ProjectId, model.InstanceId)
return req
}
type PostgreSQLFlexClient interface {
GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*postgresflex.InstanceResponse, error)
ListVersionsExecute(ctx context.Context, projectId string) (*postgresflex.ListVersionsResponse, error)
GetUserExecute(ctx context.Context, projectId, instanceId, userId string) (*postgresflex.GetUserResponse, error)
}
func getNextOperations(ctx context.Context, model *inputModel, apiClient PostgreSQLFlexClient) (toDelete, toForceDelete bool, err error) {
instanceStatus, err := postgresflexUtils.GetInstanceStatus(ctx, apiClient, model.ProjectId, model.InstanceId)
if err != nil {
return false, false, fmt.Errorf("get PostgreSQL Flex instance status: %w", err)
}
if instanceStatus == wait.InstanceStateDeleted {
if !model.ForceDelete {
return false, false, fmt.Errorf("instance is already deleted, use --force to force the deletion of a delayed deleted instance")
}
return false, model.ForceDelete, nil
}
return true, model.ForceDelete, nil
}