|
| 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