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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ require (
github.com/sirupsen/logrus v1.3.0
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/ucloud/ucloud-sdk-go v0.22.89
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
github.com/ucloud/ucloud-sdk-go v0.22.90
gopkg.in/yaml.v2 v2.2.2
)

Expand All @@ -25,5 +24,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3A
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ucloud/ucloud-sdk-go v0.22.89 h1:eCvCtkan5KIzzBv+QJgEQojCbwwS+hVtOq52hfGnWtg=
github.com/ucloud/ucloud-sdk-go v0.22.89/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.90 h1:OYYPYlWZFQ5DB15akEkrd/ty28zkd6Als7PQ6shywzk=
github.com/ucloud/ucloud-sdk-go v0.22.90/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
10 changes: 10 additions & 0 deletions products/redis/internal/redis/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ func NewCommand(ctx *cli.Context) *cobra.Command {
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDelete(ctx))
cmd.AddCommand(newRestart(ctx))
cmd.AddCommand(newModifyName(ctx))
cmd.AddCommand(newModifyPassword(ctx))
cmd.AddCommand(newListBlock(ctx))
cmd.AddCommand(newListProxy(ctx))
cmd.AddCommand(newFlush(ctx))
cmd.AddCommand(newIsolation(ctx))
cmd.AddCommand(newResize(ctx))
cmd.AddCommand(newCreateProxy(ctx))
cmd.AddCommand(newDeleteProxy(ctx))
cmd.AddCommand(newResizeProxy(ctx))
return cmd
}
53 changes: 53 additions & 0 deletions products/redis/internal/redis/create_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package redis

import (
"fmt"

"github.com/spf13/cobra"

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

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

// newCreateProxy returns ucloud redis create-proxy.
func newCreateProxy(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, umem.NewClient)
req := client.NewCreateUDRedisUhproxyRequest()
cmd := &cobra.Command{
Use: "create-proxy",
Short: "Create proxy for distributed redis",
Long: "Create proxy for distributed redis",
Example: "ucloud redis create-proxy --umem-id udb-xxx --cpu 2",
Run: func(c *cobra.Command, args []string) {
resp, err := client.CreateUDRedisUhproxy(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "proxy[%s] created\n", resp.ResourceId)
ctx.EmitResult(cli.OpResultRow{ResourceID: resp.ResourceId, Action: "create-proxy", Status: "Created"})
},
}

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

req.SpaceId = flags.String("umem-id", "", "Required. Resource ID of the distributed redis")
req.CPU = flags.Int("cpu", 2, "Required. CPU cores of the proxy")
req.Port = flags.Int("port", 6379, "Optional. Port of the proxy. Default value 6379")
req.ProxyCnt = flags.Int("proxy-cnt", 1, "Optional. Number of proxies to create. Default value 1")

ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)
ctx.BindProjectID(cmd, req)

cmd.MarkFlagRequired("umem-id")
cmd.MarkFlagRequired("cpu")
command.SetCompletion(cmd, "umem-id", func() []string {
return getIDList(ctx, *req.ProjectId, *req.Region)
})

return cmd
}
21 changes: 13 additions & 8 deletions products/redis/internal/redis/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package redis

