Skip to content

Commit bba5217

Browse files
author
Jan Sternagel
committed
1. Corrected the output format for all requests
2. Switched flags to args and vice versa to keep the clean format all the way
1 parent 03406f1 commit bba5217

File tree

31 files changed

+975
-976
lines changed

31 files changed

+975
-976
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import (
2424
)
2525

2626
const (
27-
keyRingIdFlag = "key-ring"
27+
keyRingIdFlag = "key-ring-id"
2828

2929
algorithmFlag = "algorithm"
3030
descriptionFlag = "description"
3131
displayNameFlag = "name"
3232
importOnlyFlag = "import-only"
3333
purposeFlag = "purpose"
34+
protectionFlag = "protection"
3435
)
3536

3637
type inputModel struct {
@@ -42,6 +43,7 @@ type inputModel struct {
4243
Name *string
4344
ImportOnly bool // Default false
4445
Purpose *string
46+
Protection *string
4547
}
4648

4749
func NewCmd(params *params.CmdParams) *cobra.Command {
@@ -53,10 +55,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5355
Example: examples.Build(
5456
examples.NewExample(
5557
`Create a Symmetric KMS key`,
56-
`$ stackit beta kms key create --key-ring "my-keyring-id" --algorithm "rsa_2048_oaep_sha256" --name "my-key-name" --purpose "symmetric_encrypt_decrypt"`),
58+
`$ stackit beta kms key create --key-ring "my-keyring-id" --algorithm "rsa_2048_oaep_sha256" --name "my-key-name" --purpose "asymmetric_encrypt_decrypt" --protection "software"`),
5759
examples.NewExample(
5860
`Create a Message Authentication KMS key`,
59-
`$ stackit beta kms key create --key-ring "my-keyring-id" --algorithm "hmac_sha512" --name "my-key-name" --purpose "message_authentication_code"`),
61+
`$ stackit beta kms key create --key-ring "my-keyring-id" --algorithm "hmac_sha512" --name "my-key-name" --purpose "message_authentication_code" --protection "software"`),
6062
),
6163
RunE: func(cmd *cobra.Command, _ []string) error {
6264
ctx := context.Background()
@@ -87,10 +89,6 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8789

8890
// Call API
8991
req, _ := buildRequest(ctx, model, apiClient)
90-
if err != nil {
91-
return err
92-
}
93-
9492
key, err := req.Execute()
9593
if err != nil {
9694
return fmt.Errorf("create KMS key: %w", err)
@@ -128,6 +126,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
128126
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
129127
ImportOnly: flags.FlagToBoolValue(p, cmd, importOnlyFlag),
130128
Purpose: flags.FlagToStringPointer(p, cmd, purposeFlag),
129+
Protection: flags.FlagToStringPointer(p, cmd, protectionFlag),
131130
}
132131

133132
if p.IsVerbosityDebug() {
@@ -155,6 +154,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient kmsKeyClient
155154
Algorithm: kms.CreateKeyPayloadGetAlgorithmAttributeType(model.Algorithm),
156155
Purpose: kms.CreateKeyPayloadGetPurposeAttributeType(model.Purpose),
157156
ImportOnly: &model.ImportOnly,
157+
Protection: kms.CreateKeyPayloadGetProtectionAttributeType(model.Protection),
158158
})
159159
return req, nil
160160
}
@@ -171,20 +171,18 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *kms
171171
return fmt.Errorf("marshal KMS key: %w", err)
172172
}
173173
p.Outputln(string(details))
174-
return nil
175174

176175
case print.YAMLOutputFormat:
177176
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
178177
if err != nil {
179178
return fmt.Errorf("marshal KMS key: %w", err)
180179
}
181180
p.Outputln(string(details))
182-
return nil
183181

184182
default:
185-
p.Outputf("Created key for project %q. key ID: %s\n", projectLabel, utils.PtrString(resp.Id))
186-
return nil
183+
p.Outputf("Created the key '%s' for project %q. Key ID: %s\n", utils.PtrString(resp.DisplayName), projectLabel, utils.PtrString(resp.Id))
187184
}
185+
return nil
188186
}
189187

