GeezerVison is showing github.com — Merge pull request #127 from jemy-wang/feat/pgsql-cli · ucloud/ucloud-cli@5e01434
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/products.gen.go +2Lines changed: 2 additions & 0 deletions Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "github.com/spf13/cobra" | | 5 | + | | 6 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 7 | +) | | 8 | + | | 9 | +// newPgsqlBackup ucloud pgsql backup | | 10 | +func newPgsqlBackup(ctx *cli.Context) *cobra.Command { | | 11 | + cmd := &cobra.Command{ | | 12 | + Use: "backup", | | 13 | + Short: "List and manipulate backups of UPgSQL instances", | | 14 | + Long: "List and manipulate backups of UPgSQL instances", | | 15 | + } | | 16 | + cmd.AddCommand(newBackupList(ctx)) | | 17 | + cmd.AddCommand(newBackupDownload(ctx)) | | 18 | + cmd.AddCommand(newBackupStrategy(ctx)) | | 19 | + cmd.AddCommand(newBackupUpdateStrategy(ctx)) | | 20 | + return cmd | | 21 | +} |
Collapse file Copy file name to clipboard +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "github.com/spf13/cobra" | | 5 | + | | 6 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 7 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 8 | +) | | 9 | + | | 10 | +// newBackupDownload ucloud pgsql backup download | | 11 | +func newBackupDownload(ctx *cli.Context) *cobra.Command { | | 12 | + client := newUPgSQLClient(ctx) | | 13 | + req := client.NewGetUPgSQLBackupURLRequest() | | 14 | + cmd := &cobra.Command{ | | 15 | + Use: "download", | | 16 | + Short: "Display download URLs of a UPgSQL backup", | | 17 | + Long: "Display the public and inner download URLs of a UPgSQL backup", | | 18 | + Run: func(c *cobra.Command, args []string) { | | 19 | + *req.InstanceID = ctx.PickResourceID(*req.InstanceID) | | 20 | + resp, err := client.GetUPgSQLBackupURL(req) | | 21 | + if err != nil { | | 22 | + ctx.HandleError(err) | | 23 | + return | | 24 | + } | | 25 | + ctx.PrintList([]PgsqlBackupURLRow{{ | | 26 | + BackupPath: resp.BackupPath, | | 27 | + InnerBackupPath: resp.InnerBackupPath, | | 28 | + }}) | | 29 | + }, | | 30 | + } | | 31 | + | | 32 | + flags := cmd.Flags() | | 33 | + flags.SortFlags = false | | 34 | + | | 35 | + req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance") | | 36 | + req.BackupID = flags.String("backup-id", "", "Required. Backup ID of the backup to download") | | 37 | + ctx.BindProjectID(cmd, req) | | 38 | + ctx.BindRegion(cmd, req) | | 39 | + ctx.BindZone(cmd, req) | | 40 | + | | 41 | + cmd.MarkFlagRequired("instance-id") | | 42 | + cmd.MarkFlagRequired("backup-id") | | 43 | + command.SetCompletion(cmd, "instance-id", func() []string { | | 44 | + return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone()) | | 45 | + }) | | 46 | + command.SetCompletion(cmd, "backup-id", func() []string { | | 47 | + return getBackupIDList(ctx, *req.InstanceID, req.GetProjectId(), req.GetRegion(), req.GetZone()) | | 48 | + }) | | 49 | + | | 50 | + return cmd | | 51 | +} |
Collapse file Copy file name to clipboard +80Lines changed: 80 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "fmt" | | 5 | + | | 6 | + "github.com/spf13/cobra" | | 7 | + | | 8 | + sdk "github.com/ucloud/ucloud-sdk-go/ucloud" | | 9 | + | | 10 | + "github.com/ucloud/ucloud-cli/internal/common" | | 11 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 12 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 13 | +) | | 14 | + | | 15 | +var pgsqlBackupTypeMap = map[string]int{ | | 16 | + "auto": 1, | | 17 | + "manual": 2, | | 18 | +} | | 19 | +var pgsqlReverseBackupTypeMap = map[int]string{ | | 20 | + 1: "auto", | | 21 | + 2: "manual", | | 22 | + 0: "all", | | 23 | +} | | 24 | + | | 25 | +// newBackupList ucloud pgsql backup list | | 26 | +func newBackupList(ctx *cli.Context) *cobra.Command { | | 27 | + var bpType string | | 28 | + client := newUPgSQLClient(ctx) | | 29 | + req := client.NewListUPgSQLBackupRequest() | | 30 | + cmd := &cobra.Command{ | | 31 | + Use: "list", | | 32 | + Short: "List backups of a UPgSQL instance", | | 33 | + Long: "List backups of a UPgSQL instance", | | 34 | + Run: func(c *cobra.Command, args []string) { | | 35 | + *req.InstanceID = ctx.PickResourceID(*req.InstanceID) | | 36 | + if v, ok := pgsqlBackupTypeMap[bpType]; ok { | | 37 | + req.BackupType = sdk.Int(v) | | 38 | + } | | 39 | + resp, err := client.ListUPgSQLBackup(req) | | 40 | + if err != nil { | | 41 | + ctx.HandleError(err) | | 42 | + return | | 43 | + } | | 44 | + list := []PgsqlBackupRow{} | | 45 | + for _, b := range resp.DataSet { | | 46 | + list = append(list, PgsqlBackupRow{ | | 47 | + BackupID: b.BackupID, | | 48 | + BackupName: b.BackupName, | | 49 | + InstanceID: b.InstanceID, | | 50 | + State: b.State, | | 51 | + BackupType: pgsqlReverseBackupTypeMap[b.BackupType], | | 52 | + BackupSize: fmt.Sprintf("%dB", b.BackupSize), | | 53 | + BackupStartTime: common.FormatDateTime(b.BackupStartTime), | | 54 | + BackupEndTime: common.FormatDateTime(b.BackupEndTime), | | 55 | + }) | | 56 | + } | | 57 | + ctx.PrintList(list) | | 58 | + }, | | 59 | + } | | 60 | + | | 61 | + flags := cmd.Flags() | | 62 | + flags.SortFlags = false | | 63 | + | | 64 | + req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance") | | 65 | + flags.StringVar(&bpType, "backup-type", "", "Optional. Backup type. Accept values: auto, manual") | | 66 | + ctx.BindProjectID(cmd, req) | | 67 | + ctx.BindRegion(cmd, req) | | 68 | + ctx.BindZone(cmd, req) | | 69 | + ctx.BindOffset(cmd, req) | | 70 | + ctx.BindLimit(cmd, req) | | 71 | + | | 72 | + command.SetFlagValues(cmd, "backup-type", "auto", "manual") | | 73 | + command.SetCompletion(cmd, "instance-id", func() []string { | | 74 | + return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone()) | | 75 | + }) | | 76 | + | | 77 | + cmd.MarkFlagRequired("instance-id") | | 78 | + | | 79 | + return cmd | | 80 | +} |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "fmt" | | 5 | + | | 6 | + "github.com/spf13/cobra" | | 7 | + | | 8 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 9 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 10 | +) | | 11 | + | | 12 | +// newBackupStrategy ucloud pgsql backup strategy | | 13 | +func newBackupStrategy(ctx *cli.Context) *cobra.Command { | | 14 | + client := newUPgSQLClient(ctx) | | 15 | + req := client.NewGetUPgSQLBackupStrategyRequest() | | 16 | + cmd := &cobra.Command{ | | 17 | + Use: "strategy", | | 18 | + Short: "Display the backup strategy of a UPgSQL instance", | | 19 | + Long: "Display the backup strategy of a UPgSQL instance", | | 20 | + Run: func(c *cobra.Command, args []string) { | | 21 | + *req.InstanceID = ctx.PickResourceID(*req.InstanceID) | | 22 | + resp, err := client.GetUPgSQLBackupStrategy(req) | | 23 | + if err != nil { | | 24 | + ctx.HandleError(err) | | 25 | + return | | 26 | + } | | 27 | + fmt.Fprintln(ctx.ProgressWriter(), "BackupStrategy:") | | 28 | + ctx.PrintList([]PgsqlBackupStrategyRow{{ | | 29 | + BackupMethod: resp.BackupMethod, | | 30 | + BackupTimeRange: resp.BackupTimeRange, | | 31 | + BackupWeek: resp.BackupWeek, | | 32 | + }}) | | 33 | + }, | | 34 | + } | | 35 | + | | 36 | + flags := cmd.Flags() | | 37 | + flags.SortFlags = false | | 38 | + | | 39 | + req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance") | | 40 | + ctx.BindProjectID(cmd, req) | | 41 | + ctx.BindRegion(cmd, req) | | 42 | + ctx.BindZone(cmd, req) | | 43 | + | | 44 | + cmd.MarkFlagRequired("instance-id") | | 45 | + command.SetCompletion(cmd, "instance-id", func() []string { | | 46 | + return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone()) | | 47 | + }) | | 48 | + | | 49 | + return cmd | | 50 | +} |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "fmt" | | 5 | + | | 6 | + "github.com/spf13/cobra" | | 7 | + | | 8 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 9 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 10 | +) | | 11 | + | | 12 | +// newBackupUpdateStrategy ucloud pgsql backup update-strategy | | 13 | +func newBackupUpdateStrategy(ctx *cli.Context) *cobra.Command { | | 14 | + client := newUPgSQLClient(ctx) | | 15 | + req := client.NewUpdateUPgSQLBackupStrategyRequest() | | 16 | + cmd := &cobra.Command{ | | 17 | + Use: "update-strategy", | | 18 | + Short: "Update the backup strategy of a UPgSQL instance", | | 19 | + Long: "Update the backup strategy of a UPgSQL instance", | | 20 | + Run: func(c *cobra.Command, args []string) { | | 21 | + *req.InstanceID = ctx.PickResourceID(*req.InstanceID) | | 22 | + _, err := client.UpdateUPgSQLBackupStrategy(req) | | 23 | + if err != nil { | | 24 | + ctx.HandleError(err) | | 25 | + return | | 26 | + } | | 27 | + fmt.Fprintf(ctx.ProgressWriter(), "backup strategy of pgsql[%s] updated\n", *req.InstanceID) | | 28 | + ctx.EmitResult(cli.OpResultRow{ResourceID: *req.InstanceID, Action: "update-strategy", Status: "Updated"}) | | 29 | + }, | | 30 | + } | | 31 | + | | 32 | + flags := cmd.Flags() | | 33 | + flags.SortFlags = false | | 34 | + | | 35 | + req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance") | | 36 | + req.BackupTimeRange = flags.String("backup-time-range", "", "Optional. Auto backup start time range, e.g. (3:00~4:00)") | | 37 | + req.BackupWeek = flags.String("backup-week", "", "Optional. Days of week to start auto backup, e.g. 1,2,3,4,5,6,7") | | 38 | + req.BackupMethod = flags.String("backup-method", "", "Optional. Default backup method") | | 39 | + ctx.BindProjectID(cmd, req) | | 40 | + ctx.BindRegion(cmd, req) | | 41 | + ctx.BindZone(cmd, req) | | 42 | + | | 43 | + cmd.MarkFlagRequired("instance-id") | | 44 | + command.SetCompletion(cmd, "instance-id", func() []string { | | 45 | + return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone()) | | 46 | + }) | | 47 | + | | 48 | + return cmd | | 49 | +} |
Collapse file Copy file name to clipboard +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "github.com/ucloud/ucloud-sdk-go/services/upgsql" | | 5 | + sdk "github.com/ucloud/ucloud-sdk-go/ucloud" | | 6 | + "github.com/ucloud/ucloud-sdk-go/ucloud/request" | | 7 | + | | 8 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 9 | +) | | 10 | + | | 11 | +// newUPgSQLClient returns an authed UPgSQL client whose requests are encoded as | | 12 | +// JSON bodies instead of form-urlencoded. | | 13 | +// | | 14 | +// The UPgSQL gateway cannot unmarshal form-urlencoded string values into Go | | 15 | +// int/bool request fields (e.g. ListUPgSQLParamTemplate.Count), returning | | 16 | +// RetCode 214001 "json: cannot unmarshal string into Go struct field ... of | | 17 | +// type int". The SDK default form encoder serializes *int/*bool as the strings | | 18 | +// "100"/"false", which trips this. Switching to NewJSONEncoder keeps numeric | | 19 | +// and boolean fields as JSON numbers/booleans, which the gateway accepts. | | 20 | +// | | 21 | +// The encoder is swapped per-request via an SDK request handler (runs before | | 22 | +// buildHTTPRequest) using the SAME config+credential the default form encoder | | 23 | +// would use, so signing is unchanged. This covers every typed call that goes | | 24 | +// through this client, including the completion helpers. | | 25 | +// | | 26 | +// Note: this works for AK/SK profiles (Signature lives in the signed JSON | | 27 | +// body). OAuth profiles additionally need the platform cred-header injector to | | 28 | +// strip Signature/PublicKey from a JSON body (it currently only strips form | | 29 | +// bodies) — that is a separate platform-layer change; until then OAuth+pgsql | | 30 | +// remains non-functional (as it is today). | | 31 | +func newUPgSQLClient(ctx *cli.Context) *upgsql.UPgSQLClient { | | 32 | + client := cli.NewServiceClient(ctx, upgsql.NewClient) | | 33 | + _ = client.AddRequestHandler(func(c *sdk.Client, req request.Common) (request.Common, error) { | | 34 | + req.SetEncoder(request.NewJSONEncoder(c.GetConfig(), c.GetCredential())) | | 35 | + return req, nil | | 36 | + }) | | 37 | + return client | | 38 | +} |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pgsql | | 2 | + | | 3 | +import ( | | 4 | + "github.com/spf13/cobra" | | 5 | + | | 6 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 7 | +) | | 8 | + | | 9 | +// NewCommand builds the `pgsql` root command and mounts the `db` subtree. | | 10 | +func NewCommand(ctx *cli.Context) *cobra.Command { | | 11 | + cmd := &cobra.Command{ | | 12 | + Use: "pgsql", | | 13 | + Short: "Manipulate UPgSQL on UCloud platform", | | 14 | + Long: "Manipulate UPgSQL (UCloud PostgreSQL) on UCloud platform", | | 15 | + } | | 16 | + cmd.AddCommand(newPgsqlDB(ctx)) | | 17 | + cmd.AddCommand(newPgsqlConf(ctx)) | | 18 | + cmd.AddCommand(newPgsqlBackup(ctx)) | | 19 | + cmd.AddCommand(newPgsqlLog(ctx)) | | 20 | + cmd.AddCommand(newPgsqlSupabase(ctx)) | | 21 | + return cmd | | 22 | +} |
|