import (
"fmt"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -31,16 +30,22 @@ func newDelete(ctx *cli.Context) *cobra.Command {
results := []cli.OpResultRow{}
for _, idname := range idNames {
id := ctx.PickResourceID(idname)
if strings.HasPrefix(id, "uredis") || strings.HasPrefix(id, "uhredis") || strings.HasPrefix(id, "uregionredis") {
mode, err := describeRedisMode(ctx, id)
if err != nil {
ctx.HandleError(err)
continue
}
switch mode {
case redisModeMasterReplica:
if deleteMasterReplica(ctx, &p, id) {
results = append(results, cli.OpResultRow{ResourceID: id, Action: "delete", Status: "Deleted"})
}
} else if strings.HasPrefix(id, "udredis") {
case redisModeDistributed:
if deleteDistributed(ctx, &p, id) {
results = append(results, cli.OpResultRow{ResourceID: id, Action: "delete", Status: "Deleted"})
}
} else {
fmt.Fprintf(ctx.ProgressWriter(), "redis[%s] unknown id prefix, skip\n", idname)
default:
fmt.Fprintf(ctx.ProgressWriter(), "redis[%s] unknown resource type, skip\n", idname)
}
}
ctx.EmitResult(results...)
Expand All @@ -54,16 +59,16 @@ func newDelete(ctx *cli.Context) *cobra.Command {
flags.StringVar(&p.region, "region", ctx.DefaultRegion(), "Optional. Override default region for this command invocation, see 'ucloud region'")
flags.StringVar(&p.zone, "zone", ctx.DefaultZone(), "Optional. Override default availability zone for this command invocation, see 'ucloud region'")
flags.StringVar(&p.projectID, "project-id", ctx.DefaultProjectID(), "Optional. Override default project-id for this command invocation, see 'ucloud project list'")

command.SetCompletion(cmd, "region", ctx.RegionList)
command.SetCompletion(cmd, "zone", func() []string { return ctx.ZoneList(p.region) })
command.SetCompletion(cmd, "project-id", ctx.ProjectList)

cmd.MarkFlagRequired("umem-id")

command.SetCompletion(cmd, "umem-id", func() []string {
return getIDList(ctx, p.projectID, p.region)
})

cmd.MarkFlagRequired("umem-id")

return cmd
}

Expand Down
51 changes: 51 additions & 0 deletions products/redis/internal/redis/delete_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package redis

import (
"fmt"

"github.com/spf13/cobra"

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

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

// newDeleteProxy returns ucloud redis delete-proxy.
func newDeleteProxy(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, umem.NewClient)
req := client.NewDeleteUDRedisProxyRequest()
cmd := &cobra.Command{
Use: "delete-proxy",
Short: "Delete proxy of distributed redis",
Long: "Delete proxy of distributed redis",
Example: "ucloud redis delete-proxy --umem-id udb-xxx --proxy-id proxy-xxx",
Run: func(c *cobra.Command, args []string) {
_, err := client.DeleteUDRedisProxy(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "proxy[%s] deleted\n", *req.ProxyId)
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.ProxyId, Action: "delete-proxy", Status: "Deleted"})
},
}

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

req.SpaceId = flags.String("umem-id", "", "Required. Resource ID of the distributed redis")
req.ProxyId = flags.String("proxy-id", "", "Required. Proxy ID of the proxy to delete")

ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)
ctx.BindProjectID(cmd, req)

cmd.MarkFlagRequired("umem-id")
cmd.MarkFlagRequired("proxy-id")
command.SetCompletion(cmd, "umem-id", func() []string {
return getIDList(ctx, *req.ProjectId, *req.Region)
})

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

import (
"fmt"

"github.com/spf13/cobra"

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

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

type flushParams struct {
flushType string
dbNum int
region string
zone string
projectID string
}

// newFlush returns ucloud redis flush.
func newFlush(ctx *cli.Context) *cobra.Command {
var idNames []string
var p flushParams
cmd := &cobra.Command{
Use: "flush",
Short: "Clear data of redis instances",
Long: "Clear data of redis instances. Master-replica instances call FlushallURedisGroup, distributed instances call RemoveUDRedisData",
Example: "ucloud redis flush --umem-id uredis-rl5xuxx/testcli1 --flush-type FlushAll",
Run: func(c *cobra.Command, args []string) {
results := []cli.OpResultRow{}
for _, idname := range idNames {
id := ctx.PickResourceID(idname)
mode, err := describeRedisMode(ctx, id)
if err != nil {
ctx.HandleError(err)
continue
}
switch mode {
case redisModeMasterReplica:
if flushMasterReplica(ctx, &p, id) {
results = append(results, cli.OpResultRow{ResourceID: id, Action: "flush", Status: "Flushed"})
}
case redisModeDistributed:
if flushDistributed(ctx, &p, id) {
results = append(results, cli.OpResultRow{ResourceID: id, Action: "flush", Status: "Flushed"})
}
default:
fmt.Fprintf(ctx.ProgressWriter(), "redis[%s] unknown resource type, skip\n", idname)
}
}
ctx.EmitResult(results...)
},
}

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

flags.StringSliceVar(&idNames, "umem-id", nil, "Required. Resource ID of redis instances to flush data")
flags.StringVar(&p.flushType, "flush-type", "FlushAll", "Optional. FlushType of redis flush. Only for master-replica instances. Accept values: 'FlushAll', 'FlushDb'")
flags.IntVar(&p.dbNum, "db-num", 0, "Optional. DbNum to flush. Only used when flush-type is FlushDb for master-replica instances")
flags.StringVar(&p.region, "region", ctx.DefaultRegion(), "Optional. Override default region for this command invocation, see 'ucloud region'")
flags.StringVar(&p.zone, "zone", ctx.DefaultZone(), "Optional. Override default availability zone for this command invocation, see 'ucloud region'")
flags.StringVar(&p.projectID, "project-id", ctx.DefaultProjectID(), "Optional. Override default project-id for this command invocation, see 'ucloud project list'")

command.SetCompletion(cmd, "region", ctx.RegionList)
command.SetCompletion(cmd, "zone", func() []string { return ctx.ZoneList(p.region) })
command.SetCompletion(cmd, "project-id", ctx.ProjectList)
command.SetCompletion(cmd, "umem-id", func() []string {
return getIDList(ctx, p.projectID, p.region)
})

cmd.MarkFlagRequired("umem-id")

return cmd
}

func flushMasterReplica(ctx *cli.Context, p *flushParams, id string) bool {
client := cli.NewServiceClient(ctx, umem.NewClient)
req := client.NewFlushallURedisGroupRequest()
req.Region = &p.region
req.Zone = &p.zone
req.ProjectId = &p.projectID
req.GroupId = &id
req.FlushType = &p.flushType
if p.flushType == "FlushDb" {
req.DbNum = sdk.Int(p.dbNum)
}
_, err := client.FlushallURedisGroup(req)
if err != nil {
ctx.HandleError(err)
return false
}
fmt.Fprintf(ctx.ProgressWriter(), "redis[%s] data flushed\n", id)
return true
}

func flushDistributed(ctx *cli.Context, p *flushParams, id string) bool {
client := cli.NewServiceClient(ctx, umem.NewClient)
req := client.NewRemoveUDRedisDataRequest()
req.Region = &p.region
req.Zone = &p.zone
req.ProjectId = &p.projectID
req.SpaceId = &id
_, err := client.RemoveUDRedisData(req)
if err != nil {
ctx.HandleError(err)
return false
}
fmt.Fprintf(ctx.ProgressWriter(), "redis[%s] data flushed\n", id)
return true
}
Loading
Loading