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.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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.87
github.com/ucloud/ucloud-sdk-go v0.22.89
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
gopkg.in/yaml.v2 v2.2.2
)
Expand Down
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.87 h1:IAQBH1bl27+nBlBFo/hx5mMuDOMQHFks7cpOTfgxK3U=
github.com/ucloud/ucloud-sdk-go v0.22.87/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
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=
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
44 changes: 44 additions & 0 deletions products/css/internal/css/appversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package css

import (
"fmt"

"github.com/spf13/cobra"

uessdk "github.com/ucloud/ucloud-sdk-go/services/ues"

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

// newAppVersion ucloud css app-version
func newAppVersion(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, uessdk.NewClient)
req := client.NewGetUESAppVersionRequest()
cmd := &cobra.Command{
Use: "app-version",
Short: "List available UES application versions",
Long: "List available UES application versions",
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.GetUESAppVersion(req)
if err != nil {
ctx.HandleError(err)
return
}
list := []AppVersionRow{}
for _, v := range resp.AppVersionList {
list = append(list, AppVersionRow{
AppName: v.AppName,
AppVersion: v.AppVersion,
IsMultiZone: fmt.Sprintf("%t", v.IsMultiZone),
})
}
ctx.PrintList(list)
},
}
flags := cmd.Flags()
flags.SortFlags = false
req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Assign project-id")
req.Region = flags.String("region", ctx.DefaultRegion(), "Optional. Assign region")
req.Zone = flags.String("zone", ctx.DefaultZone(), "Optional. Assign availability zone")
return cmd
}
27 changes: 27 additions & 0 deletions products/css/internal/css/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package css

import (
"github.com/spf13/cobra"

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

// NewCommand builds the `css` root command and mounts the subcommands.
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "css",
Short: "Manage UES (Elasticsearch/OpenSearch) instances",
Long: "Manage UES (Elasticsearch/OpenSearch) instances",
}
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDelete(ctx))
cmd.AddCommand(newDescribe(ctx))
cmd.AddCommand(newList(ctx))
cmd.AddCommand(newExpand(ctx))
cmd.AddCommand(newResize(ctx))
cmd.AddCommand(newRestart(ctx))
cmd.AddCommand(newDiskLimit(ctx))
cmd.AddCommand(newNodeConf(ctx))
cmd.AddCommand(newAppVersion(ctx))
return cmd
}
44 changes: 44 additions & 0 deletions products/css/internal/css/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package css

