Skip to content

Commit cc5c7e9

Browse files
committed
adapted security group rules list command to align to expectations
1 parent 434f580 commit cc5c7e9

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

internal/cmd/security-group/rule/list/list.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7474
return fmt.Errorf("list security group rules: %w", err)
7575
}
7676

77-
if resp.Items == nil || len(*resp.Items) == 0 {
78-
securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.Region, model.SecurityGroupId)
79-
if err != nil {
80-
params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err)
81-
securityGroupLabel = model.SecurityGroupId
82-
}
77+
items := resp.GetItems()
8378

84-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
85-
if err != nil {
86-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
87-
projectLabel = model.ProjectId
88-
}
89-
params.Printer.Info("No rules found in security group %q for project %q\n", securityGroupLabel, projectLabel)
90-
return nil
79+
securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.Region, model.SecurityGroupId)
80+
if err != nil {
81+
params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err)
82+
securityGroupLabel = model.SecurityGroupId
83+
}
84+
85+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
86+
if err != nil {
87+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
88+
projectLabel = model.ProjectId
9189
}
9290

9391
// Truncate output
94-
items := *resp.Items
9592
if model.Limit != nil && len(items) > int(*model.Limit) {
9693
items = items[:*model.Limit]
9794
}
9895

99-
return outputResult(params.Printer, model.OutputFormat, items)
96+
return outputResult(params.Printer, model.OutputFormat, projectLabel, securityGroupLabel, items)
10097
},
10198
}
10299
configureFlags(cmd)
@@ -139,8 +136,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
139136
return apiClient.ListSecurityGroupRules(ctx, model.ProjectId, model.Region, model.SecurityGroupId)
140137
}
141138

142-
func outputResult(p *print.Printer, outputFormat string, securityGroupRules []iaas.SecurityGroupRule) error {
139+
func outputResult(p *print.Printer, outputFormat, projectLabel, securityGroupLabel string, securityGroupRules []iaas.SecurityGroupRule) error {
143140
return p.OutputResult(outputFormat, securityGroupRules, func() error {
141+
if len(securityGroupRules) == 0 {
142+
p.Outputf("No rules found in security group %q for project %q\n", securityGroupLabel, projectLabel)
143+
return nil
144+
}
144145
table := tables.NewTable()
145146
table.SetHeader("ID", "ETHER TYPE", "DIRECTION", "PROTOCOL", "REMOTE SECURITY GROUP ID")
146147

internal/cmd/security-group/rule/list/list_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ func TestBuildRequest(t *testing.T) {
186186
func TestOutputResult(t *testing.T) {
187187
type args struct {
188188
outputFormat string
189+
projectLabel string
190+
securityGroupLabel string
189191
securityGroupRules []iaas.SecurityGroupRule
190192
}
191193
tests := []struct {
@@ -203,7 +205,7 @@ func TestOutputResult(t *testing.T) {
203205
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
204206
for _, tt := range tests {
205207
t.Run(tt.name, func(t *testing.T) {
206-
if err := outputResult(p, tt.args.outputFormat, tt.args.securityGroupRules); (err != nil) != tt.wantErr {
208+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.securityGroupLabel, tt.args.securityGroupRules); (err != nil) != tt.wantErr {
207209
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
208210
}
209211
})

0 commit comments

Comments
 (0)