Skip to content

Commit 7bbcd7a

Browse files
author
Jan Sternagel
committed
empty responses also handeld in json/yaml format
1 parent ada9a54 commit 7bbcd7a

File tree

8 files changed

+51
-33
lines changed

8 files changed

+51
-33
lines changed

internal/cmd/beta/kms/key/list/list.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6161
if err != nil {
6262
return fmt.Errorf("get KMS Keys: %w", err)
6363
}
64-
if resp.Keys == nil || len(*resp.Keys) == 0 {
65-
params.Printer.Info("No Keys found for project %q in region %q under the key ring %q\n", model.ProjectId, model.Region, model.KeyRingId)
66-
return nil
67-
}
68-
keys := *resp.Keys
6964

70-
return outputResult(params.Printer, model.OutputFormat, keys)
65+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyRingId, *resp.Keys)
7166
},
7267
}
7368

@@ -104,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
10499
return req
105100
}
106101

107-
func outputResult(p *print.Printer, outputFormat string, keys []kms.Key) error {
102+
func outputResult(p *print.Printer, outputFormat string, projectId, keyRingId string, keys []kms.Key) error {
108103
switch outputFormat {
109104
case print.JSONOutputFormat:
110105
details, err := json.MarshalIndent(keys, "", " ")
@@ -123,6 +118,10 @@ func outputResult(p *print.Printer, outputFormat string, keys []kms.Key) error {
123118

124119
return nil
125120
default:
121+
if len(keys) == 0 {
122+
p.Outputf("No Keys found for project %q under the key ring %q\n", projectId, keyRingId)
123+
return nil
124+
}
126125
table := tables.NewTable()
127126
table.SetHeader("ID", "NAME", "SCOPE", "ALGORITHM", "DELETION DATE", "STATUS")
128127

internal/cmd/beta/kms/key/list/list_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ func TestOutputResult(t *testing.T) {
215215
tests := []struct {
216216
description string
217217
keys []kms.Key
218+
projectId string
219+
keyRingId string
218220
outputFormat string
219221
wantErr bool
220222
}{
@@ -223,17 +225,23 @@ func TestOutputResult(t *testing.T) {
223225
{
224226
description: "default output",
225227
keys: []kms.Key{},
228+
projectId: uuid.NewString(),
229+
keyRingId: uuid.NewString(),
226230
wantErr: false,
227231
},
228232
{
229233
description: "json output",
230234
keys: []kms.Key{},
235+
projectId: uuid.NewString(),
236+
keyRingId: uuid.NewString(),
231237
outputFormat: print.JSONOutputFormat,
232238
wantErr: false,
233239
},
234240
{
235241
description: "yaml output",
236242
keys: []kms.Key{},
243+
projectId: uuid.NewString(),
244+
keyRingId: uuid.NewString(),
237245
outputFormat: print.YAMLOutputFormat,
238246
wantErr: false,
239247
},
@@ -243,7 +251,7 @@ func TestOutputResult(t *testing.T) {
243251
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
244252
for _, tt := range tests {
245253
t.Run(tt.description, func(t *testing.T) {
246-
err := outputResult(p, tt.outputFormat, tt.keys)
254+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRingId, tt.keys)
247255
if (err != nil) != tt.wantErr {
248256
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
249257
}

internal/cmd/beta/kms/keyring/list/list.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3030
Long: "Lists all KMS Keyrings.",
3131
Args: args.NoArgs,
3232
Example: examples.Build(
33-
// Enforce a specific region for the KMS
3433
examples.NewExample(
3534
`List all KMS Keyrings`,
3635
"$ stackit beta kms keyring list"),
@@ -57,13 +56,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5756
if err != nil {
5857
return fmt.Errorf("get KMS Keyrings: %w", err)
5958
}
60-
if resp.KeyRings == nil || len(*resp.KeyRings) == 0 {
61-
params.Printer.Info("No Keyrings found for project %q in region %q\n", model.ProjectId, model.Region)
62-
return nil
63-
}
64-
keyRings := *resp.KeyRings
6559

66-
return outputResult(params.Printer, model.OutputFormat, keyRings)
60+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, *resp.KeyRings)
6761
},
6862
}
6963

@@ -97,7 +91,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
9791
return req
9892
}
9993

100-
func outputResult(p *print.Printer, outputFormat string, keyRings []kms.KeyRing) error {
94+
func outputResult(p *print.Printer, outputFormat, projectId string, keyRings []kms.KeyRing) error {
10195
switch outputFormat {
10296
case print.JSONOutputFormat:
10397
details, err := json.MarshalIndent(keyRings, "", " ")
@@ -116,6 +110,11 @@ func outputResult(p *print.Printer, outputFormat string, keyRings []kms.KeyRing)
116110

117111
return nil
118112
default:
113+
if len(keyRings) == 0 {
114+
p.Outputf("No Keyrings found for project %q\n", projectId)
115+
return nil
116+
}
117+
119118
table := tables.NewTable()
120119
table.SetHeader("ID", "NAME", "STATUS")
121120

internal/cmd/beta/kms/keyring/list/list_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,29 @@ func TestBuildRequest(t *testing.T) {
173173
func TestOutputResult(t *testing.T) {
174174
tests := []struct {
175175
description string
176+
projectId string
176177
keyRings []kms.KeyRing
177178
outputFormat string
178179
projectLabel string
179180
wantErr bool
180181
}{
181182
{
182183
description: "default output",
184+
projectId: uuid.NewString(),
183185
keyRings: []kms.KeyRing{},
184186
projectLabel: "my-project",
185187
wantErr: false,
186188
},
187189
{
188190
description: "json output",
191+
projectId: uuid.NewString(),
189192
keyRings: []kms.KeyRing{},
190193
outputFormat: print.JSONOutputFormat,
191194
wantErr: false,
192195
},
193196
{
194197
description: "yaml output",
198+
projectId: uuid.NewString(),
195199
keyRings: []kms.KeyRing{},
196200
outputFormat: print.YAMLOutputFormat,
197201
wantErr: false,
@@ -202,7 +206,7 @@ func TestOutputResult(t *testing.T) {
202206
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
203207
for _, tt := range tests {
204208
t.Run(tt.description, func(t *testing.T) {
205-
err := outputResult(p, tt.outputFormat, tt.keyRings)
209+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRings)
206210
if (err != nil) != tt.wantErr {
207211
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
208212
}

internal/cmd/beta/kms/version/list/list.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6464
if err != nil {
6565
return fmt.Errorf("get Key Versions: %w", err)
6666
}
67-
if resp.Versions == nil || len(*resp.Versions) == 0 {
68-
params.Printer.Info("No Key Versions found for project %q in region %q for the key %q\n", model.ProjectId, model.Region, model.KeyId)
69-
return nil
70-
}
71-
keys := *resp.Versions
7267

73-
return outputResult(params.Printer, model.OutputFormat, keys)
68+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyId, *resp.Versions)
7469
},
7570
}
7671

@@ -106,7 +101,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
106101
return apiClient.ListVersions(ctx, model.ProjectId, model.Region, model.KeyRingId, model.KeyId)
107102
}
108103

109-
func outputResult(p *print.Printer, outputFormat string, versions []kms.Version) error {
104+
func outputResult(p *print.Printer, outputFormat, projectId, keyId string, versions []kms.Version) error {
110105
switch outputFormat {
111106
case print.JSONOutputFormat:
112107
details, err := json.MarshalIndent(versions, "", " ")
@@ -125,6 +120,10 @@ func outputResult(p *print.Printer, outputFormat string, versions []kms.Version)
125120

126121
return nil
127122
default:
123+
if len(versions) == 0 {
124+
p.Outputf("No Key Versions found for project %q for the key %q\n", projectId, keyId)
125+
return nil
126+
}
128127
table := tables.NewTable()
129128
table.SetHeader("ID", "NUMBER", "CREATED AT", "DESTROY DATE", "STATUS")
130129

internal/cmd/beta/kms/version/list/list_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,19 @@ func TestBuildRequest(t *testing.T) {
230230
func TestOutputResult(t *testing.T) {
231231
tests := []struct {
232232
description string
233+
projectId string
234+
keyId string
233235
versions []kms.Version
234236
outputFormat string
235237
projectLabel string
236238
wantErr bool
237239
}{
240+
{
241+
description: "empty default",
242+
versions: nil,
243+
projectLabel: "my-project",
244+
wantErr: false,
245+
},
238246
{
239247
description: "default output",
240248
versions: []kms.Version{},
@@ -259,7 +267,7 @@ func TestOutputResult(t *testing.T) {
259267
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
260268
for _, tt := range tests {
261269
t.Run(tt.description, func(t *testing.T) {
262-
err := outputResult(p, tt.outputFormat, tt.versions)
270+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyId, tt.versions)
263271
if (err != nil) != tt.wantErr {
264272
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
265273
}

internal/cmd/beta/kms/wrappingkey/list/list.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6262
if err != nil {
6363
return fmt.Errorf("get KMS Wrapping Keys: %w", err)
6464
}
65-
if resp.WrappingKeys == nil || len(*resp.WrappingKeys) == 0 {
66-
params.Printer.Info("No Wrapping Keys found for project %q in region %q under the key ring %q\n", model.ProjectId, model.Region, model.KeyRingId)
67-
return nil
68-
}
69-
wrappingKeys := *resp.WrappingKeys
7065

71-
return outputResult(params.Printer, model.OutputFormat, wrappingKeys)
66+
return outputResult(params.Printer, model.OutputFormat, model.ProjectId, model.KeyRingId, *resp.WrappingKeys)
7267
},
7368
}
7469
return cmd
@@ -104,7 +99,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
10499
return req
105100
}
106101

107-
func outputResult(p *print.Printer, outputFormat string, wrappingKeys []kms.WrappingKey) error {
102+
func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, wrappingKeys []kms.WrappingKey) error {
108103
switch outputFormat {
109104
case print.JSONOutputFormat:
110105
details, err := json.MarshalIndent(wrappingKeys, "", " ")
@@ -123,6 +118,10 @@ func outputResult(p *print.Printer, outputFormat string, wrappingKeys []kms.Wrap
123118

124119
return nil
125120
default:
121+
if len(wrappingKeys) == 0 {
122+
p.Outputf("No Wrapping Keys found for project %q under the key ring %q\n", projectId, keyRingId)
123+
return nil
124+
}
126125
table := tables.NewTable()
127126
table.SetHeader("ID", "NAME", "SCOPE", "ALGORITHM", "EXPIRES AT", "STATUS")
128127

internal/cmd/beta/kms/wrappingkey/list/list_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func TestBuildRequest(t *testing.T) {
220220
func TestOutputResult(t *testing.T) {
221221
tests := []struct {
222222
description string
223+
projectId string
224+
keyRingId string
223225
wrappingKeys []kms.WrappingKey
224226
outputFormat string
225227
projectLabel string
@@ -249,7 +251,7 @@ func TestOutputResult(t *testing.T) {
249251
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
250252
for _, tt := range tests {
251253
t.Run(tt.description, func(t *testing.T) {
252-
err := outputResult(p, tt.outputFormat, tt.wrappingKeys)
254+
err := outputResult(p, tt.outputFormat, tt.projectId, tt.keyRingId, tt.wrappingKeys)
253255
if (err != nil) != tt.wantErr {
254256
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
255257
}

0 commit comments

Comments
 (0)