190188
func configureFlags(cmd *cobra.Command) {
@@ -194,7 +192,8 @@ func configureFlags(cmd *cobra.Command) {
194192
cmd.Flags().String(descriptionFlag, "", "Optional description of the key")
195193
cmd.Flags().Bool(importOnlyFlag, false, "States whether versions can be created or only imported")
196194
cmd.Flags().String(purposeFlag, "", "Purpose of the key. Enum: 'symmetric_encrypt_decrypt', 'asymmetric_encrypt_decrypt', 'message_authentication_code', 'asymmetric_sign_verify' ")
195+
cmd.Flags().String(protectionFlag, "", "The underlying system that is responsible for protecting the key material. Value: 'software'")
197196

198-
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag, algorithmFlag, purposeFlag, displayNameFlag)
197+
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag, algorithmFlag, purposeFlag, displayNameFlag, protectionFlag)
199198
cobra.CheckErr(err)
200199
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
testPurpose = "asymmetric_encrypt_decrypt"
2323
testDescription = "my key description"
2424
testImportOnly = "true"
25+
testProtection = "software"
2526
)
2627

2728
type testCtxKey struct{}
@@ -44,6 +45,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
4445
purposeFlag: testPurpose,
4546
descriptionFlag: testDescription,
4647
importOnlyFlag: testImportOnly,
48+
protectionFlag: testProtection,
4749
}
4850
for _, mod := range mods {
4951
mod(flagValues)
@@ -65,6 +67,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
6567
Purpose: utils.Ptr(testPurpose),
6668
Description: utils.Ptr(testDescription),
6769
ImportOnly: true, // Watch out: ImportOnly is not testImportOnly!
70+
Protection: utils.Ptr(testProtection),
6871
}
6972
for _, mod := range mods {
7073
mod(model)
@@ -81,6 +84,7 @@ func fixtureRequest(mods ...func(request *kms.ApiCreateKeyRequest)) kms.ApiCreat
8184
Purpose: kms.CreateKeyPayloadGetPurposeAttributeType(utils.Ptr(testPurpose)),
8285
Description: utils.Ptr(testDescription),
8386
ImportOnly: utils.Ptr(true),
87+
Protection: kms.CreateKeyPayloadGetProtectionAttributeType(utils.Ptr(testProtection)),
8488
})
8589

8690
for _, mod := range mods {
@@ -161,6 +165,13 @@ func TestParseInput(t *testing.T) {
161165
}),
162166
isValid: false,
163167
},
168+
{
169+
description: "protection missing (required)",
170+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
171+
delete(flagValues, protectionFlag)
172+
}),
173+
isValid: false,
174+
},
164175
{
165176
description: "name missing (required)",
166177
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
@@ -248,6 +259,7 @@ func TestBuildRequest(t *testing.T) {
248259
Purpose: kms.CreateKeyPayloadGetPurposeAttributeType(utils.Ptr(testPurpose)),
249260
Description: nil,
250261
ImportOnly: utils.Ptr(false),
262+
Protection: kms.CreateKeyPayloadGetProtectionAttributeType(utils.Ptr(testProtection)),
251263
}),
252264
},
253265
}

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

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"time"
87

98
"github.com/goccy/go-yaml"
109
"github.com/spf13/cobra"
@@ -16,36 +15,38 @@ import (
1615
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1817
kmsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/kms/utils"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1919

2020
"github.com/stackitcloud/stackit-cli/internal/pkg/services/kms/client"
2121
"github.com/stackitcloud/stackit-sdk-go/services/kms"
2222
)
2323

2424
const (
25-
keyRingIdFlag = "key-ring"
26-
keyIdFlag = "key"
25+
keyIdArg = "KEY_ID"
26+
27+
keyRingIdFlag = "key-ring-id"
2728
)
2829

2930
type inputModel struct {
3031
*globalflags.GlobalFlagModel
31-
KeyRingId string
3232
KeyId string
33+
KeyRingId string
3334
}
3435

