|
| 1 | +package backup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/ionos-cloud/ionosctl/v6/commands/dbaas/postgres-v2/backup/location" |
| 7 | + "github.com/ionos-cloud/ionosctl/v6/internal/client" |
| 8 | + "github.com/ionos-cloud/ionosctl/v6/internal/constants" |
| 9 | + "github.com/ionos-cloud/ionosctl/v6/internal/core" |
| 10 | + "github.com/ionos-cloud/ionosctl/v6/internal/printer/table" |
| 11 | + psqlv2 "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/psql/v3" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var backupCols = []table.Column{ |
| 16 | + {Name: "BackupId", JSONPath: "id", Default: true}, |
| 17 | + {Name: "ClusterId", JSONPath: "properties.clusterId", Default: true}, |
| 18 | + {Name: "PostgresClusterVersion", JSONPath: "properties.postgresClusterVersion", Default: true}, |
| 19 | + {Name: "Location", JSONPath: "properties.location", Default: true}, |
| 20 | + {Name: "IsActive", JSONPath: "properties.isActive", Default: true}, |
| 21 | + {Name: "EarliestRecoveryTargetTime", JSONPath: "properties.earliestRecoveryTargetTime", Default: true}, |
| 22 | + {Name: "LatestRecoveryTargetTime", JSONPath: "properties.latestRecoveryTargetTime", Default: true}, |
| 23 | + {Name: "State", JSONPath: "metadata.state"}, |
| 24 | + {Name: "CreatedDate", JSONPath: "metadata.createdDate"}, |
| 25 | +} |
| 26 | + |
| 27 | +func BackupCmd() *core.Command { |
| 28 | + backupCmd := &core.Command{ |
| 29 | + Command: &cobra.Command{ |
| 30 | + Use: "backup", |
| 31 | + Aliases: []string{"b"}, |
| 32 | + Short: "PostgreSQL Backup Operations", |
| 33 | + Long: "The sub-commands of `ionosctl dbaas postgres-v2 backup` allow you to list and get DBaaS PostgreSQL Backups.", |
| 34 | + TraverseChildren: true, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + backupCmd.Command.PersistentFlags().StringSlice(constants.ArgCols, nil, table.ColsMessage(backupCols)) |
| 39 | + _ = backupCmd.Command.RegisterFlagCompletionFunc( |
| 40 | + constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 41 | + return table.AllCols(backupCols), cobra.ShellCompDirectiveNoFileComp |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + backupCmd.AddCommand(BackupListCmd()) |
| 46 | + backupCmd.AddCommand(BackupGetCmd()) |
| 47 | + backupCmd.AddCommand(location.BackupLocationCmd()) |
| 48 | + |
| 49 | + return backupCmd |
| 50 | +} |
| 51 | + |
| 52 | +func Backups(fs ...Filter) (psqlv2.BackupReadList, error) { |
| 53 | + req := client.Must().PostgresClientV2.BackupsApi.BackupsGet(context.Background()) |
| 54 | + |
| 55 | + for _, f := range fs { |
| 56 | + var err error |
| 57 | + req, err = f(req) |
| 58 | + if err != nil { |
| 59 | + return psqlv2.BackupReadList{}, err |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + ls, _, err := req.Execute() |
| 64 | + if err != nil { |
| 65 | + return psqlv2.BackupReadList{}, err |
| 66 | + } |
| 67 | + return ls, nil |
| 68 | +} |
| 69 | + |
| 70 | +type Filter func(request psqlv2.ApiBackupsGetRequest) (psqlv2.ApiBackupsGetRequest, error) |
0 commit comments