Skip to content

Commit 65099c9

Browse files
committed
refactor(ufw):
- clean & refactored list implementation - added describe implementation
1 parent db95609 commit 65099c9

3 files changed

Lines changed: 126 additions & 7 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,123 @@
11
package describe
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/tables"
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+
)
19+
20+
const (
21+
instanceIdArg = "INSTANCE_ID"
22+
)
23+
24+
type inputModel struct {
25+
*globalflags.GlobalFlagModel
26+
InstanceId string
27+
}
28+
29+
func NewCmd(params *types.CmdParams) *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: fmt.Sprintf("describe %s", instanceIdArg),
32+
Short: "Shows details of an UFW rule instance",
33+
Long: "Shows details of an STACKIT Unified Firewall (UFW) rule instance.",
34+
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
35+
Example: examples.Build(
36+
examples.NewExample(
37+
`Get details of an UFW rule instance with ID "xxx"`,
38+
"$ stackit ufw rule instance describe xxx"),
39+
examples.NewExample(
40+
`Get details of an UFW rule instance with ID "xxx" in JSON format`,
41+
"$ stackit ufw rule instance describe xxx --output-format json"),
42+
),
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
ctx := context.Background()
45+
model, err := parseInput(params.Printer, cmd, args)
46+
if err != nil {
47+
return err
48+
}
49+
50+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
51+
if err != nil {
52+
return err
53+
}
54+
55+
req := buildRequest(ctx, model, apiClient)
56+
resp, err := req.Execute()
57+
if err != nil {
58+
return fmt.Errorf("read UFW rule instance: %w", err)
59+
}
60+
61+
return outputResult(params.Printer, model.OutputFormat, resp)
62+
},
63+
}
64+
return cmd
65+
}
66+
67+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
68+
instanceId := inputArgs[0]
69+
70+
globalFlags := globalflags.Parse(p, cmd)
71+
if globalFlags.ProjectId == "" {
72+
return nil, &errors.ProjectIdError{}
73+
}
74+
75+
if globalFlags.Region == "" {
76+
return nil, &errors.RegionError{}
77+
}
78+
79+
model := inputModel{
80+
GlobalFlagModel: globalFlags,
81+
InstanceId: instanceId,
82+
}
83+
84+
p.DebugInputModel(model)
85+
return &model, nil
86+
}
87+
88+
func buildRequest(ctx context.Context, model *inputModel, apiClient *ufw.APIClient) ufw.ApiGetRuleRequest {
89+
return apiClient.DefaultAPI.GetRule(ctx, model.ProjectId, model.Region, model.InstanceId)
90+
}
91+
92+
func outputResult(p *print.Printer, outputFormat string, rule *ufw.RuleResponse) error {
93+
return p.OutputResult(outputFormat, rule, func() error {
94+
if rule == nil {
95+
return fmt.Errorf("no instance rule passed")
96+
}
97+
98+
table := tables.NewTable()
99+
table.AddRow("PRODUCT", utils.PtrString(&rule.Product))
100+
table.AddSeparator()
101+
table.AddRow("SOURCE", utils.PtrString(&rule.SourceIP))
102+
table.AddSeparator()
103+
table.AddRow("DEPLOYMENT TARGET", utils.PtrString(&rule.InstanceName))
104+
table.AddSeparator()
105+
table.AddRow("PROTOCOL", utils.PtrString(&rule.Protocol))
106+
table.AddSeparator()
107+
table.AddRow("DIRECTION", utils.PtrString(&rule.Direction))
108+
table.AddSeparator()
109+
table.AddRow("PORT RANGE", fmt.Sprintf("%d", rule.PortRange))
110+
table.AddSeparator()
111+
table.AddRow("ETHER TYPE", utils.PtrString(&rule.EtherType))
112+
table.AddSeparator()
113+
table.AddRow("STATUS", utils.PtrString(&rule.Status))
114+
table.AddSeparator()
115+
116+
err := table.Display(p)
117+
if err != nil {
118+
return fmt.Errorf("render table: %w", err)
119+
}
120+
121+
return nil
122+
})
123+
}

‎internal/cmd/ufw/rules/list/list.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,10 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
105105
return cmd
106106
}
107107

108-
// Configure command flags (type, default value, and description)
109108
func configureFlags(cmd *cobra.Command) {
110109
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
111110
}
112111

113-
// Parse command input and return a standardized model
114112
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
115113
globalFlags := globalflags.Parse(p, cmd)
116114
if globalFlags.ProjectId == "" {
@@ -138,13 +136,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
138136
return &model, nil
139137
}
140138

141-
// Build request to the API
142139
func buildRequest(ctx context.Context, model *inputModel, apiClient *ufw.APIClient) ufw.ApiListRulesRequest {
143140
req := apiClient.DefaultAPI.ListRules(ctx, model.ProjectId, model.Region)
144141
return req
145142
}
146143

147-
// Output result based on the configured output format
148144
func outputResult(p *print.Printer, outputFormat, projectLabel string, resources []ufw.RuleResponse) error {
149145
return p.OutputResult(outputFormat, resources, func() error {
150146
if len(resources) == 0 {
@@ -153,11 +149,11 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resources
153149
}
154150

155151
table := tables.NewTable()
156-
table.SetHeader("PRODUCT", "DEPLOYMENT TARGET", "PROTOCOL",
152+
table.SetHeader("PRODUCT", "SOURCE", "DEPLOYMENT TARGET", "PROTOCOL",
157153
"DIRECTION", "PORT RANGE", "ETHER TYPE", "STATUS")
158154
for i := range resources {
159155
resource := resources[i]
160-
table.AddRow(resource.Product, resource.InstanceName, resource.Protocol, resource.Direction,
156+
table.AddRow(resource.Product, resource.SourceIP, resource.InstanceName, resource.Protocol, resource.Direction,
161157
resource.PortRange, resource.EtherType, resource.Status)
162158
}
163159
err := table.Display(p)

‎internal/cmd/ufw/rules/rules.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rules
22

33
import (
44
"github.com/spf13/cobra"
5+
"github.com/stackitcloud/stackit-cli/internal/cmd/ufw/rules/describe"
56
"github.com/stackitcloud/stackit-cli/internal/cmd/ufw/rules/list"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
@@ -22,7 +23,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
2223

2324
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2425
cmd.AddCommand(list.NewCmd(params))
25-
//cmd.AddCommand(describe.NewCmd(params))
26+
cmd.AddCommand(describe.NewCmd(params))
2627
//cmd.AddCommand(create.NewCmd(params))
2728
//cmd.AddCommand(delete.NewCmd(params))
2829
//cmd.AddCommand(update.NewCmd(params))

0 commit comments

Comments
 (0)