|
| 1 | +package storagebox |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/hetznercloud/cli/internal/cmd/base" |
| 9 | + "github.com/hetznercloud/cli/internal/cmd/cmpl" |
| 10 | + "github.com/hetznercloud/cli/internal/cmd/util" |
| 11 | + "github.com/hetznercloud/cli/internal/hcapi2" |
| 12 | + "github.com/hetznercloud/cli/internal/state" |
| 13 | + "github.com/hetznercloud/hcloud-go/v2/hcloud" |
| 14 | +) |
| 15 | + |
| 16 | +var EnableSnapshotPlanCmd = base.Cmd{ |
| 17 | + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { |
| 18 | + cmd := &cobra.Command{ |
| 19 | + Use: "enable-snapshot-plan [options] <storage-box>", |
| 20 | + Short: "Enable automatic snapshots for a Storage Box", |
| 21 | + ValidArgsFunction: cmpl.SuggestArgs( |
| 22 | + cmpl.SuggestCandidatesF(client.StorageBox().Names), |
| 23 | + ), |
| 24 | + TraverseChildren: true, |
| 25 | + DisableFlagsInUseLine: true, |
| 26 | + } |
| 27 | + |
| 28 | + cmd.Flags().Int("max-snapshots", 0, "Maximum amount of Snapshots that should be created by this Snapshot Plan") |
| 29 | + _ = cmd.MarkFlagRequired("max-snapshots") |
| 30 | + |
| 31 | + cmd.Flags().Int("minute", 0, "Minute the Snapshot Plan should be executed on (UTC). Not specified means every minute") |
| 32 | + cmd.Flags().Int("hour", 0, "Hour the Snapshot Plan should be executed on (UTC). Not specified means every hour") |
| 33 | + cmd.Flags().String("day-of-week", "", "Day of the week the Snapshot Plan should be executed on. Not specified means every day") |
| 34 | + cmd.Flags().Int("day-of-month", 0, "Day of the month the Snapshot Plan should be executed on. Not specified means every day") |
| 35 | + |
| 36 | + _ = cmd.RegisterFlagCompletionFunc("day-of-week", cmpl.SuggestCandidates("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")) |
| 37 | + |
| 38 | + // TODO: should we add some validation here to avoid footguns? (e.g. backing up every minute) |
| 39 | + |
| 40 | + return cmd |
| 41 | + }, |
| 42 | + Run: func(s state.State, cmd *cobra.Command, args []string) error { |
| 43 | + idOrName := args[0] |
| 44 | + maxSnapshots, _ := cmd.Flags().GetInt("max-snapshots") |
| 45 | + minute, _ := cmd.Flags().GetInt("minute") |
| 46 | + hour, _ := cmd.Flags().GetInt("hour") |
| 47 | + dayOfWeek, _ := cmd.Flags().GetString("day-of-week") |
| 48 | + dayOfMonth, _ := cmd.Flags().GetInt("day-of-month") |
| 49 | + |
| 50 | + storageBox, _, err := s.Client().StorageBox().Get(s, idOrName) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if storageBox == nil { |
| 55 | + return fmt.Errorf("Storage Box not found: %s", idOrName) |
| 56 | + } |
| 57 | + |
| 58 | + opts := hcloud.StorageBoxEnableSnapshotPlanOpts{ |
| 59 | + MaxSnapshots: maxSnapshots, |
| 60 | + } |
| 61 | + |
| 62 | + if cmd.Flags().Changed("minute") { |
| 63 | + opts.Minute = &minute |
| 64 | + } |
| 65 | + if cmd.Flags().Changed("hour") { |
| 66 | + opts.Hour = &hour |
| 67 | + } |
| 68 | + if cmd.Flags().Changed("day-of-week") { |
| 69 | + weekday, err := util.WeekdayFromString(dayOfWeek) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if weekday == 0 { |
| 74 | + // The API expects 7 for Sunday |
| 75 | + weekday = 7 |
| 76 | + } |
| 77 | + opts.DayOfWeek = hcloud.Ptr(int(weekday)) |
| 78 | + } |
| 79 | + if cmd.Flags().Changed("day-of-month") { |
| 80 | + opts.DayOfMonth = &dayOfMonth |
| 81 | + } |
| 82 | + |
| 83 | + action, _, err := s.Client().StorageBox().EnableSnapshotPlan(s, storageBox, opts) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + if err := s.WaitForActions(s, cmd, action); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + cmd.Printf("Snapshot Plan enabled for Storage Box %d\n", storageBox.ID) |
| 93 | + return nil |
| 94 | + }, |
| 95 | +} |
0 commit comments