Skip to content

Commit d457735

Browse files
chore(postgresflex): fix invalid json output, adjust tests
Relates to STACKITCLI-374
1 parent 2306512 commit d457735

8 files changed

Lines changed: 80 additions & 53 deletions

File tree

internal/cmd/postgresflex/backup/describe/describe_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,40 @@ func Test_outputResult(t *testing.T) {
250250
args args
251251
wantErr bool
252252
}{
253-
{"empty", args{}, true},
254-
{"standard", args{outputFormat: "", backup: &postgresflex.Backup{StartTime: utils.Ptr(time.Now().Format(time.RFC3339))}}, false},
255-
{"complete", args{outputFormat: "", backup: &postgresflex.Backup{
256-
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
257-
Id: utils.Ptr("id"),
258-
Labels: []string{"foo", "bar", "baz"},
259-
Name: utils.Ptr("name"),
260-
Options: &map[string]string{"test1": "test1", "test2": "test2"},
261-
Size: utils.Ptr(int64(42)),
262-
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
263-
}}, false},
253+
{
254+
name: "empty",
255+
args: args{},
256+
wantErr: true,
257+
},
258+
{
259+
name: "standard",
260+
args: args{
261+
outputFormat: "",
262+
backup: &postgresflex.Backup{
263+
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
264+
},
265+
},
266+
wantErr: false,
267+
},
268+
{
269+
name: "complete",
270+
args: args{
271+
outputFormat: "",
272+
backup: &postgresflex.Backup{
273+
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
274+
Id: utils.Ptr("id"),
275+
Labels: []string{"foo",
276+
"bar",
277+
"baz",
278+
},
279+
Name: utils.Ptr("name"),
280+
Options: &map[string]string{"test1": "test1", "test2": "test2"},
281+
Size: utils.Ptr(int64(42)),
282+
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
283+
},
284+
},
285+
wantErr: false,
286+
},
264287
}
265288
params := testparams.NewTestParams()
266289
for _, tt := range tests {

internal/cmd/postgresflex/backup/list/list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,14 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8080
if err != nil {
8181
return fmt.Errorf("get backups for PostgreSQL Flex instance %q: %w", instanceLabel, err)
8282
}
83-
if len(resp.Items) == 0 {
84-
params.Printer.Outputf("No backups found for instance %q", instanceLabel)
85-
return nil
86-
}
8783
backups := resp.Items
8884

8985
// Truncate output
9086
if model.Limit != nil && len(backups) > int(*model.Limit) {
9187
backups = backups[:*model.Limit]
9288
}
9389

94-
return outputResult(params.Printer, model.OutputFormat, backups)
90+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, backups)
9591
},
9692
}
9793

@@ -133,8 +129,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle
133129
return req
134130
}
135131

