Skip to content

Commit 0fba4e5

Browse files
committed
refactor(ufw):
- added delete implementation
1 parent 65099c9 commit 0fba4e5

2 files changed

Lines changed: 113 additions & 2 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
11
package delete
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/ufw/client"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
17+
ufw "github.com/stackitcloud/stackit-sdk-go/services/ufw/v1api"
18+
"github.com/stackitcloud/stackit-sdk-go/services/ufw/v1api/wait"
19+
)
20+
21+
const (
22+
instanceIdArg = "INSTANCE_ID"
23+
)
24+
25+
type inputModel struct {
26+
*globalflags.GlobalFlagModel
27+
InstanceId string
28+
}
29+
30+
func NewCmd(params *types.CmdParams) *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: fmt.Sprintf("delete %s", instanceIdArg),
33+
Short: "Deletes a UFW rule instance",
34+
Long: "Deletes a UFW rule instance.",
35+
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
36+
Example: examples.Build(
37+
examples.NewExample(
38+
`Delete a UFW rule instance with ID "xxx"`,
39+
"$ stackit ufw instance delete xxx"),
40+
),
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
ctx := context.Background()
43+
model, err := parseInput(params.Printer, cmd, args)
44+
if err != nil {
45+
return err
46+
}
47+
48+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
49+
if err != nil {
50+
return err
51+
}
52+
53+
prompt := fmt.Sprintf("Are you sure you want to delete instance %q? (This cannot be undone)", model.InstanceId)
54+
err = params.Printer.PromptForConfirmation(prompt)
55+
if err != nil {
56+
return err
57+
}
58+
59+
req := buildRequest(ctx, model, apiClient)
60+
_, err = req.Execute()
61+
if err != nil {
62+
return fmt.Errorf("delete UFW rule instance: %w", err)
63+
}
64+
65+
// Wait for async operation, if async mode not enabled
66+
if !model.Async {
67+
err := spinner.Run(params.Printer, "Deleting instance", func() error {
68+
_, err = wait.DeleteRuleWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId).WaitWithContext(ctx)
69+
return err
70+
})
71+
if err != nil {
72+
return fmt.Errorf("wait for UFW rule instance deletion: %w", err)
73+
}
74+
}
75+
76+
operationState := "Deleted"
77+
if model.Async {
78+
operationState = "Triggered deletion of"
79+
}
80+
params.Printer.Outputf("%s instance %q\n", operationState, model.InstanceId)
81+
return nil
82+
},
83+
}
84+
return cmd
85+
}
86+
87+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
88+
instanceId := inputArgs[0]
89+
90+
globalFlags := globalflags.Parse(p, cmd)
91+
if globalFlags.ProjectId == "" {
92+
return nil, &errors.ProjectIdError{}
93+
}
94+
95+
if globalFlags.Region == "" {
96+
return nil, &errors.RegionError{}
97+
}
98+
99+
model := inputModel{
100+
GlobalFlagModel: globalFlags,
101+
InstanceId: instanceId,
102+
}
103+
104+
p.DebugInputModel(model)
105+
return &model, nil
106+
}
107+
108+
func buildRequest(ctx context.Context, model *inputModel, apiClient *ufw.APIClient) ufw.ApiDeleteRuleRequest {
109+
return apiClient.DefaultAPI.DeleteRule(ctx, model.ProjectId, model.Region, model.InstanceId)
110+
}

‎internal/cmd/ufw/ufw.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ufw
22

33
import (
44
"github.com/spf13/cobra"
5+
"github.com/stackitcloud/stackit-cli/internal/cmd/ufw/rules/delete"
6+
"github.com/stackitcloud/stackit-cli/internal/cmd/ufw/rules/describe"
57
"github.com/stackitcloud/stackit-cli/internal/cmd/ufw/rules/list"
68
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
79
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
@@ -22,8 +24,8 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
2224

2325
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2426
cmd.AddCommand(list.NewCmd(params))
25-
//cmd.AddCommand(describe.NewCmd(params))
27+
cmd.AddCommand(describe.NewCmd(params))
2628
//cmd.AddCommand(create.NewCmd(params))
2729
//cmd.AddCommand(update.NewCmd(params))
28-
//cmd.AddCommand(delete.NewCmd(params))
30+
cmd.AddCommand(delete.NewCmd(params))
2931
}

0 commit comments

Comments
 (0)