|
| 1 | +package backupschedules |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/thalassa-cloud/cli/internal/completion" |
| 10 | + "github.com/thalassa-cloud/cli/internal/table" |
| 11 | + "github.com/thalassa-cloud/cli/internal/thalassaclient" |
| 12 | + "github.com/thalassa-cloud/client-go/dbaas" |
| 13 | + tcclient "github.com/thalassa-cloud/client-go/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + backupScheduleCreateName string |
| 18 | + backupScheduleCreateDescription string |
| 19 | + backupScheduleCreateSchedule string |
| 20 | + backupScheduleCreateRetentionPolicy string |
| 21 | + backupScheduleCreateMethod string |
| 22 | + backupScheduleCreateLabels []string |
| 23 | + backupScheduleCreateAnnotations []string |
| 24 | +) |
| 25 | + |
| 26 | +// backupScheduleCreateCmd represents the backup-schedules create command |
| 27 | +var backupScheduleCreateCmd = &cobra.Command{ |
| 28 | + Use: "create", |
| 29 | + Short: "Create a database backup schedule", |
| 30 | + Long: "Create a new backup schedule for a database cluster", |
| 31 | + Args: cobra.ExactArgs(1), |
| 32 | + ValidArgsFunction: completion.CompleteDbClusterID, |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + client, err := thalassaclient.GetThalassaClient() |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("failed to create client: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + clusterIdentity := args[0] |
| 40 | + |
| 41 | + if backupScheduleCreateName == "" { |
| 42 | + return fmt.Errorf("name is required") |
| 43 | + } |
| 44 | + if backupScheduleCreateSchedule == "" { |
| 45 | + return fmt.Errorf("schedule is required") |
| 46 | + } |
| 47 | + if backupScheduleCreateRetentionPolicy == "" { |
| 48 | + return fmt.Errorf("retention-policy is required") |
| 49 | + } |
| 50 | + if backupScheduleCreateMethod == "" { |
| 51 | + return fmt.Errorf("method is required") |
| 52 | + } |
| 53 | + |
| 54 | + // Parse labels from key=value format |
| 55 | + labels := make(map[string]string) |
| 56 | + for _, label := range backupScheduleCreateLabels { |
| 57 | + parts := strings.SplitN(label, "=", 2) |
| 58 | + if len(parts) == 2 { |
| 59 | + labels[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Parse annotations from key=value format |
| 64 | + annotations := make(map[string]string) |
| 65 | + for _, annotation := range backupScheduleCreateAnnotations { |
| 66 | + parts := strings.SplitN(annotation, "=", 2) |
| 67 | + if len(parts) == 2 { |
| 68 | + annotations[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + method := dbaas.DbClusterBackupScheduleMethod(backupScheduleCreateMethod) |
| 73 | + if method != dbaas.DbClusterBackupScheduleMethodSnapshot && method != dbaas.DbClusterBackupScheduleMethodBarman { |
| 74 | + return fmt.Errorf("method must be either 'snapshot' or 'barman'") |
| 75 | + } |
| 76 | + |
| 77 | + createReq := dbaas.CreateDbBackupScheduleRequest{ |
| 78 | + Name: backupScheduleCreateName, |
| 79 | + Schedule: backupScheduleCreateSchedule, |
| 80 | + RetentionPolicy: backupScheduleCreateRetentionPolicy, |
| 81 | + Method: method, |
| 82 | + Labels: labels, |
| 83 | + Annotations: annotations, |
| 84 | + } |
| 85 | + |
| 86 | + if backupScheduleCreateDescription != "" { |
| 87 | + createReq.Description = &backupScheduleCreateDescription |
| 88 | + } |
| 89 | + |
| 90 | + schedule, err := client.DBaaS().CreateDbBackupSchedule(cmd.Context(), clusterIdentity, createReq) |
| 91 | + if err != nil { |
| 92 | + if tcclient.IsNotFound(err) { |
| 93 | + return fmt.Errorf("database cluster not found: %s", clusterIdentity) |
| 94 | + } |
| 95 | + return fmt.Errorf("failed to create backup schedule: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Output in table format |
| 99 | + clusterName := "" |
| 100 | + if schedule.DbCluster != nil { |
| 101 | + clusterName = schedule.DbCluster.Name |
| 102 | + } |
| 103 | + |
| 104 | + nextBackup := "-" |
| 105 | + if schedule.NextBackupAt != nil { |
| 106 | + nextBackup = schedule.NextBackupAt.Format("2006-01-02 15:04:05") |
| 107 | + } |
| 108 | + |
| 109 | + body := [][]string{ |
| 110 | + { |
| 111 | + schedule.Identity, |
| 112 | + schedule.Name, |
| 113 | + clusterName, |
| 114 | + string(schedule.Method), |
| 115 | + schedule.Schedule, |
| 116 | + schedule.RetentionPolicy, |
| 117 | + nextBackup, |
| 118 | + string(schedule.Status), |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + backupScheduleCreateNoHeader, _ := cmd.Flags().GetBool(NoHeaderKey) |
| 123 | + if backupScheduleCreateNoHeader { |
| 124 | + table.Print(nil, body) |
| 125 | + } else { |
| 126 | + table.Print([]string{"ID", "Name", "Cluster", "Method", "Schedule", "Retention", "Next Backup", "Status"}, body) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | + }, |
| 131 | +} |
| 132 | + |
| 133 | +func init() { |
| 134 | + BackupSchedulesCmd.AddCommand(backupScheduleCreateCmd) |
| 135 | + |
| 136 | + backupScheduleCreateCmd.Flags().Bool(NoHeaderKey, false, "Do not print the header") |
| 137 | + backupScheduleCreateCmd.Flags().StringVarP(&backupScheduleCreateName, "name", "n", "", "Name of the backup schedule (required)") |
| 138 | + backupScheduleCreateCmd.Flags().StringVar(&backupScheduleCreateDescription, "description", "", "Description of the backup schedule") |
| 139 | + backupScheduleCreateCmd.Flags().StringVar(&backupScheduleCreateSchedule, "schedule", "", "Cron expression for the backup schedule (required)") |
| 140 | + backupScheduleCreateCmd.Flags().StringVar(&backupScheduleCreateRetentionPolicy, "retention-policy", "", "Retention policy for the backup schedule (required)") |
| 141 | + backupScheduleCreateCmd.Flags().StringVar(&backupScheduleCreateMethod, "method", "barman", "Backup method: 'barman' (default)") |
| 142 | + backupScheduleCreateCmd.Flags().StringSliceVar(&backupScheduleCreateLabels, "labels", []string{}, "Labels in key=value format (can be specified multiple times)") |
| 143 | + backupScheduleCreateCmd.Flags().StringSliceVar(&backupScheduleCreateAnnotations, "annotations", []string{}, "Annotations in key=value format (can be specified multiple times)") |
| 144 | + |
| 145 | + _ = backupScheduleCreateCmd.MarkFlagRequired("name") |
| 146 | + _ = backupScheduleCreateCmd.MarkFlagRequired("schedule") |
| 147 | + _ = backupScheduleCreateCmd.MarkFlagRequired("retention-policy") |
| 148 | +} |
0 commit comments