Skip to content

Commit 519f724

Browse files
author
Jan Sternagel
committed
Fix feedback for async operations
1 parent e58bc38 commit 519f724

File tree

10 files changed

+89
-103
lines changed

10 files changed

+89
-103
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
20-
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
2120
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2221
"github.com/stackitcloud/stackit-sdk-go/services/kms"
2322
"github.com/stackitcloud/stackit-sdk-go/services/kms/wait"
@@ -55,10 +54,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5554
Example: examples.Build(
5655
examples.NewExample(
5756
`Create a Symmetric KMS key`,
58-
`$ stackit beta kms key create --key-ring-id "XXX" --algorithm "rsa_2048_oaep_sha256" --name "my-key-name" --purpose "asymmetric_encrypt_decrypt" --protection "software"`),
57+
`$ stackit beta kms key create --key-ring-id "my-key-ring-id" --algorithm "rsa_2048_oaep_sha256" --name "my-key-name" --purpose "asymmetric_encrypt_decrypt" --protection "software"`),
5958
examples.NewExample(
6059
`Create a Message Authentication KMS key`,
61-
`$ stackit beta kms key create --key-ring-id "XXX" --algorithm "hmac_sha512" --name "my-key-name" --purpose "message_authentication_code" --protection "software"`),
60+
`$ stackit beta kms key create --key-ring-id "my-key-ring-id" --algorithm "hmac_sha512" --name "my-key-name" --purpose "message_authentication_code" --protection "software"`),
6261
),
6362
RunE: func(cmd *cobra.Command, _ []string) error {
6463
ctx := context.Background()
@@ -73,15 +72,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7372
return err
7473
}
7574

76-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
77-
if err != nil {
78-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
79-
projectLabel = model.ProjectId
80-
}
81-
8275
if !model.AssumeYes {
83-
prompt := fmt.Sprintf("Are you sure you want to create a KMS Key for project %q?", projectLabel)
84-
err = params.Printer.PromptForConfirmation(prompt)
76+
err = params.Printer.PromptForConfirmation("Are you sure you want to create a KMS Key?")
8577
if err != nil {
8678
return err
8779
}
@@ -105,7 +97,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
10597
s.Stop()
10698
}
10799

108-
return outputResult(params.Printer, model.OutputFormat, projectLabel, resp)
100+
return outputResult(params.Printer, model, resp)
109101
},
110102
}
111103
configureFlags(cmd)
@@ -151,12 +143,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient kmsKeyClient
151143
return req, nil
152144
}
153145

