|
1 | 1 | package describe |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "encoding/json" |
6 | 4 | "fmt" |
7 | 5 |
|
8 | | - "github.com/goccy/go-yaml" |
| 6 | + "github.com/spf13/cobra" |
9 | 7 | "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/globalflags" |
13 | 8 | "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
14 | | - "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" |
15 | | - skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" |
16 | | - "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
17 | | - |
18 | | - "github.com/spf13/cobra" |
19 | | - "github.com/stackitcloud/stackit-sdk-go/services/ske" |
20 | 9 | ) |
21 | 10 |
|
22 | 11 | const ( |
23 | 12 | clusterNameArg = "CLUSTER_NAME" |
24 | 13 | ) |
25 | 14 |
|
26 | | -type inputModel struct { |
27 | | - *globalflags.GlobalFlagModel |
28 | | - ClusterName string |
29 | | -} |
30 | | - |
31 | | -func NewCmd(p *print.Printer) *cobra.Command { |
| 15 | +func NewCmd(_ *print.Printer) *cobra.Command { |
32 | 16 | cmd := &cobra.Command{ |
33 | 17 | Use: fmt.Sprintf("describe %s", clusterNameArg), |
34 | 18 | Short: "Shows details of the credentials associated to a SKE cluster", |
35 | 19 | Long: "Shows details of the credentials associated to a STACKIT Kubernetes Engine (SKE) cluster", |
36 | | - Args: args.SingleArg(clusterNameArg, nil), |
| 20 | + Args: args.NoArgs, |
37 | 21 | Deprecated: fmt.Sprintf("%s\n%s\n%s\n%s\n", |
38 | | - "and will be removed in a future release.", |
| 22 | + "and was removed.", |
39 | 23 | "Please use the following command to obtain a kubeconfig file instead:", |
40 | 24 | " $ stackit ske kubeconfig create CLUSTER_NAME", |
41 | 25 | "For more information, visit: https://docs.stackit.cloud/stackit/en/how-to-rotate-ske-credentials-200016334.html", |
42 | 26 | ), |
43 | | - Example: examples.Build( |
44 | | - examples.NewExample( |
45 | | - `Get details of the credentials associated to the SKE cluster with name "my-cluster"`, |
46 | | - "$ stackit ske credentials describe my-cluster"), |
47 | | - examples.NewExample( |
48 | | - `Get details of the credentials associated to the SKE cluster with name "my-cluster" in JSON format`, |
49 | | - "$ stackit ske credentials describe my-cluster --output-format json"), |
50 | | - ), |
51 | | - RunE: func(cmd *cobra.Command, args []string) error { |
52 | | - ctx := context.Background() |
53 | | - model, err := parseInput(p, cmd, args) |
54 | | - if err != nil { |
55 | | - return err |
56 | | - } |
57 | | - |
58 | | - // Configure API client |
59 | | - apiClient, err := client.ConfigureClient(p) |
60 | | - if err != nil { |
61 | | - return err |
62 | | - } |
63 | 27 |
|
64 | | - // Check if SKE is enabled for this project |
65 | | - enabled, err := skeUtils.ProjectEnabled(ctx, apiClient, model.ProjectId) |
66 | | - if err != nil { |
67 | | - return err |
68 | | - } |
69 | | - if !enabled { |
70 | | - return fmt.Errorf("SKE isn't enabled for this project, please run 'stackit ske enable'") |
71 | | - } |
72 | | - |
73 | | - // Call API |
74 | | - req := buildRequest(ctx, model, apiClient) |
75 | | - resp, err := req.Execute() |
76 | | - if err != nil { |
77 | | - return fmt.Errorf("get SKE credentials: %w", err) |
78 | | - } |
79 | | - |
80 | | - return outputResult(p, model.OutputFormat, resp) |
| 28 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 29 | + return nil |
81 | 30 | }, |
82 | 31 | } |
83 | 32 | return cmd |
84 | 33 | } |
85 | | - |
86 | | -func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
87 | | - clusterName := inputArgs[0] |
88 | | - |
89 | | - globalFlags := globalflags.Parse(p, cmd) |
90 | | - if globalFlags.ProjectId == "" { |
91 | | - return nil, &errors.ProjectIdError{} |
92 | | - } |
93 | | - |
94 | | - model := inputModel{ |
95 | | - GlobalFlagModel: globalFlags, |
96 | | - ClusterName: clusterName, |
97 | | - } |
98 | | - |
99 | | - if p.IsVerbosityDebug() { |
100 | | - modelStr, err := print.BuildDebugStrFromInputModel(model) |
101 | | - if err != nil { |
102 | | - p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
103 | | - } else { |
104 | | - p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - return &model, nil |
109 | | -} |
110 | | - |
111 | | -func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClient) ske.ApiGetCredentialsRequest { |
112 | | - req := apiClient.GetCredentials(ctx, model.ProjectId, model.ClusterName) //nolint:staticcheck //command will be removed in a later update |
113 | | - return req |
114 | | -} |
115 | | - |
116 | | -func outputResult(p *print.Printer, outputFormat string, credentials *ske.Credentials) error { |
117 | | - switch outputFormat { |
118 | | - case print.JSONOutputFormat: |
119 | | - details, err := json.MarshalIndent(credentials, "", " ") |
120 | | - if err != nil { |
121 | | - return fmt.Errorf("marshal SKE credentials: %w", err) |
122 | | - } |
123 | | - p.Outputln(string(details)) |
124 | | - |
125 | | - return nil |
126 | | - case print.YAMLOutputFormat: |
127 | | - details, err := yaml.MarshalWithOptions(credentials, yaml.IndentSequence(true)) |
128 | | - if err != nil { |
129 | | - return fmt.Errorf("marshal SKE credentials: %w", err) |
130 | | - } |
131 | | - p.Outputln(string(details)) |
132 | | - |
133 | | - return nil |
134 | | - default: |
135 | | - table := tables.NewTable() |
136 | | - table.AddRow("SERVER", *credentials.Server) |
137 | | - table.AddSeparator() |
138 | | - table.AddRow("TOKEN", *credentials.Token) |
139 | | - err := table.Display(p) |
140 | | - if err != nil { |
141 | | - return fmt.Errorf("render table: %w", err) |
142 | | - } |
143 | | - |
144 | | - return nil |
145 | | - } |
146 | | -} |
0 commit comments