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.8.3
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/ucloud/ucloud-sdk-go v0.22.92
github.com/ucloud/ucloud-sdk-go v0.22.96
gopkg.in/yaml.v2 v2.2.8
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ucloud/ucloud-sdk-go v0.22.92 h1:HuT+JQfyEx9OFhluTQ/Zusb6obB+2kJ9OJlU/JREHR4=
github.com/ucloud/ucloud-sdk-go v0.22.92/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.96 h1:efyqknRMEaoik6lH1g3DqO0XBipz4xhurGAXcQcDc2Q=
github.com/ucloud/ucloud-sdk-go v0.22.96/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
23 changes: 23 additions & 0 deletions products/ugn/internal/ugn/bw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ugn

import (
"github.com/spf13/cobra"

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

// newBW ucloud ugn bw
func newBW(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "bw",
Short: "List and manipulate ugn bandwidth packages",
Long: "List and manipulate ugn bandwidth packages",
}

cmd.AddCommand(newBWCreate(ctx))
cmd.AddCommand(newBWDelete(ctx))
cmd.AddCommand(newBWList(ctx))
cmd.AddCommand(newBWModifyBandwidth(ctx))

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

import (
"fmt"

"github.com/spf13/cobra"

ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"

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

// newBWCreate ucloud ugn bw create
func newBWCreate(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
req := client.NewCreateSimpleUGNBwPackageRequest()
cmd := &cobra.Command{
Use: "create",
Short: "Create a ugn bandwidth package",
Long: "Create a ugn bandwidth package",
Run: func(c *cobra.Command, args []string) {
_, err := client.CreateSimpleUGNBwPackage(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw created\n")
ctx.EmitResult(cli.OpResultRow{Action: "create-bw", Status: "Created"})
},
}

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

req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
req.BandWidth = flags.Int("bandwidth", 0, "Required. Bandwidth value in Mbps")
req.Path = flags.String("path", "IGP", "Path policy: Delay/IGP/TCO")
req.PayMode = flags.String("pay-mode", "", "Required. Pay mode: FixedBw/Max5/Traffic")
req.ChargeType = flags.String("charge-type", "", "Required. Charge type: Month/Postpay")
req.RegionA = flags.String("region-a", "", "Required. Region A of the bandwidth package")
req.RegionB = flags.String("region-b", "", "Required. Region B of the bandwidth package")
req.Name = flags.String("name", "", "Optional. Bandwidth package name")
req.Qos = flags.String("qos", "Platinum", "Optional. QoS: Diamond/Platinum/Gold")
req.CouponId = flags.String("coupon-id", "", "Optional. Coupon ID")
req.Quantity = flags.Float64("quantity", 1, "Optional. Duration in months, default 1")

ctx.BindProjectID(cmd, req)
ctx.SetCompletion(cmd, "ugn-id", func() []string {
return getAllUGNIdNames(ctx, *req.ProjectId)
})
ctx.SetCompletion(cmd, "project-id", ctx.ProjectList)
command.SetFlagValues(cmd, "path", "Delay", "IGP", "TCO")
command.SetFlagValues(cmd, "pay-mode", "FixedBw", "Max5", "Traffic")
command.SetFlagValues(cmd, "charge-type", "Month", "Postpay")
command.SetFlagValues(cmd, "qos", "Diamond", "Platinum", "Gold")

cmd.MarkFlagRequired("ugn-id")
cmd.MarkFlagRequired("bandwidth")
cmd.MarkFlagRequired("pay-mode")
cmd.MarkFlagRequired("charge-type")
cmd.MarkFlagRequired("region-a")
cmd.MarkFlagRequired("region-b")

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

import (
"fmt"

"github.com/spf13/cobra"

ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"

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

// newBWDelete ucloud ugn bw delete
func newBWDelete(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
req := client.NewDeleteUGNBwPackageRequest()
var yes bool

cmd := &cobra.Command{
Use: "delete",
Short: "Delete ugn bandwidth packages",
Long: "Delete ugn bandwidth packages",
Run: func(c *cobra.Command, args []string) {
ok, err := ctx.Confirm(yes, "Are you sure you want to delete the bandwidth package?")
if err != nil {
ctx.HandleError(err)
return
}
if !ok {
return
}
_, err = client.DeleteUGNBwPackage(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw[%s] deleted\n", *req.BwPackageID)
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.BwPackageID, Action: "delete-bw", Status: "Deleted"})
},
}

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

req.BwPackageID = flags.String("bw-package-id", "", "Required. Resource ID of the bandwidth package to delete")
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
flags.BoolVarP(&yes, "yes", "y", false, "Optional. Skip the confirmation prompt.")

cmd.MarkFlagRequired("bw-package-id")
cmd.MarkFlagRequired("ugn-id")

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

import (
"github.com/spf13/cobra"

ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"

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

// bwRow is the table row for `ucloud ugn bw list`.
type bwRow struct {
ResourceID string
Name string
BandwidthMbps float64
RegionA string
RegionB string
Path string
QoS string
ChargeType string
CreateTime string
ExpireTime string
}

// newBWList ucloud ugn bw list
func newBWList(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
req := client.NewGetSimpleUGNBwPackagesRequest()
cmd := &cobra.Command{
Use: "list",
Short: "List ugn bandwidth packages",
Long: "List ugn bandwidth packages",
Run: func(c *cobra.Command, args []string) {
*req.UGNID = ctx.PickResourceID(*req.UGNID)
resp, err := client.GetSimpleUGNBwPackages(req)
if err != nil {
ctx.HandleError(err)
return
}
rows := make([]bwRow, 0, len(resp.BwPackages))
for _, bw := range resp.BwPackages {
path := bw.Path
if path == "None" {
path = "IGP"
}
expireTime := ""
if bw.ExpireTime > 0 {
expireTime = common.FormatDate(bw.ExpireTime)
}
rows = append(rows, bwRow{
ResourceID: bw.PackageID,
Name: bw.Name,
BandwidthMbps: bw.BandWidth,
RegionA: bw.RegionA,
RegionB: bw.RegionB,
Path: path,
QoS: bw.Qos,
ChargeType: bw.PayMode,
CreateTime: common.FormatDate(bw.CreateTime),
ExpireTime: expireTime,
})
}
ctx.PrintList(rows)
},
}

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

req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
ctx.BindOffset(cmd, req)
req.Limit = flags.Int("limit", 50, "Optional. Limit")
req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Project-id, see 'ucloud project list'")

cmd.MarkFlagRequired("ugn-id")
ctx.SetCompletion(cmd, "ugn-id", func() []string {
return getAllUGNIdNames(ctx, *req.ProjectId)
})
ctx.SetCompletion(cmd, "project-id", ctx.ProjectList)

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

import (
"fmt"

"github.com/spf13/cobra"

ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"

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

// newBWModifyBandwidth ucloud ugn bw modify-bandwidth
func newBWModifyBandwidth(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
req := client.NewModifyUGNBandwidthRequest()
cmd := &cobra.Command{
Use: "modify-bandwidth",
Short: "Modify ugn bandwidth",
Long: "Modify ugn bandwidth",
Run: func(c *cobra.Command, args []string) {
_, err := client.ModifyUGNBandwidth(req)
if err != nil {
ctx.HandleError(err)
return
}
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw[%s] bandwidth modified to %d\n", *req.PackageID, *req.BandWidth)
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.PackageID, Action: "modify-bandwidth", Status: "Modified"})
},
}

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

req.BandWidth = flags.Int("bandwidth", 0, "Required. New bandwidth value")
req.PackageID = flags.String("package-id", "", "Required. Bandwidth package ID")
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")

cmd.MarkFlagRequired("bandwidth")
cmd.MarkFlagRequired("package-id")
cmd.MarkFlagRequired("ugn-id")

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

import (
"github.com/spf13/cobra"

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

// NewCommand builds the `ugn` root command.
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "ugn",
Short: "List and manipulate ugn instances",
Long: "List and manipulate ugn instances",
}

cmd.AddCommand(newList(ctx))
cmd.AddCommand(newGet(ctx))
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDelete(ctx))
cmd.AddCommand(newRegion(ctx))
cmd.AddCommand(newBW(ctx))
cmd.AddCommand(newNetwork(ctx))
cmd.AddCommand(newRoute(ctx))

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

import (
"fmt"

ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"

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

func getAllUGNIns(ctx *cli.Context, project string) ([]ugnsdk.UGN, error) {
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
req := client.NewListUGNRequest()
req.ProjectId = sdk.String(cli.PickResourceID(project))
list := make([]ugnsdk.UGN, 0)
for offset, limit := 0, 50; offset <= 1000; offset += limit {
req.Limit = sdk.Int(limit)
req.Offset = sdk.Int(offset)
resp, err := client.ListUGN(req)
if err != nil {
return nil, err
}
list = append(list, resp.UGNs...)
if offset+limit >= resp.TotalCount {
break
}
}
return list, nil
}

func getAllUGNIdNames(ctx *cli.Context, project string) []string {
ugnInsList, err := getAllUGNIns(ctx, project)
if err != nil {
return nil
}
idNameList := []string{}
for _, ugn := range ugnInsList {
idNameList = append(idNameList, fmt.Sprintf("%s/%s", ugn.UGNID, ugn.Name))
}
return idNameList
}
Loading
Loading