136-
func outputResult(p *print.Printer, outputFormat string, backups []postgresflex.Backup) error {
132+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, backups []postgresflex.Backup) error {
137133
return p.OutputResult(outputFormat, backups, func() error {
134+
if len(backups) == 0 {
135+
p.Outputf("No backups found for instance %q", instanceLabel)
136+
return nil
137+
}
138138
table := tables.NewTable()
139139
table.SetHeader("ID", "CREATED AT", "EXPIRES AT", "BACKUP SIZE")
140140
for i := range backups {

internal/cmd/postgresflex/backup/list/list_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
9+
910
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1011

1112
"github.com/google/go-cmp/cmp"
@@ -176,17 +177,18 @@ func TestBuildRequest(t *testing.T) {
176177

177178
func Test_outputResult(t *testing.T) {
178179
type args struct {
179-
outputFormat string
180-
backups []postgresflex.Backup
180+
outputFormat string
181+
instanceLabel string
182+
backups []postgresflex.Backup
181183
}
182184
tests := []struct {
183185
name string
184186
args args
185187
wantErr bool
186188
}{
187189
{"empty", args{}, false},
188-
{"standard", args{outputFormat: "", backups: []postgresflex.Backup{}}, false},
189-
{"complete", args{outputFormat: "", backups: []postgresflex.Backup{
190+
{"standard", args{outputFormat: "", instanceLabel: "label", backups: []postgresflex.Backup{}}, false},
191+
{"complete", args{outputFormat: "", instanceLabel: "label", backups: []postgresflex.Backup{
190192
{
191193
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
192194
Id: utils.Ptr("id"),
@@ -211,7 +213,7 @@ func Test_outputResult(t *testing.T) {
211213
params := testparams.NewTestParams()
212214
for _, tt := range tests {
213215
t.Run(tt.name, func(t *testing.T) {
214-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.backups); (err != nil) != tt.wantErr {
216+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instanceLabel, tt.args.backups); (err != nil) != tt.wantErr {
215217
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
216218
}
217219
})

internal/cmd/postgresflex/instance/list/list.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6868
if err != nil {
6969
return fmt.Errorf("get PostgreSQL Flex instances: %w", err)
7070
}
71-
if len(resp.Items) == 0 {
72-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
73-
if err != nil {
74-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
75-
projectLabel = model.ProjectId
76-
}
77-
params.Printer.Info("No instances found for project %q\n", projectLabel)
78-
return nil
79-
}
71+
8072
instances := resp.Items
8173

8274
// Truncate output
8375
if model.Limit != nil && len(instances) > int(*model.Limit) {
8476
instances = instances[:*model.Limit]
8577
}
8678

87-
return outputResult(params.Printer, model.OutputFormat, instances)
79+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
80+
if err != nil {
81+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
82+
projectLabel = model.ProjectId
83+
}
84+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
8885
},
8986
}
9087

@@ -124,8 +121,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle
124121
return req
125122
}
126123

127-
func outputResult(p *print.Printer, outputFormat string, instances []postgresflex.InstanceListInstance) error {
124+
func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []postgresflex.InstanceListInstance) error {
128125
return p.OutputResult(outputFormat, instances, func() error {
126+
if len(instances) == 0 {
127+
p.Outputf("No instances found for project %q\n", projectLabel)
128+
return nil
129+
}
129130
caser := cases.Title(language.English)
130131
table := tables.NewTable()
131132
table.SetHeader("ID", "NAME", "STATUS")

internal/cmd/postgresflex/instance/list/list_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func TestBuildRequest(t *testing.T) {
151151
func Test_outputResult(t *testing.T) {
152152
type args struct {
153153
outputFormat string
154+
projectLabel string
154155
instances []postgresflex.InstanceListInstance
155156
}
156157
tests := []struct {
@@ -159,8 +160,8 @@ func Test_outputResult(t *testing.T) {
159160
wantErr bool
160161
}{
161162
{"empty", args{}, false},
162-
{"standard", args{"", []postgresflex.InstanceListInstance{}}, false},
163-
{"complete", args{"", []postgresflex.InstanceListInstance{
163+
{"standard", args{"", "label", []postgresflex.InstanceListInstance{}}, false},
164+
{"complete", args{"", "label", []postgresflex.InstanceListInstance{
164165
{
165166
Id: new(string),
166167
Name: new(string),
@@ -181,7 +182,7 @@ func Test_outputResult(t *testing.T) {
181182
params := testparams.NewTestParams()
182183
for _, tt := range tests {
183184
t.Run(tt.name, func(t *testing.T) {
184-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
185+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr {
185186
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
186187
}
187188
})

internal/cmd/postgresflex/options/options.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ func buildStoragesTable(storagesResp postgresflex.ListStoragesResponse) tables.T
230230
table := tables.NewTable()
231231
table.SetTitle("Storages")
232232
table.SetHeader("MINIMUM", "MAXIMUM", "STORAGE CLASS")
233-
for i := range storagesResp.StorageClasses {
234-
sc := storagesResp.StorageClasses[i]
233+
for _, sc := range storagesResp.StorageClasses {
235234
table.AddRow(
236235
utils.PtrString(storagesResp.StorageRange.Min),
237236
utils.PtrString(storagesResp.StorageRange.Max),

internal/cmd/postgresflex/user/list/list.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,19 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6969
if err != nil {
7070
return fmt.Errorf("get PostgreSQL Flex users: %w", err)
7171
}
72-
if len(resp.Items) == 0 {
73-
instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
74-
if err != nil {
75-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
76-
instanceLabel = model.InstanceId
77-
}
78-
params.Printer.Info("No users found for instance %q\n", instanceLabel)
79-
return nil
80-
}
8172
users := resp.Items
8273

8374
// Truncate output
8475
if model.Limit != nil && len(users) > int(*model.Limit) {
8576
users = users[:*model.Limit]
8677
}
8778

88-
return outputResult(params.Printer, model.OutputFormat, users)
79+
instanceLabel, err := postgresflexUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
80+
if err != nil {
81+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
82+
instanceLabel = model.InstanceId
83+
}
84+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, users)
8985
},
9086
}
9187

@@ -130,8 +126,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle
130126
return req
131127
}
132128

133-
func outputResult(p *print.Printer, outputFormat string, users []postgresflex.ListUsersResponseItem) error {
129+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, users []postgresflex.ListUsersResponseItem) error {
134130
return p.OutputResult(outputFormat, users, func() error {
131+
if len(users) == 0 {
132+
p.Outputf("No users found for instance %q\n", instanceLabel)
133+
return nil
134+
}
135135
table := tables.NewTable()
136136
table.SetHeader("ID", "USERNAME")
137137
for i := range users {

internal/cmd/postgresflex/user/list/list_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ func TestBuildRequest(t *testing.T) {
167167

168168
func Test_outputResult(t *testing.T) {
169169
type args struct {
170-
outputFormat string
171-
users []postgresflex.ListUsersResponseItem
170+
outputFormat string
171+
instanceLabel string
172+
users []postgresflex.ListUsersResponseItem
172173
}
173174
tests := []struct {
174175
name string
@@ -177,15 +178,15 @@ func Test_outputResult(t *testing.T) {
177178
}{
178179
{"empty", args{}, false},
179180
{"standard", args{users: []postgresflex.ListUsersResponseItem{{}}}, false},
180-
{"complete", args{users: []postgresflex.ListUsersResponseItem{{
181+
{"complete", args{instanceLabel: "label", users: []postgresflex.ListUsersResponseItem{{
181182
Id: new(string),
182183
Username: new(string),
183184
}}}, false},
184185
}
185186
params := testparams.NewTestParams()
186187
for _, tt := range tests {
187188
t.Run(tt.name, func(t *testing.T) {
188-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.users); (err != nil) != tt.wantErr {
189+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instanceLabel, tt.args.users); (err != nil) != tt.wantErr {
189190
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
190191
}
191192
})

0 commit comments

Comments
 (0)