Skip to content

Commit d5b1c14

Browse files
committed
adapted volume snapshots list command to align to expectations
1 parent 6535939 commit d5b1c14

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

internal/cmd/volume/snapshot/list/list.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7272
return fmt.Errorf("list snapshots: %w", err)
7373
}
7474

75-
// Check if response is empty
76-
if resp.Items == nil || len(*resp.Items) == 0 {
77-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
78-
if err != nil {
79-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
80-
projectLabel = model.ProjectId
81-
}
82-
params.Printer.Info("No snapshots found for project %q\n", projectLabel)
83-
return nil
84-
}
75+
snapshots := resp.GetItems()
8576

86-
snapshots := *resp.Items
77+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
78+
if err != nil {
79+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
80+
projectLabel = model.ProjectId
81+
}
8782

88-
// Apply limit if specified
83+
// Truncate output
8984
if model.Limit != nil && int(*model.Limit) < len(snapshots) {
9085
snapshots = snapshots[:*model.Limit]
9186
}
9287

93-
return outputResult(params.Printer, model.OutputFormat, snapshots)
88+
return outputResult(params.Printer, model.OutputFormat, projectLabel, snapshots)
9489
},
9590
}
9691

@@ -137,12 +132,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
137132
return req
138133
}
139134

140-
func outputResult(p *print.Printer, outputFormat string, snapshots []iaas.Snapshot) error {
141-
if snapshots == nil {
142-
return fmt.Errorf("list snapshots response is empty")
143-
}
144-
135+
func outputResult(p *print.Printer, outputFormat, projectLabel string, snapshots []iaas.Snapshot) error {
145136
return p.OutputResult(outputFormat, snapshots, func() error {
137+
if len(snapshots) == 0 {
138+
p.Outputf("No snapshots found for project %q\n", projectLabel)
139+
return nil
140+
}
146141
table := tables.NewTable()
147142
table.SetHeader("ID", "NAME", "SIZE", "STATUS", "VOLUME ID", "LABELS", "CREATED AT", "UPDATED AT")
148143

internal/cmd/volume/snapshot/list/list_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func TestBuildRequest(t *testing.T) {
183183
func TestOutputResult(t *testing.T) {
184184
type args struct {
185185
outputFormat string
186+
projectLabel string
186187
snapshots []iaas.Snapshot
187188
}
188189
tests := []struct {
@@ -193,7 +194,7 @@ func TestOutputResult(t *testing.T) {
193194
{
194195
name: "empty",
195196
args: args{},
196-
wantErr: true,
197+
wantErr: false,
197198
},
198199
{
199200
name: "empty snapshot in slice",
@@ -221,7 +222,7 @@ func TestOutputResult(t *testing.T) {
221222
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
222223
for _, tt := range tests {
223224
t.Run(tt.name, func(t *testing.T) {
224-
if err := outputResult(p, tt.args.outputFormat, tt.args.snapshots); (err != nil) != tt.wantErr {
225+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.snapshots); (err != nil) != tt.wantErr {
225226
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
226227
}
227228
})

0 commit comments

Comments
 (0)