|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + |
| 20 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + backupIdArg = "BACKUP_ID" |
| 25 | + nameFlag = "name" |
| 26 | + labelsFlag = "labels" |
| 27 | +) |
| 28 | + |
| 29 | +type inputModel struct { |
| 30 | + *globalflags.GlobalFlagModel |
| 31 | + BackupId string |
| 32 | + Name *string |
| 33 | + Labels map[string]string |
| 34 | +} |
| 35 | + |
| 36 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 37 | + cmd := &cobra.Command{ |
| 38 | + Use: fmt.Sprintf("update %s", backupIdArg), |
| 39 | + Short: "Updates a backup", |
| 40 | + Long: "Updates a backup by its ID.", |
| 41 | + Args: args.SingleArg(backupIdArg, utils.ValidateUUID), |
| 42 | + Example: examples.Build( |
| 43 | + examples.NewExample( |
| 44 | + `Update a backup name`, |
| 45 | + "$ stackit volume backup update xxx-xxx-xxx --name new-name"), |
| 46 | + examples.NewExample( |
| 47 | + `Update backup labels`, |
| 48 | + "$ stackit volume backup update xxx-xxx-xxx --labels key1=value1,key2=value2"), |
| 49 | + ), |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + ctx := context.Background() |
| 52 | + model, err := parseInput(params.Printer, cmd, args) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + // Configure API client |
| 58 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Call API |
| 64 | + req := buildRequest(ctx, model, apiClient) |
| 65 | + resp, err := req.Execute() |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("update backup: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Get backup label (use ID if name not available) |
| 71 | + backupLabel := model.BackupId |
| 72 | + if resp.Name != nil { |
| 73 | + backupLabel = *resp.Name |
| 74 | + } |
| 75 | + |
| 76 | + return outputResult(params.Printer, model.OutputFormat, backupLabel, resp) |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + configureFlags(cmd) |
| 81 | + return cmd |
| 82 | +} |
| 83 | + |
| 84 | +func configureFlags(cmd *cobra.Command) { |
| 85 | + cmd.Flags().String(nameFlag, "", "Name of the backup") |
| 86 | + cmd.Flags().StringToString(labelsFlag, nil, "Key-value string pairs as labels") |
| 87 | +} |
| 88 | + |
| 89 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 90 | + backupId := inputArgs[0] |
| 91 | + |
| 92 | + globalFlags := globalflags.Parse(p, cmd) |
| 93 | + if globalFlags.ProjectId == "" { |
| 94 | + return nil, &errors.ProjectIdError{} |
| 95 | + } |
| 96 | + |
| 97 | + name := flags.FlagToStringPointer(p, cmd, nameFlag) |
| 98 | + labels := flags.FlagToStringToStringPointer(p, cmd, labelsFlag) |
| 99 | + if labels == nil { |
| 100 | + labels = &map[string]string{} |
| 101 | + } |
| 102 | + |
| 103 | + model := inputModel{ |
| 104 | + GlobalFlagModel: globalFlags, |
| 105 | + BackupId: backupId, |
| 106 | + Name: name, |
| 107 | + Labels: *labels, |
| 108 | + } |
| 109 | + |
| 110 | + if p.IsVerbosityDebug() { |
| 111 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 112 | + if err != nil { |
| 113 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 114 | + } else { |
| 115 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return &model, nil |
| 120 | +} |
| 121 | + |
| 122 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateBackupRequest { |
| 123 | + req := apiClient.UpdateBackup(ctx, model.ProjectId, model.BackupId) |
| 124 | + |
| 125 | + updatePayload := iaas.NewUpdateBackupPayloadWithDefaults() |
| 126 | + if model.Name != nil { |
| 127 | + updatePayload.Name = model.Name |
| 128 | + } |
| 129 | + |
| 130 | + // Convert map[string]string to map[string]interface{} |
| 131 | + var labelsMap *map[string]interface{} |
| 132 | + if len(model.Labels) > 0 { |
| 133 | + labelsMap = utils.Ptr(map[string]interface{}{}) |
| 134 | + for k, v := range model.Labels { |
| 135 | + (*labelsMap)[k] = v |
| 136 | + } |
| 137 | + } |
| 138 | + updatePayload.Labels = labelsMap |
| 139 | + |
| 140 | + req = req.UpdateBackupPayload(*updatePayload) |
| 141 | + return req |
| 142 | +} |
| 143 | + |
| 144 | +func outputResult(p *print.Printer, outputFormat, backupLabel string, backup *iaas.Backup) error { |
| 145 | + if backup == nil { |
| 146 | + return fmt.Errorf("backup response is empty") |
| 147 | + } |
| 148 | + |
| 149 | + switch outputFormat { |
| 150 | + case print.JSONOutputFormat: |
| 151 | + details, err := json.MarshalIndent(backup, "", " ") |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("marshal backup: %w", err) |
| 154 | + } |
| 155 | + p.Outputln(string(details)) |
| 156 | + return nil |
| 157 | + |
| 158 | + case print.YAMLOutputFormat: |
| 159 | + details, err := yaml.MarshalWithOptions(backup, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 160 | + if err != nil { |
| 161 | + return fmt.Errorf("marshal backup: %w", err) |
| 162 | + } |
| 163 | + p.Outputln(string(details)) |
| 164 | + return nil |
| 165 | + |
| 166 | + default: |
| 167 | + p.Outputf("Updated backup %q\n", backupLabel) |
| 168 | + return nil |
| 169 | + } |
| 170 | +} |
0 commit comments