3536
func NewCmd(params *params.CmdParams) *cobra.Command {
3637
cmd := &cobra.Command{
37-
Use: "delete",
38+
Use: fmt.Sprintf("delete %s", keyIdArg),
3839
Short: "Deletes a KMS key",
3940
Long: "Deletes a KMS key inside a specific key ring.",
40-
Args: args.NoArgs,
41+
Args: args.SingleArg(keyIdArg, utils.ValidateUUID),
4142
Example: examples.Build(
4243
examples.NewExample(
4344
`Delete a KMS key "my-key-id" inside the key ring "my-key-ring-id"`,
44-
`$ stackit beta kms keyring delete --key-ring "my-key-ring-id" --key "my-key-id"`),
45+
`$ stackit beta kms keyring delete "my-key-id" --key-ring "my-key-ring-id"`),
4546
),
46-
RunE: func(cmd *cobra.Command, _ []string) error {
47+
RunE: func(cmd *cobra.Command, args []string) error {
4748
ctx := context.Background()
48-
model, err := parseInput(params.Printer, cmd)
49+
model, err := parseInput(params.Printer, cmd, args)
4950
if err != nil {
5051
return err
5152
}
@@ -79,20 +80,22 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7980

8081
// Don't wait for a month until the deletion was performed.
8182
// Just print the deletion date.
82-
deletionDate, err := kmsUtils.GetKeyDeletionDate(ctx, apiClient, model.ProjectId, model.Region, model.KeyRingId, model.KeyId)
83+
resp, err := apiClient.GetKeyExecute(ctx, model.ProjectId, model.Region, model.KeyRingId, model.KeyId)
8384
if err != nil {
84-
return err
85+
params.Printer.Debug(print.ErrorLevel, "get key: %v", err)
8586
}
8687

87-
return outputResult(params.Printer, model.OutputFormat, model.KeyId, keyName, deletionDate)
88+
return outputResult(params.Printer, model.OutputFormat, resp)
8889
},
8990
}
9091

9192
configureFlags(cmd)
9293
return cmd
9394
}
9495

95-
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
96+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
97+
keyId := inputArgs[0]
98+
9699
globalFlags := globalflags.Parse(p, cmd)
97100
if globalFlags.ProjectId == "" {
98101
return nil, &errors.ProjectIdError{}
@@ -101,7 +104,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
101104
model := inputModel{
102105
GlobalFlagModel: globalFlags,
103106
KeyRingId: flags.FlagToStringValue(p, cmd, keyRingIdFlag),
104-
KeyId: flags.FlagToStringValue(p, cmd, keyIdFlag),
107+
KeyId: keyId,
105108
}
106109

107110
if p.IsVerbosityDebug() {
@@ -123,53 +126,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClie
123126

124127
func configureFlags(cmd *cobra.Command) {
125128
cmd.Flags().Var(flags.UUIDFlag(), keyRingIdFlag, "ID of the KMS Key Ring where the Key is stored")
126-
cmd.Flags().Var(flags.UUIDFlag(), keyIdFlag, "ID of the actual Key")
127-
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag, keyIdFlag)
129+
err := flags.MarkFlagsRequired(cmd, keyRingIdFlag)
128130
cobra.CheckErr(err)
129131
}
130132

131-
func outputResult(p *print.Printer, outputFormat, keyId, keyName string, deletionDate time.Time) error {
133+
func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
134+
if resp == nil {
135+
return fmt.Errorf("response from 'GetKeyExecute()' is nil")
136+
}
137+
132138
switch outputFormat {
133139
case print.JSONOutputFormat:
134-
details := struct {
135-
KeyId string `json:"keyId"`
136-
KeyName string `json:"keyName"`
137-
Status string `json:"status"`
138-
DeletionDate time.Time `json:"deletionDate"`
139-
}{
140-
KeyId: keyId,
141-
KeyName: keyName,
142-
Status: "Deletion Scheduled",
143-
DeletionDate: deletionDate,
144-
}
145-
b, err := json.MarshalIndent(details, "", " ")
140+
details, err := json.MarshalIndent(resp, "", " ")
146141
if err != nil {
147142
return fmt.Errorf("marshal output to JSON: %w", err)
148143
}
149-
p.Outputln(string(b))
150-
return nil
151-
144+
p.Outputln(string(details))
152145
case print.YAMLOutputFormat:
153-
details := struct {
154-
KeyId string `yaml:"keyId"`
155-
KeyName string `yaml:"keyName"`
156-
Status string `yaml:"status"`
157-
DeletionDate time.Time `yaml:"deletionDate"`
158-
}{
159-
KeyId: keyId,
160-
KeyName: keyName,
161-
Status: "Deletion Scheduled",
162-
DeletionDate: deletionDate,
163-
}
164-
b, err := yaml.Marshal(details)
146+
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
165147
if err != nil {
166148
return fmt.Errorf("marshal output to YAML: %w", err)
167149
}
168-
p.Outputln(string(b))
169-
return nil
150+
p.Outputln(string(details))
170151

171152
default:
172-
p.Outputf("Deletion of KMS key %q scheduled successfully for the deletion date: %q\n", keyName, deletionDate)
173-
return nil
153+
p.Outputf("Deletion of KMS key %s scheduled successfully for the deletion date: %s\n", utils.PtrString(resp.DisplayName), utils.PtrString(resp.DeletionDate))
174154
}
155+
return nil
175156
}

0 commit comments

Comments
 (0)