Skip to content

Commit a796b41

Browse files
authored
Add sqlserverflex instance delete/describe commands (#347)
* Implement delete and describe commands * Improvements to mongodb and postgresflex commands * Generate docs * Remove extra separator
1 parent e7956de commit a796b41

File tree

11 files changed

+808
-7
lines changed

11 files changed

+808
-7
lines changed

docs/stackit_sqlserverflex_instance.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ stackit sqlserverflex instance [flags]
3030

3131
* [stackit sqlserverflex](./stackit_sqlserverflex.md) - Provides functionality for SQLServer Flex
3232
* [stackit sqlserverflex instance create](./stackit_sqlserverflex_instance_create.md) - Creates an SQLServer Flex instance
33+
* [stackit sqlserverflex instance delete](./stackit_sqlserverflex_instance_delete.md) - Deletes an SQLServer Flex instance
34+
* [stackit sqlserverflex instance describe](./stackit_sqlserverflex_instance_describe.md) - Shows details of an SQLServer Flex instance
3335
* [stackit sqlserverflex instance list](./stackit_sqlserverflex_instance_list.md) - Lists all SQLServer Flex instances
3436
* [stackit sqlserverflex instance update](./stackit_sqlserverflex_instance_update.md) - Updates an SQLServer Flex instance
3537

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## stackit sqlserverflex instance delete
2+
3+
Deletes an SQLServer Flex instance
4+
5+
### Synopsis
6+
7+
Deletes an SQLServer Flex instance.
8+
9+
```
10+
stackit sqlserverflex instance delete INSTANCE_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Delete an SQLServer Flex instance with ID "xxx"
17+
$ stackit sqlserverflex instance delete xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit sqlserverflex instance delete"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
34+
```
35+
36+
### SEE ALSO
37+
38+
* [stackit sqlserverflex instance](./stackit_sqlserverflex_instance.md) - Provides functionality for SQLServer Flex instances
39+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## stackit sqlserverflex instance describe
2+
3+
Shows details of an SQLServer Flex instance
4+
5+
### Synopsis
6+
7+
Shows details of an SQLServer Flex instance.
8+
9+
```
10+
stackit sqlserverflex instance describe INSTANCE_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Get details of an SQLServer Flex instance with ID "xxx"
17+
$ stackit sqlserverflex instance describe xxx
18+
19+
Get details of an SQLServer Flex instance with ID "xxx" in JSON format
20+
$ stackit sqlserverflex instance describe xxx --output-format json
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit sqlserverflex instance describe"
27+
```
28+
29+
### Options inherited from parent commands
30+
31+
```
32+
-y, --assume-yes If set, skips all confirmation prompts
33+
--async If set, runs the command asynchronously
34+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
35+
-p, --project-id string Project ID
36+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
37+
```
38+
39+
### SEE ALSO
40+
41+
* [stackit sqlserverflex instance](./stackit_sqlserverflex_instance.md) - Provides functionality for SQLServer Flex instances
42+

internal/cmd/mongodbflex/backup/schedule/schedule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.I
139139
return nil
140140
default:
141141
table := tables.NewTable()
142-
table.AddRow("BACKUP SCHEDULE", *instance.BackupSchedule)
142+
table.AddRow("BACKUP SCHEDULE (UTC)", *instance.BackupSchedule)
143143
table.AddSeparator()
144144
table.AddRow("DAILY SNAPSHOT RETENTION (DAYS)", (*instance.Options)["dailySnapshotRetentionDays"])
145145
table.AddSeparator()

internal/cmd/mongodbflex/instance/describe/describe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.I
134134
table.AddSeparator()
135135
table.AddRow("STATUS", *instance.Status)
136136
table.AddSeparator()
137-
table.AddRow("STORAGE SIZE", *instance.Storage.Size)
137+
table.AddRow("STORAGE SIZE (GB)", *instance.Storage.Size)
138138
table.AddSeparator()
139139
table.AddRow("VERSION", *instance.Version)
140140
table.AddSeparator()
@@ -148,9 +148,9 @@ func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.I
148148
table.AddSeparator()
149149
table.AddRow("CPU", *instance.Flavor.Cpu)
150150
table.AddSeparator()
151-
table.AddRow("RAM", *instance.Flavor.Memory)
151+
table.AddRow("RAM (GB)", *instance.Flavor.Memory)
152152
table.AddSeparator()
153-
table.AddRow("BACKUP SCHEDULE", *instance.BackupSchedule)
153+
table.AddRow("BACKUP SCHEDULE (UTC)", *instance.BackupSchedule)
154154
table.AddSeparator()
155155
err = table.Display(p)
156156
if err != nil {

internal/cmd/postgresflex/instance/describe/describe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *postgresflex.
136136
table.AddSeparator()
137137
table.AddRow("STATUS", cases.Title(language.English).String(*instance.Status))
138138
table.AddSeparator()
139-
table.AddRow("STORAGE SIZE", *instance.Storage.Size)
139+
table.AddRow("STORAGE SIZE (GB)", *instance.Storage.Size)
140140
table.AddSeparator()
141141
table.AddRow("VERSION", *instance.Version)
142142
table.AddSeparator()
@@ -150,7 +150,7 @@ func outputResult(p *print.Printer, outputFormat string, instance *postgresflex.
150150
table.AddSeparator()
151151
table.AddRow("CPU", *instance.Flavor.Cpu)
152152
table.AddSeparator()
153-
table.AddRow("RAM", *instance.Flavor.Memory)
153+
table.AddRow("RAM (GB)", *instance.Flavor.Memory)
154154
table.AddSeparator()
155155
table.AddRow("BACKUP SCHEDULE (UTC)", *instance.BackupSchedule)
156156
table.AddSeparator()
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package delete
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/sqlserverflex/client"
13+
sqlserverflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/sqlserverflex/utils"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
16+
17+
"github.com/spf13/cobra"
18+
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
19+
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/wait"
20+
)
21+
22+
const (
23+
instanceIdArg = "INSTANCE_ID"
24+
)
25+
26+
type inputModel struct {
27+
*globalflags.GlobalFlagModel
28+
InstanceId string
29+
}
30+
31+
func NewCmd(p *print.Printer) *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: fmt.Sprintf("delete %s", instanceIdArg),
34+
Short: "Deletes an SQLServer Flex instance",
35+
Long: "Deletes an SQLServer Flex instance.",
36+
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
37+
Example: examples.Build(
38+
examples.NewExample(
39+
`Delete an SQLServer Flex instance with ID "xxx"`,
40+
"$ stackit sqlserverflex instance delete xxx"),
41+
),
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
ctx := context.Background()
44+
model, err := parseInput(p, cmd, args)
45+
if err != nil {
46+
return err
47+
}
48+
49+
// Configure API client
50+
apiClient, err := client.ConfigureClient(p)
51+
if err != nil {
52+
return err
53+
}
54+
55+
instanceLabel, err := sqlserverflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
56+
if err != nil {
57+
p.Debug(print.ErrorLevel, "get instance name: %v", err)
58+
instanceLabel = model.InstanceId
59+
}
60+
61+
if !model.AssumeYes {
62+
prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", instanceLabel)
63+
err = p.PromptForConfirmation(prompt)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
69+
// Call API
70+
req := buildRequest(ctx, model, apiClient)
71+
err = req.Execute()
72+
if err != nil {
73+
return fmt.Errorf("delete SQLServer Flex instance: %w", err)
74+
}
75+
76+
// Wait for async operation, if async mode not enabled
77+
if !model.Async {
78+
s := spinner.New(p)
79+
s.Start("Deleting instance")
80+
_, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
81+
if err != nil {
82+
return fmt.Errorf("wait for SQLServer Flex instance deletion: %w", err)
83+
}
84+
s.Stop()
85+
}
86+
87+
operationState := "Deleted"
88+
if model.Async {
89+
operationState = "Triggered deletion of"
90+
}
91+
p.Info("%s instance %q\n", operationState, instanceLabel)
92+
return nil
93+
},
94+
}
95+
return cmd
96+
}
97+
98+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
99+
instanceId := inputArgs[0]
100+
101+
globalFlags := globalflags.Parse(p, cmd)
102+
if globalFlags.ProjectId == "" {
103+
return nil, &errors.ProjectIdError{}
104+
}
105+
106+
model := inputModel{
107+
GlobalFlagModel: globalFlags,
108+
InstanceId: instanceId,
109+
}
110+
111+
if p.IsVerbosityDebug() {
112+
modelStr, err := print.BuildDebugStrFromInputModel(model)
113+
if err != nil {
114+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
115+
} else {
116+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
117+
}
118+
}
119+
120+
return &model, nil
121+
}
122+
123+
func buildRequest(ctx context.Context, model *inputModel, apiClient *sqlserverflex.APIClient) sqlserverflex.ApiDeleteInstanceRequest {
124+
req := apiClient.DeleteInstance(ctx, model.ProjectId, model.InstanceId)
125+
return req
126+
}

0 commit comments

Comments
 (0)