Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/products.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions products/pgsql/internal/pgsql/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pgsql

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// newPgsqlBackup ucloud pgsql backup
func newPgsqlBackup(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "backup",
Short: "List and manipulate backups of UPgSQL instances",
Long: "List and manipulate backups of UPgSQL instances",
}
cmd.AddCommand(newBackupList(ctx))
cmd.AddCommand(newBackupDownload(ctx))
cmd.AddCommand(newBackupStrategy(ctx))
cmd.AddCommand(newBackupUpdateStrategy(ctx))
return cmd
}
51 changes: 51 additions & 0 deletions products/pgsql/internal/pgsql/backup_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pgsql

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
"github.com/ucloud/ucloud-cli/pkg/command"
)

// newBackupDownload ucloud pgsql backup download
func newBackupDownload(ctx *cli.Context) *cobra.Command {
client := newUPgSQLClient(ctx)
req := client.NewGetUPgSQLBackupURLRequest()
cmd := &cobra.Command{
Use: "download",
Short: "Display download URLs of a UPgSQL backup",
Long: "Display the public and inner download URLs of a UPgSQL backup",
Run: func(c *cobra.Command, args []string) {
*req.InstanceID = ctx.PickResourceID(*req.InstanceID)
resp, err := client.GetUPgSQLBackupURL(req)
if err != nil {
ctx.HandleError(err)
return
}
ctx.PrintList([]PgsqlBackupURLRow{{
BackupPath: resp.BackupPath,
InnerBackupPath: resp.InnerBackupPath,
}})
},
}

flags := cmd.Flags()
flags.SortFlags = false

req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance")
req.BackupID = flags.String("backup-id", "", "Required. Backup ID of the backup to download")
ctx.BindProjectID(cmd, req)
ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)

cmd.MarkFlagRequired("instance-id")
cmd.MarkFlagRequired("backup-id")
command.SetCompletion(cmd, "instance-id", func() []string {
return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone())
})
command.SetCompletion(cmd, "backup-id", func() []string {
return getBackupIDList(ctx, *req.InstanceID, req.GetProjectId(), req.GetRegion(), req.GetZone())
})

return cmd
}
80 changes: 80 additions & 0 deletions products/pgsql/internal/pgsql/backup_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package pgsql

import (
"fmt"

"github.com/spf13/cobra"

sdk "github.com/ucloud/ucloud-sdk-go/ucloud"

"github.com/ucloud/ucloud-cli/internal/common"
"github.com/ucloud/ucloud-cli/pkg/cli"
"github.com/ucloud/ucloud-cli/pkg/command"
)

var pgsqlBackupTypeMap = map[string]int{
"auto": 1,
"manual": 2,
}
var pgsqlReverseBackupTypeMap = map[int]string{
1: "auto",
2: "manual",
0: "all",
}

// newBackupList ucloud pgsql backup list
func newBackupList(ctx *cli.Context) *cobra.Command {
var bpType string
client := newUPgSQLClient(ctx)
req := client.NewListUPgSQLBackupRequest()
cmd := &cobra.Command{
Use: "list",
Short: "List backups of a UPgSQL instance",
Long: "List backups of a UPgSQL instance",
Run: func(c *cobra.Command, args []string) {
*req.InstanceID = ctx.PickResourceID(*req.InstanceID)
if v, ok := pgsqlBackupTypeMap[bpType]; ok {
req.BackupType = sdk.Int(v)
}
resp, err := client.ListUPgSQLBackup(req)
if err != nil {
ctx.HandleError(err)
return
}
list := []PgsqlBackupRow{}
for _, b := range resp.DataSet {
list = append(list, PgsqlBackupRow{
BackupID: b.BackupID,
BackupName: b.BackupName,
InstanceID: b.InstanceID,
State: b.State,
BackupType: pgsqlReverseBackupTypeMap[b.BackupType],
BackupSize: fmt.Sprintf("%dB", b.BackupSize),
BackupStartTime: common.FormatDateTime(b.BackupStartTime),
BackupEndTime: common.FormatDateTime(b.BackupEndTime),
})
}
ctx.PrintList(list)
},
}

flags := cmd.Flags()
flags.SortFlags = false

req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance")
flags.StringVar(&bpType, "backup-type", "", "Optional. Backup type. Accept values: auto, manual")
ctx.BindProjectID(cmd, req)
ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)
ctx.BindOffset(cmd, req)
ctx.BindLimit(cmd, req)

command.SetFlagValues(cmd, "backup-type", "auto", "manual")
command.SetCompletion(cmd, "instance-id", func() []string {
return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone())
})

cmd.MarkFlagRequired("instance-id")

return cmd
}
50 changes: 50 additions & 0 deletions products/pgsql/internal/pgsql/backup_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pgsql

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
"github.com/ucloud/ucloud-cli/pkg/command"
)

