Skip to content

Commit 33b630c

Browse files
fix(albwaf): apply review feedback
Relates to STACKITCLI-443
1 parent 47b6663 commit 33b630c

7 files changed

Lines changed: 117 additions & 12 deletions

File tree

internal/cmd/beta/albwaf/albwaf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
func NewCmd(params *types.CmdParams) *cobra.Command {
1313
cmd := &cobra.Command{
1414
Use: "alb-waf",
15-
Short: "Provides functionality for Application Load Balancer Web Application Firwall",
16-
Long: "Provides functionality for Application Load Balancer Web Application Firwall.",
15+
Short: "Provides functionality for Application Load Balancer Web Application Firewall",
16+
Long: "Provides functionality for Application Load Balancer Web Application Firewall.",
1717
Args: args.NoArgs,
1818
Run: utils.CmdHelp,
1919
}

internal/cmd/beta/albwaf/custom-rule-group/create/create.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7474

7575
// Call API
7676
req := buildRequest(ctx, model, apiClient)
77-
_, err = req.Execute()
77+
resp, err := req.Execute()
7878
if err != nil {
7979
return fmt.Errorf("create ALB WAF custom rule group: %w", err)
8080
}
8181

82-
params.Printer.Outputf("Created custom rule group with name %q \n", model.Payload.Name)
83-
return nil
82+
return outputResult(params.Printer, model.OutputFormat, resp)
8483
},
8584
}
8685
configureFlags(cmd)
@@ -124,3 +123,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *albwaf.APIC
124123
req = req.CreateCustomRuleGroupPayload(*model.Payload)
125124
return req
126125
}
126+
127+
func outputResult(p *print.Printer, outputFormat string, resp *albwaf.GetCustomRuleGroupResponse) error {
128+
return p.OutputResult(outputFormat, resp, func() error {
129+
if resp == nil {
130+
p.Outputf("Received empty custom rule group response")
131+
return nil
132+
}
133+
p.Outputf("Created custom rule group with name %q \n", resp.Name)
134+
return nil
135+
})
136+
}

internal/cmd/beta/albwaf/custom-rule-group/create/create_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
albwaf "github.com/stackitcloud/stackit-sdk-go/services/albwaf/v1api"
88

99
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1012
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1113

1214
"github.com/google/go-cmp/cmp"
@@ -171,3 +173,44 @@ func TestBuildRequest(t *testing.T) {
171173
})
172174
}
173175
}
176+
177+
func TestOutputResult(t *testing.T) {
178+
tests := []struct {
179+
description string
180+
outputFormat string
181+
resp *albwaf.GetCustomRuleGroupResponse
182+
wantErr bool
183+
}{
184+
{
185+
description: "empty",
186+
outputFormat: "",
187+
resp: nil,
188+
wantErr: false,
189+
},
190+
{
191+
description: "base",
192+
outputFormat: "",
193+
resp: &albwaf.GetCustomRuleGroupResponse{
194+
Name: "test-custom-rule-group",
195+
},
196+
wantErr: false,
197+
},
198+
{
199+
description: "json output",
200+
outputFormat: print.JSONOutputFormat,
201+
resp: &albwaf.GetCustomRuleGroupResponse{
202+
Name: "test-custom-rule-group",
203+
},
204+
wantErr: false,
205+
},
206+
}
207+
params := testparams.NewTestParams()
208+
209+
for _, tt := range tests {
210+
t.Run(tt.description, func(t *testing.T) {
211+
if err := outputResult(params.Printer, tt.outputFormat, tt.resp); (err != nil) != tt.wantErr {
212+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
213+
}
214+
})
215+
}
216+
}

internal/cmd/beta/albwaf/custom-rule-group/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7474
return fmt.Errorf("delete ALB WAF custom rule group: %w", err)
7575
}
7676

77-
params.Printer.Outputf("Custom rule group %q deleted.\n", model.Name)
77+
params.Printer.Info("Custom rule group %q deleted.\n", model.Name)
7878
return nil
7979
},
8080
}

