|
| 1 | +package hibernate |
| 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/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" |
| 15 | + "github.com/stackitcloud/stackit-sdk-go/services/ske" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + clusterNameArg = "CLUSTER_NAME" |
| 20 | +) |
| 21 | + |
| 22 | +type inputModel struct { |
| 23 | + *globalflags.GlobalFlagModel |
| 24 | + ClusterName string |
| 25 | +} |
| 26 | + |
| 27 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: fmt.Sprintf("hibernate %s", clusterNameArg), |
| 30 | + Short: "Trigger hibernate for a SKE cluster", |
| 31 | + Long: "Trigger hibernate for a STACKIT Kubernetes Engine (SKE) cluster.", |
| 32 | + Args: args.SingleArg(clusterNameArg, nil), |
| 33 | + Example: examples.Build( |
| 34 | + examples.NewExample( |
| 35 | + `Trigger hibernate for a SKE cluster with name "my-cluster"`, |
| 36 | + "$ stackit ske cluster hibernate my-cluster"), |
| 37 | + ), |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + ctx := context.Background() |
| 40 | + model, err := parseInput(params.Printer, cmd, args) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + // Configure API client |
| 45 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + if !model.AssumeYes { |
| 51 | + prompt := fmt.Sprintf("Are you sure you want to trigger hibernate for %q in project %q?", model.ClusterName, model.ProjectId) |
| 52 | + err = params.Printer.PromptForConfirmation(prompt) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Call API |
| 59 | + req := buildRequest(ctx, model, apiClient) |
| 60 | + _, err = req.Execute() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("hibernate SKE cluster: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + params.Printer.Info("Hibernate got triggered for %q\n", model.ClusterName) |
| 66 | + return nil |
| 67 | + }, |
| 68 | + } |
| 69 | + return cmd |
| 70 | +} |
| 71 | + |
| 72 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 73 | + clusterName := inputArgs[0] |
| 74 | + |
| 75 | + globalFlags := globalflags.Parse(p, cmd) |
| 76 | + if globalFlags.ProjectId == "" { |
| 77 | + return nil, &errors.ProjectIdError{} |
| 78 | + } |
| 79 | + |
| 80 | + model := inputModel{ |
| 81 | + GlobalFlagModel: globalFlags, |
| 82 | + ClusterName: clusterName, |
| 83 | + } |
| 84 | + |
| 85 | + if p.IsVerbosityDebug() { |
| 86 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 87 | + if err != nil { |
| 88 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 89 | + } else { |
| 90 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return &model, nil |
| 95 | +} |
| 96 | + |
| 97 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClient) ske.ApiTriggerHibernateRequest { |
| 98 | + req := apiClient.TriggerHibernate(ctx, model.ProjectId, model.Region, model.ClusterName) |
| 99 | + return req |
| 100 | +} |
0 commit comments