Skip to content

Commit 777630a

Browse files
committed
adapted security group rules list command to align to expectations
1 parent b663d3d commit 777630a

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
@@ -184,6 +184,8 @@ func TestBuildRequest(t *testing.T) {
184184
func TestOutputResult(t *testing.T) {
185185
type args struct {
186186
outputFormat string
187+
projectLabel string
188+
securityGroupLabel string
187189
securityGroupRules []iaas.SecurityGroupRule
188190
}
189191
tests := []struct {
@@ -200,7 +202,7 @@ func TestOutputResult(t *testing.T) {
200202
params := testparams.NewTestParams()
201203
for _, tt := range tests {
202204
t.Run(tt.name, func(t *testing.T) {
203-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.securityGroupRules); (err != nil) != tt.wantErr {
205+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.securityGroupLabel, tt.args.securityGroupRules); (err != nil) != tt.wantErr {
204206
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
205207
}
206208
})

0 commit comments

Comments
 (0)