internal/cmd/beta/albwaf/custom-rule-group/list/list.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,21 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *albwaf.APIC
122122

123123
func fetchCustomRuleGroups(ctx context.Context, model *inputModel, apiClient *albwaf.APIClient) ([]albwaf.GetCustomRuleGroupResponse, error) {
124124
var pageId string
125-
var items []albwaf.GetCustomRuleGroupResponse
125+
items := make([]albwaf.GetCustomRuleGroupResponse, 0)
126126
received := int64(0)
127127
limit := int64(math.MaxInt64)
128128
if model.Limit != nil {
129129
limit = *model.Limit
130130
}
131+
// Replace with pagination function to be introduced in STACKITSDK-525
131132
for {
132133
want := min(int64(maxPageSize), limit-received)
133134
request := buildRequest(ctx, model, apiClient, pageId, want)
134135
response, err := request.Execute()
135136
if err != nil {
136137
return nil, fmt.Errorf("list custom rule groups: %w", err)
137138
}
138-
if response.Items != nil {
139-
items = append(items, response.Items...)
140-
}
139+
items = append(items, response.Items...)
141140
pageId = ""
142141
if response.NextPageId != nil {
143142
pageId = *response.NextPageId

internal/cmd/beta/albwaf/custom-rule-group/update/update.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8888
return fmt.Errorf("update ALB WAF custom rule group: %w", err)
8989
}
9090

91-
params.Printer.Outputf("Updated custom rule group %q.\n", resp.Name)
92-
return nil
91+
return outputResult(params.Printer, model.OutputFormat, resp)
9392
},
9493
}
9594
configureFlags(cmd)
@@ -136,3 +135,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *albwaf.APIC
136135
req = req.UpdateCustomRuleGroupPayload(*model.Payload)
137136
return req
138137
}
138+
139+
func outputResult(p *print.Printer, outputFormat string, resp *albwaf.GetCustomRuleGroupResponse) error {
140+
return p.OutputResult(outputFormat, resp, func() error {
141+
if resp == nil {
142+
p.Outputf("Received empty custom rule group response")
143+
return nil
144+
}
145+
p.Outputf("Updated custom rule group %q.\n", resp.Name)
146+
return nil
147+
})
148+
}

internal/cmd/beta/albwaf/custom-rule-group/update/update_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
albwaf "github.com/stackitcloud/stackit-sdk-go/services/albwaf/v1api"
88

99
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1012
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1113

1214
"github.com/google/go-cmp/cmp"
@@ -191,3 +193,44 @@ func TestBuildRequest(t *testing.T) {
191193
})
192194
}
193195
}
196+
197+
func TestOutputResult(t *testing.T) {
198+
tests := []struct {
199+
description string
200+
outputFormat string
201+
resp *albwaf.GetCustomRuleGroupResponse
202+
wantErr bool
203+
}{
204+
{
205+
description: "empty",
206+
outputFormat: "",
207+
resp: nil,
208+
wantErr: false,
209+
},
210+
{
211+
description: "base",
212+
outputFormat: "",
213+
resp: &albwaf.GetCustomRuleGroupResponse{
214+
Name: testCustomRgName,
215+
},
216+
wantErr: false,
217+
},
218+
{
219+
description: "json output",
220+
outputFormat: print.JSONOutputFormat,
221+
resp: &albwaf.GetCustomRuleGroupResponse{
222+
Name: testCustomRgName,
223+
},
224+
wantErr: false,
225+
},
226+
}
227+
params := testparams.NewTestParams()
228+
229+
for _, tt := range tests {
230+
t.Run(tt.description, func(t *testing.T) {
231+
if err := outputResult(params.Printer, tt.outputFormat, tt.resp); (err != nil) != tt.wantErr {
232+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
233+
}
234+
})
235+
}
236+
}

0 commit comments

Comments
 (0)