import (
"strings"

uessdk "github.com/ucloud/ucloud-sdk-go/services/ues"
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"

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

// getInstanceList returns "InstanceId/Name" completion candidates for instance-id flags.
func getInstanceList(ctx *cli.Context, states []string, project, region, zone string) []string {
client := cli.NewServiceClient(ctx, uessdk.NewClient)
req := client.NewListUESInstanceRequest()
req.ProjectId = sdk.String(project)
req.Region = sdk.String(region)
if zone != "" {
req.Zone = sdk.String(zone)
}
req.Limit = sdk.Int(50)
resp, err := client.ListUESInstance(req)
if err != nil {
// silent fail for completion
return nil
}
list := []string{}
for _, ins := range resp.ClusterSet {
if states != nil {
matched := false
for _, s := range states {
if ins.State == s {
matched = true
break
}
}
if !matched {
continue
}
}
list = append(list, ins.InstanceId+"/"+strings.Replace(ins.InstanceName, " ", "-", -1))
}
return list
}
84 changes: 84 additions & 0 deletions products/css/internal/css/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package css

import (
"encoding/base64"
"fmt"

"github.com/spf13/cobra"

uessdk "github.com/ucloud/ucloud-sdk-go/services/ues"
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"

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

// newCreate ucloud css create
func newCreate(ctx *cli.Context) *cobra.Command {
var async *bool
var servicePasswd *string
client := cli.NewServiceClient(ctx, uessdk.NewClient)
req := client.NewCreateUESInstanceRequest()
cmd := &cobra.Command{
Use: "create",
Short: "Create UES instance",
Long: "Create UES instance",
Run: func(cmd *cobra.Command, args []string) {
w := ctx.ProgressWriter()
// 服务密码需 base64 编码后提交;未指定时使用默认密码 changeme
passwd := *servicePasswd
if passwd == "" {
passwd = "changeme"
}
req.ServicePasswd = sdk.String(base64.StdEncoding.EncodeToString([]byte(passwd)))
resp, err := client.CreateUESInstance(req)
if err != nil {
ctx.HandleError(err)
return
}
text := fmt.Sprintf("ues[%s] is creating", resp.InstanceId)
if *async {
fmt.Fprintln(w, text)
} else {
ctx.PollerTo(w, describeUESInstanceByID(ctx)).Spoll(resp.InstanceId, text, []string{STATE_RUNNING, STATE_ABNORMAL})
}
ctx.EmitResult(cli.OpResultRow{ResourceID: resp.InstanceId, Action: "create", Status: "Creating"})
},
}
flags := cmd.Flags()
flags.SortFlags = false
req.InstanceName = flags.String("name", "", "Required. Instance name")
req.AppVersion = flags.String("app-version", "", "Required. Application version, e.g. 7.10.0")
req.AppName = flags.String("app-name", "elasticsearch", "Optional. Application name, default elasticsearch")
req.NodeConf = flags.String("node-conf", "", "Required. Node configuration identifier")
req.NodeDiskConf = flags.String("node-disk-conf", "CLOUD_RSSD", "Required. Node disk type")
req.NodeDiskSize = flags.Int("node-disk-size-gb", 100, "Optional. Node disk size in GB, default 100")
req.NodeSize = flags.Int("node-count", 3, "Optional. Node count, default 3")
req.KibanaNodeConf = flags.String("kibana-node-conf", "", "Required. Kibana node configuration")
req.KibanaNodeDiskConf = flags.String("kibana-disk-conf", "CLOUD_RSSD", "Required. Kibana disk type")
req.VPCId = flags.String("vpc-id", "", "Required. VPC ID")
req.SubnetId = flags.String("subnet-id", "", "Required. Subnet ID")
req.ServiceUserName = flags.String("service-username", "", "Optional. Service username. elasticsearch default 'elastic'; OpenSearch fixed 'admin'")
servicePasswd = flags.String("service-passwd", "", "Optional. Service password, default 'changeme'")
req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Assign project-id")
req.Region = flags.String("region", ctx.DefaultRegion(), "Optional. Assign region")
req.Zone = flags.String("zone", ctx.DefaultZone(), "Optional. Assign availability zone")
req.ChargeType = flags.String("charge-type", "Month", "Optional. 'Year', 'Month', or 'Dynamic', default Month")
req.Quantity = flags.Int("quantity", 1, "Optional. Purchase duration, default 1")
req.BusinessId = flags.String("business-id", "", "Optional. Business group ID")
req.Remark = flags.String("remark", "", "Optional. Remark")
async = flags.Bool("async", false, "Optional. Do not wait for creation to finish")

command.SetFlagValues(cmd, "charge-type", "Month", "Year", "Dynamic")

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("app-version")
cmd.MarkFlagRequired("node-conf")
cmd.MarkFlagRequired("node-disk-conf")
cmd.MarkFlagRequired("kibana-node-conf")
cmd.MarkFlagRequired("kibana-disk-conf")
cmd.MarkFlagRequired("vpc-id")
cmd.MarkFlagRequired("subnet-id")

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

import (
"fmt"

"github.com/spf13/cobra"

uessdk "github.com/ucloud/ucloud-sdk-go/services/ues"

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

// newDelete ucloud css delete
func newDelete(ctx *cli.Context) *cobra.Command {
var yes *bool
var instanceIDs *[]string
client := cli.NewServiceClient(ctx, uessdk.NewClient)
req := client.NewDeleteUESInstanceRequest()
cmd := &cobra.Command{
Use: "delete",
Short: "Delete UES instances",
Long: "Delete UES instances",
Run: func(cmd *cobra.Command, args []string) {
ok, err := ctx.Confirm(*yes, "Are you sure to delete UES instance(s)?")
if err != nil {
ctx.HandleError(err)
return
}
if !ok {
return
}
w := ctx.ProgressWriter()
results := []cli.OpResultRow{}
for _, idName := range *instanceIDs {
id := ctx.PickResourceID(idName)
req.InstanceId = &id
_, err := client.DeleteUESInstance(req)
if err != nil {
ctx.HandleError(err)
continue
}
fmt.Fprintf(w, "ues[%s] deleted\n", id)
results = append(results, cli.OpResultRow{ResourceID: id, Action: "delete", Status: "Deleted"})
}
ctx.EmitResult(results...)
},
}
flags := cmd.Flags()
flags.SortFlags = false
instanceIDs = flags.StringSlice("instance-id", nil, "Required. Instance ID(s) to delete")
req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Assign project-id")
req.Region = flags.String("region", ctx.DefaultRegion(), "Optional. Assign region")
req.Zone = flags.String("zone", ctx.DefaultZone(), "Optional. Assign availability zone")
yes = flags.BoolP("yes", "y", false, "Optional. Skip confirmation prompt")

command.SetCompletion(cmd, "instance-id", func() []string {
return getInstanceList(ctx, []string{STATE_RUNNING, STATE_STOPPED, STATE_ABNORMAL}, *req.ProjectId, *req.Region, "")
})

cmd.MarkFlagRequired("instance-id")

return cmd
}
Loading
Loading