// newBackupStrategy ucloud pgsql backup strategy
func newBackupStrategy(ctx *cli.Context) *cobra.Command {
client := newUPgSQLClient(ctx)
req := client.NewGetUPgSQLBackupStrategyRequest()
cmd := &cobra.Command{
Use: "strategy",
Short: "Display the backup strategy of a UPgSQL instance",
Long: "Display the backup strategy of a UPgSQL instance",
Run: func(c *cobra.Command, args []string) {
*req.InstanceID = ctx.PickResourceID(*req.InstanceID)
resp, err := client.GetUPgSQLBackupStrategy(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintln(ctx.ProgressWriter(), "BackupStrategy:")
ctx.PrintList([]PgsqlBackupStrategyRow{{
BackupMethod: resp.BackupMethod,
BackupTimeRange: resp.BackupTimeRange,
BackupWeek: resp.BackupWeek,
}})
},
}

flags := cmd.Flags()
flags.SortFlags = false

req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance")
ctx.BindProjectID(cmd, req)
ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)

cmd.MarkFlagRequired("instance-id")
command.SetCompletion(cmd, "instance-id", func() []string {
return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone())
})

return cmd
}
49 changes: 49 additions & 0 deletions products/pgsql/internal/pgsql/backup_update_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pgsql

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
"github.com/ucloud/ucloud-cli/pkg/command"
)

// newBackupUpdateStrategy ucloud pgsql backup update-strategy
func newBackupUpdateStrategy(ctx *cli.Context) *cobra.Command {
client := newUPgSQLClient(ctx)
req := client.NewUpdateUPgSQLBackupStrategyRequest()
cmd := &cobra.Command{
Use: "update-strategy",
Short: "Update the backup strategy of a UPgSQL instance",
Long: "Update the backup strategy of a UPgSQL instance",
Run: func(c *cobra.Command, args []string) {
*req.InstanceID = ctx.PickResourceID(*req.InstanceID)
_, err := client.UpdateUPgSQLBackupStrategy(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "backup strategy of pgsql[%s] updated\n", *req.InstanceID)
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.InstanceID, Action: "update-strategy", Status: "Updated"})
},
}

flags := cmd.Flags()
flags.SortFlags = false

req.InstanceID = flags.String("instance-id", "", "Required. Resource ID of the UPgSQL instance")
req.BackupTimeRange = flags.String("backup-time-range", "", "Optional. Auto backup start time range, e.g. (3:00~4:00)")
req.BackupWeek = flags.String("backup-week", "", "Optional. Days of week to start auto backup, e.g. 1,2,3,4,5,6,7")
req.BackupMethod = flags.String("backup-method", "", "Optional. Default backup method")
ctx.BindProjectID(cmd, req)
ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)

cmd.MarkFlagRequired("instance-id")
command.SetCompletion(cmd, "instance-id", func() []string {
return getUPgSQLIDList(ctx, req.GetProjectId(), req.GetRegion(), req.GetZone())
})

return cmd
}
38 changes: 38 additions & 0 deletions products/pgsql/internal/pgsql/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pgsql

import (
"github.com/ucloud/ucloud-sdk-go/services/upgsql"
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
"github.com/ucloud/ucloud-sdk-go/ucloud/request"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// newUPgSQLClient returns an authed UPgSQL client whose requests are encoded as
// JSON bodies instead of form-urlencoded.
//
// The UPgSQL gateway cannot unmarshal form-urlencoded string values into Go
// int/bool request fields (e.g. ListUPgSQLParamTemplate.Count), returning
// RetCode 214001 "json: cannot unmarshal string into Go struct field ... of
// type int". The SDK default form encoder serializes *int/*bool as the strings
// "100"/"false", which trips this. Switching to NewJSONEncoder keeps numeric
// and boolean fields as JSON numbers/booleans, which the gateway accepts.
//
// The encoder is swapped per-request via an SDK request handler (runs before
// buildHTTPRequest) using the SAME config+credential the default form encoder
// would use, so signing is unchanged. This covers every typed call that goes
// through this client, including the completion helpers.
//
// Note: this works for AK/SK profiles (Signature lives in the signed JSON
// body). OAuth profiles additionally need the platform cred-header injector to
// strip Signature/PublicKey from a JSON body (it currently only strips form
// bodies) — that is a separate platform-layer change; until then OAuth+pgsql
// remains non-functional (as it is today).
func newUPgSQLClient(ctx *cli.Context) *upgsql.UPgSQLClient {
client := cli.NewServiceClient(ctx, upgsql.NewClient)
_ = client.AddRequestHandler(func(c *sdk.Client, req request.Common) (request.Common, error) {
req.SetEncoder(request.NewJSONEncoder(c.GetConfig(), c.GetCredential()))
return req, nil
})
return client
}
22 changes: 22 additions & 0 deletions products/pgsql/internal/pgsql/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pgsql

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// NewCommand builds the `pgsql` root command and mounts the `db` subtree.
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "pgsql",
Short: "Manipulate UPgSQL on UCloud platform",
Long: "Manipulate UPgSQL (UCloud PostgreSQL) on UCloud platform",
}
cmd.AddCommand(newPgsqlDB(ctx))
cmd.AddCommand(newPgsqlConf(ctx))
cmd.AddCommand(newPgsqlBackup(ctx))
cmd.AddCommand(newPgsqlLog(ctx))
cmd.AddCommand(newPgsqlSupabase(ctx))
return cmd
}
Loading
Loading