154-
func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms.Key) error {
146+
func outputResult(p *print.Printer, model *inputModel, resp *kms.Key) error {
155147
if resp == nil {
156148
return fmt.Errorf("response is nil")
157149
}
158150

159-
switch outputFormat {
151+
switch model.OutputFormat {
160152
case print.JSONOutputFormat:
161153
details, err := json.MarshalIndent(resp, "", " ")
162154
if err != nil {
@@ -172,7 +164,12 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
172164
p.Outputln(string(details))
173165

174166
default:
175-
p.Outputf("Created the key '%s' for project %q. Key ID: %s\n", utils.PtrString(resp.DisplayName), projectLabel, utils.PtrString(resp.Id))
167+
operationState := "Created"
168+
if model.Async {
169+
operationState = "Triggered creation of"
170+
}
171+
p.Outputf("%s the KMS key '%s'. Key ID: %s\n", operationState, utils.PtrString(resp.DisplayName), utils.PtrString(resp.Id))
172+
176173
}
177174
return nil
178175
}

internal/cmd/beta/kms/key/create/create_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -284,42 +284,41 @@ func TestBuildRequest(t *testing.T) {
284284

285285
func TestOutputResult(t *testing.T) {
286286
tests := []struct {
287-
description string
288-
key *kms.Key
289-
outputFormat string
290-
projectLabel string
291-
wantErr bool
287+
description string
288+
model *inputModel
289+
key *kms.Key
290+
wantErr bool
292291
}{
293292
{
294293
description: "nil response",
295294
key: nil,
296295
wantErr: true,
297296
},
298297
{
299-
description: "default output",
300-
key: &kms.Key{},
301-
projectLabel: "my-project",
302-
wantErr: false,
298+
description: "default output",
299+
key: &kms.Key{},
300+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}},
301+
wantErr: false,
303302
},
304303
{
305-
description: "json output",
306-
key: &kms.Key{},
307-
outputFormat: print.JSONOutputFormat,
308-
wantErr: false,
304+
description: "json output",
305+
key: &kms.Key{},
306+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{OutputFormat: print.JSONOutputFormat}},
307+
wantErr: false,
309308
},
310309
{
311-
description: "yaml output",
312-
key: &kms.Key{},
313-
outputFormat: print.YAMLOutputFormat,
314-
wantErr: false,
310+
description: "yaml output",
311+
key: &kms.Key{},
312+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{OutputFormat: print.YAMLOutputFormat}},
313+
wantErr: false,
315314
},
316315
}
317316

318317
p := print.NewPrinter()
319318
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
320319
for _, tt := range tests {
321320
t.Run(tt.description, func(t *testing.T) {
322-
err := outputResult(p, tt.outputFormat, tt.projectLabel, tt.key)
321+
err := outputResult(p, tt.model, tt.key)
323322
if (err != nil) != tt.wantErr {
324323
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
325324
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4747
Example: examples.Build(
4848
examples.NewExample(
4949
`Import a new version for the given KMS key "my-key-id"`,
50-
`$ stackit beta kms key import "my-key-id" --key-ring "my-keyring-id" --wrapped-key "base64-encoded-wrapped-key-material" --wrapping-key-id "my-wrapping-key-id"`),
50+
`$ stackit beta kms key import "my-key-id" --key-ring-id "my-keyring-id" --wrapped-key "base64-encoded-wrapped-key-material" --wrapping-key-id "my-wrapping-key-id"`),
5151
),
5252
RunE: func(cmd *cobra.Command, args []string) error {
5353
ctx := context.Background()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6464
}
6565

6666
if !model.AssumeYes {
67-
prompt := fmt.Sprintf("Are you sure you want to rotate the key %q? (This cannot be undone)", keyName)
67+
prompt := fmt.Sprintf("Are you sure you want to rotate the key %q? (this cannot be undone)", keyName)
6868
err = params.Printer.PromptForConfirmation(prompt)
6969
if err != nil {
7070
return err

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
20-
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
2120
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2221
"github.com/stackitcloud/stackit-sdk-go/services/kms"
2322
"github.com/stackitcloud/stackit-sdk-go/services/kms/wait"
@@ -61,15 +60,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6160
return err
6261
}
6362

64-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
65-
if err != nil {
66-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
67-
projectLabel = model.ProjectId
68-
}
69-
7063
if !model.AssumeYes {
71-
prompt := fmt.Sprintf("Are you sure you want to create a KMS key ring for project %q?", projectLabel)
72-
err = params.Printer.PromptForConfirmation(prompt)
64+
err = params.Printer.PromptForConfirmation("Are you sure you want to create a KMS key ring?")
7365
if err != nil {
7466
return err
7567
}
@@ -93,15 +85,15 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
9385
// Wait for async operation, if async mode not enabled
9486
if !model.Async {
9587
s := spinner.New(params.Printer)
96-
s.Start("Creating instance")
88+
s.Start("Creating key ring")
9789
_, err = wait.CreateKeyRingWaitHandler(ctx, apiClient, model.ProjectId, model.Region, keyRingId).WaitWithContext(ctx)
9890
if err != nil {
9991
return fmt.Errorf("wait for KMS key ring creation: %w", err)
10092
}
10193
s.Stop()
10294
}
10395

104-
return outputResult(params.Printer, model.OutputFormat, projectLabel, keyRing)
96+
return outputResult(params.Printer, model, keyRing)
10597
},
10698
}
10799
configureFlags(cmd)
@@ -148,12 +140,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient kmsKeyringCl
148140
return req, nil
149141
}
150142

151-
func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms.KeyRing) error {
143+
func outputResult(p *print.Printer, model *inputModel, resp *kms.KeyRing) error {
152144
if resp == nil {
153145
return fmt.Errorf("response is nil")
154146
}
155147

156-
switch outputFormat {
148+
switch model.OutputFormat {
157149
case print.JSONOutputFormat:
158150
details, err := json.MarshalIndent(resp, "", " ")
159151
if err != nil {
@@ -169,7 +161,11 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
169161
p.Outputln(string(details))
170162

171163
default:
172-
p.Outputf("Created instance for project %q. Key ring ID: %s\n", projectLabel, utils.PtrString(resp.Id))
164+
operationState := "Created"
165+
if model.Async {
166+
operationState = "Triggered creation of"
167+
}
168+
p.Outputf("%s key ring. KMS key ring ID: %s\n", operationState, utils.PtrString(resp.Id))
173169
}
174170
return nil
175171
}

internal/cmd/beta/kms/keyring/create/create_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,42 +206,41 @@ func TestBuildRequest(t *testing.T) {
206206

207207
func TestOutputResult(t *testing.T) {
208208
tests := []struct {
209-
description string
210-
keyRing *kms.KeyRing
211-
outputFormat string
212-
projectLabel string
213-
wantErr bool
209+
model *inputModel
210+
description string
211+
keyRing *kms.KeyRing
212+
wantErr bool
214213
}{
215214
{
216215
description: "nil response",
217216
keyRing: nil,
218217
wantErr: true,
219218
},
220219
{
221-
description: "default output",
222-
keyRing: &kms.KeyRing{},
223-
projectLabel: "my-project",
224-
wantErr: false,
220+
description: "default output",
221+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}},
222+
keyRing: &kms.KeyRing{},
223+
wantErr: false,
225224
},
226225
{
227-
description: "json output",
228-
keyRing: &kms.KeyRing{},
229-
outputFormat: print.JSONOutputFormat,
230-
wantErr: false,
226+
description: "json output",
227+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{OutputFormat: print.JSONOutputFormat}},
228+
keyRing: &kms.KeyRing{},
229+
wantErr: false,
231230
},
232231
{
233-
description: "yaml output",
234-
keyRing: &kms.KeyRing{},
235-
outputFormat: print.YAMLOutputFormat,
236-
wantErr: false,
232+
description: "yaml output",
233+
model: &inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{OutputFormat: print.YAMLOutputFormat}},
234+
keyRing: &kms.KeyRing{},
235+
wantErr: false,
237236
},
238237
}
239238

240239
p := print.NewPrinter()
241240
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
242241
for _, tt := range tests {
243242
t.Run(tt.description, func(t *testing.T) {
244-
err := outputResult(p, tt.outputFormat, tt.projectLabel, tt.keyRing)
243+
err := outputResult(p, tt.model, tt.keyRing)
245244
if (err != nil) != tt.wantErr {
246245
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
247246
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
const (
22-
keyRingIdArg = "KEYRING_ID"
22+
keyRingIdArg = "KEY_RING_ID"
2323
)
2424

2525
type inputModel struct {
@@ -58,7 +58,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5858
}
5959

6060
if !model.AssumeYes {
61-
prompt := fmt.Sprintf("Are you sure you want to delete key ring %q? (This cannot be undone)", keyRingLabel)
61+
prompt := fmt.Sprintf("Are you sure you want to delete key ring %q? (this cannot be undone)", keyRingLabel)
6262
err = params.Printer.PromptForConfirmation(prompt)
6363
if err != nil {
6464
return err
@@ -72,7 +72,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7272
return fmt.Errorf("delete KMS key ring: %w", err)
7373
}
7474

75-
// Wait for async operation not relevant. Key ring deletion is synchronous.
75+
// No async wait required; key ring deletion is synchronous.
7676

7777
// Don't output anything. It's a deletion.
7878
params.Printer.Info("Deleted the key ring %q\n", keyRingLabel)

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
20-
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
2120
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2221
"github.com/stackitcloud/stackit-sdk-go/services/kms"
2322
"github.com/stackitcloud/stackit-sdk-go/services/kms/wait"
@@ -71,15 +70,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7170
return err
7271
}
7372

74-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
75-
if err != nil {
76-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
77-
projectLabel = model.ProjectId
78-
}
79-
8073
if !model.AssumeYes {
81-
prompt := fmt.Sprintf("Are you sure you want to create a KMS wrapping key for project %q?", projectLabel)
82-
err = params.Printer.PromptForConfirmation(prompt)
74+
err = params.Printer.PromptForConfirmation("Are you sure you want to create a KMS wrapping key?")
8375
if err != nil {
8476
return err
8577
}
@@ -95,15 +87,15 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
9587
// Wait for async operation, if async mode not enabled
9688
if !model.Async {
9789
s := spinner.New(params.Printer)
98-
s.Start("Creating instance")
90+
s.Start("Creating wrapping key")
9991
_, err = wait.CreateWrappingKeyWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *wrappingKey.KeyRingId, *wrappingKey.Id).WaitWithContext(ctx)
10092
if err != nil {
10193
return fmt.Errorf("wait for KMS wrapping key creation: %w", err)
10294
}
10395
s.Stop()
10496
}
10597

106-
return outputResult(params.Printer, model.OutputFormat, projectLabel, wrappingKey)
98+
return outputResult(params.Printer, model, wrappingKey)
10799
},
108100
}
109101
configureFlags(cmd)
@@ -148,12 +140,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient kmsWrappingK
148140
return req, nil
149141
}
150142

151-
func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms.WrappingKey) error {
143+
func outputResult(p *print.Printer, model *inputModel, resp *kms.WrappingKey) error {
152144
if resp == nil {
153145
return fmt.Errorf("response is nil")
154146
}
155147

156-
switch outputFormat {
148+
switch model.OutputFormat {
157149
case print.JSONOutputFormat:
158150
details, err := json.MarshalIndent(resp, "", " ")
159151
if err != nil {
@@ -169,7 +161,11 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
169161
p.Outputln(string(details))
170162

171163
default:
172-
p.Outputf("Created wrapping key for project %q. wrapping key ID: %s\n", projectLabel, utils.PtrString(resp.Id))
164+
operationState := "Created"
165+
if model.Async {
166+
operationState = "Triggered creation of"
167+
}
168+
p.Outputf("%s wrapping key. Wrapping key ID: %s\n", operationState, utils.PtrString(resp.Id))
173169
}
174170

175171
return nil

0 commit comments

Comments
 (0)