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
2424const (
25- keyRingIdFlag = "key-ring"
26- keyIdFlag = "key"
25+ keyIdArg = "KEY_ID"
26+
27+ keyRingIdFlag = "key-ring-id"
2728)
2829
2930type inputModel struct {
3031 * globalflags.GlobalFlagModel
31- KeyRingId string
3232 KeyId string
33+ KeyRingId string
3334}
3435
3536func 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
124127func 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