|
| 1 | +package create |
| 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/projectname" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + |
| 20 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 21 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + volumeIdFlag = "volume-id" |
| 26 | + nameFlag = "name" |
| 27 | + labelsFlag = "labels" |
| 28 | +) |
| 29 | + |
| 30 | +type inputModel struct { |
| 31 | + *globalflags.GlobalFlagModel |
| 32 | + VolumeID string |
| 33 | + Name *string |
| 34 | + Labels map[string]string |
| 35 | +} |
| 36 | + |
| 37 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: "create", |
| 40 | + Short: "Creates a snapshot from a volume", |
| 41 | + Long: "Creates a snapshot from a volume.", |
| 42 | + Args: args.NoArgs, |
| 43 | + Example: examples.Build( |
| 44 | + examples.NewExample( |
| 45 | + `Create a snapshot from a volume`, |
| 46 | + "$ stackit volume snapshot create --volume-id xxx --project-id xxx"), |
| 47 | + examples.NewExample( |
| 48 | + `Create a snapshot with a name`, |
| 49 | + "$ stackit volume snapshot create --volume-id xxx --name my-snapshot --project-id xxx"), |
| 50 | + examples.NewExample( |
| 51 | + `Create a snapshot with labels`, |
| 52 | + "$ stackit volume snapshot create --volume-id xxx --labels key1=value1,key2=value2 --project-id xxx"), |
| 53 | + ), |
| 54 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 55 | + ctx := context.Background() |
| 56 | + model, err := parseInput(params.Printer, cmd) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + // Configure API client |
| 62 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 68 | + if err != nil { |
| 69 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 70 | + projectLabel = model.ProjectId |
| 71 | + } |
| 72 | + |
| 73 | + // Get volume name for label |
| 74 | + volumeLabel := model.VolumeID |
| 75 | + volume, err := apiClient.GetVolume(ctx, model.ProjectId, model.VolumeID).Execute() |
| 76 | + if err != nil { |
| 77 | + params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err) |
| 78 | + } else if volume != nil && volume.Name != nil { |
| 79 | + volumeLabel = *volume.Name |
| 80 | + } |
| 81 | + |
| 82 | + if !model.AssumeYes { |
| 83 | + prompt := fmt.Sprintf("Are you sure you want to create snapshot from volume %q? (This cannot be undone)", volumeLabel) |
| 84 | + err = params.Printer.PromptForConfirmation(prompt) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Call API |
| 91 | + req := buildRequest(ctx, model, apiClient) |
| 92 | + resp, err := req.Execute() |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("create snapshot: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Wait for async operation, if async mode not enabled |
| 98 | + if !model.Async { |
| 99 | + s := spinner.New(params.Printer) |
| 100 | + s.Start("Creating snapshot") |
| 101 | + resp, err = wait.CreateSnapshotWaitHandler(ctx, apiClient, model.ProjectId, *resp.Id).WaitWithContext(ctx) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("wait for snapshot creation: %w", err) |
| 104 | + } |
| 105 | + s.Stop() |
| 106 | + } |
| 107 | + |
| 108 | + if model.Async { |
| 109 | + params.Printer.Info("Triggered snapshot of %q in %q. Snapshot ID: %s\n", volumeLabel, projectLabel, *resp.Id) |
| 110 | + } else { |
| 111 | + params.Printer.Info("Created snapshot of %q in %q. Snapshot ID: %s\n", volumeLabel, projectLabel, *resp.Id) |
| 112 | + } |
| 113 | + return nil |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + configureFlags(cmd) |
| 118 | + return cmd |
| 119 | +} |
| 120 | + |
| 121 | +func configureFlags(cmd *cobra.Command) { |
| 122 | + cmd.Flags().String(volumeIdFlag, "", "ID of the volume from which a snapshot should be created") |
| 123 | + cmd.Flags().String(nameFlag, "", "Name of the snapshot") |
| 124 | + cmd.Flags().StringToString(labelsFlag, nil, "Key-value string pairs as labels") |
| 125 | + |
| 126 | + err := flags.MarkFlagsRequired(cmd, volumeIdFlag) |
| 127 | + cobra.CheckErr(err) |
| 128 | +} |
| 129 | + |
| 130 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 131 | + globalFlags := globalflags.Parse(p, cmd) |
| 132 | + if globalFlags.ProjectId == "" { |
| 133 | + return nil, &errors.ProjectIdError{} |
| 134 | + } |
| 135 | + |
| 136 | + volumeID := flags.FlagToStringValue(p, cmd, volumeIdFlag) |
| 137 | + if volumeID == "" { |
| 138 | + return nil, fmt.Errorf("volume-id is required") |
| 139 | + } |
| 140 | + if err := utils.ValidateUUID(volumeID); err != nil { |
| 141 | + return nil, fmt.Errorf("volume-id must be a valid UUID") |
| 142 | + } |
| 143 | + |
| 144 | + name := flags.FlagToStringPointer(p, cmd, nameFlag) |
| 145 | + labels := flags.FlagToStringToStringPointer(p, cmd, labelsFlag) |
| 146 | + if labels == nil { |
| 147 | + labels = &map[string]string{} |
| 148 | + } |
| 149 | + |
| 150 | + model := inputModel{ |
| 151 | + GlobalFlagModel: globalFlags, |
| 152 | + VolumeID: volumeID, |
| 153 | + Name: name, |
| 154 | + Labels: *labels, |
| 155 | + } |
| 156 | + |
| 157 | + if p.IsVerbosityDebug() { |
| 158 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 159 | + if err != nil { |
| 160 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 161 | + } else { |
| 162 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return &model, nil |
| 167 | +} |
| 168 | + |
| 169 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateSnapshotRequest { |
| 170 | + req := apiClient.CreateSnapshot(ctx, model.ProjectId) |
| 171 | + payload := iaas.NewCreateSnapshotPayloadWithDefaults() |
| 172 | + payload.VolumeId = &model.VolumeID |
| 173 | + payload.Name = model.Name |
| 174 | + |
| 175 | + // Convert labels to map[string]interface{} |
| 176 | + if len(model.Labels) > 0 { |
| 177 | + labelsMap := map[string]interface{}{} |
| 178 | + for k, v := range model.Labels { |
| 179 | + labelsMap[k] = v |
| 180 | + } |
| 181 | + payload.Labels = &labelsMap |
| 182 | + } |
| 183 | + |
| 184 | + req = req.CreateSnapshotPayload(*payload) |
| 185 | + return req |
| 186 | +} |
0 commit comments