Skip to content

Commit c573f95

Browse files
feat(kms) describe key, keyring, wrappingkey (#1107)
* feat(kms) describe key, keyring, wrappingkey * fix(kms): do not reuse buffer during output tests * fix(kms) use constant Time for TestOutputResult for constant table widths
1 parent ce65339 commit c573f95

15 files changed

+1119
-0
lines changed

docs/stackit_beta_kms_key.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ stackit beta kms key [flags]
3232
* [stackit beta kms](./stackit_beta_kms.md) - Provides functionality for KMS
3333
* [stackit beta kms key create](./stackit_beta_kms_key_create.md) - Creates a KMS key
3434
* [stackit beta kms key delete](./stackit_beta_kms_key_delete.md) - Deletes a KMS key
35+
* [stackit beta kms key describe](./stackit_beta_kms_key_describe.md) - Describe a KMS key
3536
* [stackit beta kms key import](./stackit_beta_kms_key_import.md) - Import a KMS key
3637
* [stackit beta kms key list](./stackit_beta_kms_key_list.md) - List all KMS keys
3738
* [stackit beta kms key restore](./stackit_beta_kms_key_restore.md) - Restore a key
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## stackit beta kms key describe
2+
3+
Describe a KMS key
4+
5+
### Synopsis
6+
7+
Describe a KMS key
8+
9+
```
10+
stackit beta kms key describe KEY_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe a KMS key with ID xxx of keyring yyy
17+
$ stackit beta kms key describe xxx --keyring-id yyy
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta kms key describe"
24+
--keyring-id string Key Ring ID
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
33+
-p, --project-id string Project ID
34+
--region string Target region for region-specific requests
35+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
36+
```
37+
38+
### SEE ALSO
39+
40+
* [stackit beta kms key](./stackit_beta_kms_key.md) - Manage KMS keys
41+

docs/stackit_beta_kms_keyring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ stackit beta kms keyring [flags]
3232
* [stackit beta kms](./stackit_beta_kms.md) - Provides functionality for KMS
3333
* [stackit beta kms keyring create](./stackit_beta_kms_keyring_create.md) - Creates a KMS key ring
3434
* [stackit beta kms keyring delete](./stackit_beta_kms_keyring_delete.md) - Deletes a KMS key ring
35+
* [stackit beta kms keyring describe](./stackit_beta_kms_keyring_describe.md) - Describe a KMS key ring
3536
* [stackit beta kms keyring list](./stackit_beta_kms_keyring_list.md) - Lists all KMS key rings
3637

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit beta kms keyring describe
2+
3+
Describe a KMS key ring
4+
5+
### Synopsis
6+
7+
Describe a KMS key ring
8+
9+
```
10+
stackit beta kms keyring describe KEYRING_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe a KMS key ring with ID xxx
17+
$ stackit beta kms keyring describe xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta kms keyring describe"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit beta kms keyring](./stackit_beta_kms_keyring.md) - Manage KMS key rings
40+

docs/stackit_beta_kms_wrapping-key.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ stackit beta kms wrapping-key [flags]
3232
* [stackit beta kms](./stackit_beta_kms.md) - Provides functionality for KMS
3333
* [stackit beta kms wrapping-key create](./stackit_beta_kms_wrapping-key_create.md) - Creates a KMS wrapping key
3434
* [stackit beta kms wrapping-key delete](./stackit_beta_kms_wrapping-key_delete.md) - Deletes a KMS wrapping key
35+
* [stackit beta kms wrapping-key describe](./stackit_beta_kms_wrapping-key_describe.md) - Describe a KMS wrapping key
3536
* [stackit beta kms wrapping-key list](./stackit_beta_kms_wrapping-key_list.md) - Lists all KMS wrapping keys
3637

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## stackit beta kms wrapping-key describe
2+
3+
Describe a KMS wrapping key
4+
5+
### Synopsis
6+
7+
Describe a KMS wrapping key
8+
9+
```
10+
stackit beta kms wrapping-key describe WRAPPING_KEY_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe a KMS wrapping key with ID xxx of keyring yyy
17+
$ stackit beta kms wrappingkey describe xxx --keyring-id yyy
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta kms wrapping-key describe"
24+
--keyring-id string Key Ring ID
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
33+
-p, --project-id string Project ID
34+
--region string Target region for region-specific requests
35+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
36+
```
37+
38+
### SEE ALSO
39+
40+
* [stackit beta kms wrapping-key](./stackit_beta_kms_wrapping-key.md) - Manage KMS wrapping keys
41+
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package describe
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/kms/client"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
18+
"github.com/stackitcloud/stackit-sdk-go/services/kms"
19+
)
20+
21+
const (
22+
argKeyID = "KEY_ID"
23+
flagKeyRingID = "keyring-id"
24+
)
25+
26+
type inputModel struct {
27+
*globalflags.GlobalFlagModel
28+
KeyID string
29+
KeyRingID string
30+
}
31+
32+
func NewCmd(params *params.CmdParams) *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: fmt.Sprintf("describe %s", argKeyID),
35+
Short: "Describe a KMS key",
36+
Long: "Describe a KMS key",
37+
Args: args.SingleArg(argKeyID, utils.ValidateUUID),
38+
Example: examples.Build(
39+
examples.NewExample(
40+
`Describe a KMS key with ID xxx of keyring yyy`,
41+
`$ stackit beta kms key describe xxx --keyring-id yyy`,
42+
),
43+
),
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
ctx := context.Background()
46+
47+
model, err := parseInput(params.Printer, cmd, args)
48+
if err != nil {
49+
return err
50+
}
51+
52+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
53+
if err != nil {
54+
return err
55+
}
56+
57+
req := buildRequest(ctx, model, apiClient)
58+
59+
resp, err := req.Execute()
60+
if err != nil {
61+
return fmt.Errorf("get key: %w", err)
62+
}
63+
64+
return outputResult(params.Printer, model.OutputFormat, resp)
65+
},
66+
}
67+
configureFlags(cmd)
68+
return cmd
69+
}
70+
71+
func configureFlags(cmd *cobra.Command) {
72+
cmd.Flags().Var(flags.UUIDFlag(), flagKeyRingID, "Key Ring ID")
73+
err := flags.MarkFlagsRequired(cmd, flagKeyRingID)
74+
cobra.CheckErr(err)
75+
}
76+
77+
func parseInput(p *print.Printer, cmd *cobra.Command, args []string) (*inputModel, error) {
78+
globalFlags := globalflags.Parse(p, cmd)
79+
if globalFlags.ProjectId == "" {
80+
return nil, &errors.ProjectIdError{}
81+
}
82+
model := &inputModel{
83+
GlobalFlagModel: globalFlags,
84+
KeyID: args[0],
85+
KeyRingID: flags.FlagToStringValue(p, cmd, flagKeyRingID),
86+
}
87+
p.DebugInputModel(model)
88+
return model, nil
89+
}
90+
91+
func buildRequest(ctx context.Context, model *inputModel, apiClient *kms.APIClient) kms.ApiGetKeyRequest {
92+
return apiClient.GetKey(ctx, model.ProjectId, model.Region, model.KeyRingID, model.KeyID)
93+
}
94+
95+
func outputResult(p *print.Printer, outputFormat string, key *kms.Key) error {
96+
if key == nil {
97+
return fmt.Errorf("key response is empty")
98+
}
99+
return p.OutputResult(outputFormat, key, func() error {
100+
table := tables.NewTable()
101+
table.AddRow("ID", utils.PtrString(key.Id))
102+
table.AddSeparator()
103+
table.AddRow("DISPLAY NAME", utils.PtrString(key.DisplayName))
104+
table.AddSeparator()
105+
table.AddRow("CREATED AT", utils.PtrString(key.CreatedAt))
106+
table.AddSeparator()
107+
table.AddRow("STATE", utils.PtrString(key.State))
108+
table.AddSeparator()
109+
table.AddRow("DESCRIPTION", utils.PtrString(key.Description))
110+
table.AddSeparator()
111+
table.AddRow("ACCESS SCOPE", utils.PtrString(key.AccessScope))
112+
table.AddSeparator()
113+
table.AddRow("ALGORITHM", utils.PtrString(key.Algorithm))
114+
table.AddSeparator()
115+
table.AddRow("DELETION DATE", utils.PtrString(key.DeletionDate))
116+
table.AddSeparator()
117+
table.AddRow("IMPORT ONLY", utils.PtrString(key.ImportOnly))
118+
table.AddSeparator()
119+
table.AddRow("KEYRING ID", utils.PtrString(key.KeyRingId))
120+
table.AddSeparator()
121+
table.AddRow("PROTECTION", utils.PtrString(key.Protection))
122+
table.AddSeparator()
123+
table.AddRow("PURPOSE", utils.PtrString(key.Purpose))
124+
125+
err := table.Display(p)
126+
if err != nil {
127+
return fmt.Errorf("display table: %w", err)
128+
}
129+
return nil
130+
})
131+
}

0 commit comments

Comments
 (0)