Skip to content

Commit 3210b7b

Browse files
authored
Merge pull request #145 from kyler-67/feature/ugncli
feat(ugn): add UGN CLI plugin
2 parents 62b2d4d + 60b76c4 commit 3210b7b

25 files changed

Lines changed: 1168 additions & 3 deletions

‎cmd/products.gen.go‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/sirupsen/logrus v1.8.3
1111
github.com/spf13/cobra v1.10.2
1212
github.com/spf13/pflag v1.0.10
13-
github.com/ucloud/ucloud-sdk-go v0.22.92
13+
github.com/ucloud/ucloud-sdk-go v0.22.96
1414
gopkg.in/yaml.v2 v2.2.8
1515
)
1616

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
4141
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4242
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
4343
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
44-
github.com/ucloud/ucloud-sdk-go v0.22.92 h1:HuT+JQfyEx9OFhluTQ/Zusb6obB+2kJ9OJlU/JREHR4=
45-
github.com/ucloud/ucloud-sdk-go v0.22.92/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
44+
github.com/ucloud/ucloud-sdk-go v0.22.96 h1:efyqknRMEaoik6lH1g3DqO0XBipz4xhurGAXcQcDc2Q=
45+
github.com/ucloud/ucloud-sdk-go v0.22.96/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
4646
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
4747
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
4848
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

‎products/ugn/internal/ugn/bw.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ugn
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/ucloud/ucloud-cli/pkg/cli"
7+
)
8+
9+
// newBW ucloud ugn bw
10+
func newBW(ctx *cli.Context) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "bw",
13+
Short: "List and manipulate ugn bandwidth packages",
14+
Long: "List and manipulate ugn bandwidth packages",
15+
}
16+
17+
cmd.AddCommand(newBWCreate(ctx))
18+
cmd.AddCommand(newBWDelete(ctx))
19+
cmd.AddCommand(newBWList(ctx))
20+
cmd.AddCommand(newBWModifyBandwidth(ctx))
21+
22+
return cmd
23+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ugn
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
9+
10+
"github.com/ucloud/ucloud-cli/pkg/cli"
11+
"github.com/ucloud/ucloud-cli/pkg/command"
12+
)
13+
14+
// newBWCreate ucloud ugn bw create
15+
func newBWCreate(ctx *cli.Context) *cobra.Command {
16+
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
17+
req := client.NewCreateSimpleUGNBwPackageRequest()
18+
cmd := &cobra.Command{
19+
Use: "create",
20+
Short: "Create a ugn bandwidth package",
21+
Long: "Create a ugn bandwidth package",
22+
Run: func(c *cobra.Command, args []string) {
23+
_, err := client.CreateSimpleUGNBwPackage(req)
24+
if err != nil {
25+
ctx.HandleError(err)
26+
return
27+
}
28+
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw created\n")
29+
ctx.EmitResult(cli.OpResultRow{Action: "create-bw", Status: "Created"})
30+
},
31+
}
32+
33+
flags := cmd.Flags()
34+
flags.SortFlags = false
35+
36+
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
37+
req.BandWidth = flags.Int("bandwidth", 0, "Required. Bandwidth value in Mbps")
38+
req.Path = flags.String("path", "IGP", "Path policy: Delay/IGP/TCO")
39+
req.PayMode = flags.String("pay-mode", "", "Required. Pay mode: FixedBw/Max5/Traffic")
40+
req.ChargeType = flags.String("charge-type", "", "Required. Charge type: Month/Postpay")
41+
req.RegionA = flags.String("region-a", "", "Required. Region A of the bandwidth package")
42+
req.RegionB = flags.String("region-b", "", "Required. Region B of the bandwidth package")
43+
req.Name = flags.String("name", "", "Optional. Bandwidth package name")
44+
req.Qos = flags.String("qos", "Platinum", "Optional. QoS: Diamond/Platinum/Gold")
45+
req.CouponId = flags.String("coupon-id", "", "Optional. Coupon ID")
46+
req.Quantity = flags.Float64("quantity", 1, "Optional. Duration in months, default 1")
47+
48+
ctx.BindProjectID(cmd, req)
49+
ctx.SetCompletion(cmd, "ugn-id", func() []string {
50+
return getAllUGNIdNames(ctx, *req.ProjectId)
51+
})
52+
ctx.SetCompletion(cmd, "project-id", ctx.ProjectList)
53+
command.SetFlagValues(cmd, "path", "Delay", "IGP", "TCO")
54+
command.SetFlagValues(cmd, "pay-mode", "FixedBw", "Max5", "Traffic")
55+
command.SetFlagValues(cmd, "charge-type", "Month", "Postpay")
56+
command.SetFlagValues(cmd, "qos", "Diamond", "Platinum", "Gold")
57+
58+
cmd.MarkFlagRequired("ugn-id")
59+
cmd.MarkFlagRequired("bandwidth")
60+
cmd.MarkFlagRequired("pay-mode")
61+
cmd.MarkFlagRequired("charge-type")
62+
cmd.MarkFlagRequired("region-a")
63+
cmd.MarkFlagRequired("region-b")
64+
65+
return cmd
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ugn
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
9+
10+
"github.com/ucloud/ucloud-cli/pkg/cli"
11+
)
12+
13+
// newBWDelete ucloud ugn bw delete
14+
func newBWDelete(ctx *cli.Context) *cobra.Command {
15+
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
16+
req := client.NewDeleteUGNBwPackageRequest()
17+
var yes bool
18+
19+
cmd := &cobra.Command{
20+
Use: "delete",
21+
Short: "Delete ugn bandwidth packages",
22+
Long: "Delete ugn bandwidth packages",
23+
Run: func(c *cobra.Command, args []string) {
24+
ok, err := ctx.Confirm(yes, "Are you sure you want to delete the bandwidth package?")
25+
if err != nil {
26+
ctx.HandleError(err)
27+
return
28+
}
29+
if !ok {
30+
return
31+
}
32+
_, err = client.DeleteUGNBwPackage(req)
33+
if err != nil {
34+
ctx.HandleError(err)
35+
return
36+
}
37+
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw[%s] deleted\n", *req.BwPackageID)
38+
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.BwPackageID, Action: "delete-bw", Status: "Deleted"})
39+
},
40+
}
41+
42+
flags := cmd.Flags()
43+
flags.SortFlags = false
44+
45+
req.BwPackageID = flags.String("bw-package-id", "", "Required. Resource ID of the bandwidth package to delete")
46+
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
47+
flags.BoolVarP(&yes, "yes", "y", false, "Optional. Skip the confirmation prompt.")
48+
49+
cmd.MarkFlagRequired("bw-package-id")
50+
cmd.MarkFlagRequired("ugn-id")
51+
52+
return cmd
53+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package ugn
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
7+
8+
"github.com/ucloud/ucloud-cli/internal/common"
9+
"github.com/ucloud/ucloud-cli/pkg/cli"
10+
)
11+
12+
// bwRow is the table row for `ucloud ugn bw list`.
13+
type bwRow struct {
14+
ResourceID string
15+
Name string
16+
BandwidthMbps float64
17+
RegionA string
18+
RegionB string
19+
Path string
20+
QoS string
21+
ChargeType string
22+
CreateTime string
23+
ExpireTime string
24+
}
25+
26+
// newBWList ucloud ugn bw list
27+
func newBWList(ctx *cli.Context) *cobra.Command {
28+
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
29+
req := client.NewGetSimpleUGNBwPackagesRequest()
30+
cmd := &cobra.Command{
31+
Use: "list",
32+
Short: "List ugn bandwidth packages",
33+
Long: "List ugn bandwidth packages",
34+
Run: func(c *cobra.Command, args []string) {
35+
*req.UGNID = ctx.PickResourceID(*req.UGNID)
36+
resp, err := client.GetSimpleUGNBwPackages(req)
37+
if err != nil {
38+
ctx.HandleError(err)
39+
return
40+
}
41+
rows := make([]bwRow, 0, len(resp.BwPackages))
42+
for _, bw := range resp.BwPackages {
43+
path := bw.Path
44+
if path == "None" {
45+
path = "IGP"
46+
}
47+
expireTime := ""
48+
if bw.ExpireTime > 0 {
49+
expireTime = common.FormatDate(bw.ExpireTime)
50+
}
51+
rows = append(rows, bwRow{
52+
ResourceID: bw.PackageID,
53+
Name: bw.Name,
54+
BandwidthMbps: bw.BandWidth,
55+
RegionA: bw.RegionA,
56+
RegionB: bw.RegionB,
57+
Path: path,
58+
QoS: bw.Qos,
59+
ChargeType: bw.PayMode,
60+
CreateTime: common.FormatDate(bw.CreateTime),
61+
ExpireTime: expireTime,
62+
})
63+
}
64+
ctx.PrintList(rows)
65+
},
66+
}
67+
68+
flags := cmd.Flags()
69+
flags.SortFlags = false
70+
71+
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
72+
ctx.BindOffset(cmd, req)
73+
req.Limit = flags.Int("limit", 50, "Optional. Limit")
74+
req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Project-id, see 'ucloud project list'")
75+
76+
cmd.MarkFlagRequired("ugn-id")
77+
ctx.SetCompletion(cmd, "ugn-id", func() []string {
78+
return getAllUGNIdNames(ctx, *req.ProjectId)
79+
})
80+
ctx.SetCompletion(cmd, "project-id", ctx.ProjectList)
81+
82+
return cmd
83+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ugn
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
9+
10+
"github.com/ucloud/ucloud-cli/pkg/cli"
11+
)
12+
13+
// newBWModifyBandwidth ucloud ugn bw modify-bandwidth
14+
func newBWModifyBandwidth(ctx *cli.Context) *cobra.Command {
15+
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
16+
req := client.NewModifyUGNBandwidthRequest()
17+
cmd := &cobra.Command{
18+
Use: "modify-bandwidth",
19+
Short: "Modify ugn bandwidth",
20+
Long: "Modify ugn bandwidth",
21+
Run: func(c *cobra.Command, args []string) {
22+
_, err := client.ModifyUGNBandwidth(req)
23+
if err != nil {
24+
ctx.HandleError(err)
25+
return
26+
}
27+
fmt.Fprintf(ctx.ProgressWriter(), "ugn bw[%s] bandwidth modified to %d\n", *req.PackageID, *req.BandWidth)
28+
ctx.EmitResult(cli.OpResultRow{ResourceID: *req.PackageID, Action: "modify-bandwidth", Status: "Modified"})
29+
},
30+
}
31+
32+
flags := cmd.Flags()
33+
flags.SortFlags = false
34+
35+
req.BandWidth = flags.Int("bandwidth", 0, "Required. New bandwidth value")
36+
req.PackageID = flags.String("package-id", "", "Required. Bandwidth package ID")
37+
req.UGNID = flags.String("ugn-id", "", "Required. Resource ID of the ugn instance")
38+
39+
cmd.MarkFlagRequired("bandwidth")
40+
cmd.MarkFlagRequired("package-id")
41+
cmd.MarkFlagRequired("ugn-id")
42+
43+
return cmd
44+
}

‎products/ugn/internal/ugn/cmd.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ugn
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/ucloud/ucloud-cli/pkg/cli"
7+
)
8+
9+
// NewCommand builds the `ugn` root command.
10+
func NewCommand(ctx *cli.Context) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "ugn",
13+
Short: "List and manipulate ugn instances",
14+
Long: "List and manipulate ugn instances",
15+
}
16+
17+
cmd.AddCommand(newList(ctx))
18+
cmd.AddCommand(newGet(ctx))
19+
cmd.AddCommand(newCreate(ctx))
20+
cmd.AddCommand(newDelete(ctx))
21+
cmd.AddCommand(newRegion(ctx))
22+
cmd.AddCommand(newBW(ctx))
23+
cmd.AddCommand(newNetwork(ctx))
24+
cmd.AddCommand(newRoute(ctx))
25+
26+
return cmd
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ugn
2+
3+
import (
4+
"fmt"
5+
6+
ugnsdk "github.com/ucloud/ucloud-sdk-go/services/ugn"
7+
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
8+
9+
"github.com/ucloud/ucloud-cli/pkg/cli"
10+
)
11+
12+
func getAllUGNIns(ctx *cli.Context, project string) ([]ugnsdk.UGN, error) {
13+
client := cli.NewServiceClient(ctx, ugnsdk.NewClient)
14+
req := client.NewListUGNRequest()
15+
req.ProjectId = sdk.String(cli.PickResourceID(project))
16+
list := make([]ugnsdk.UGN, 0)
17+
for offset, limit := 0, 50; offset <= 1000; offset += limit {
18+
req.Limit = sdk.Int(limit)
19+
req.Offset = sdk.Int(offset)
20+
resp, err := client.ListUGN(req)
21+
if err != nil {
22+
return nil, err
23+
}
24+
list = append(list, resp.UGNs...)
25+
if offset+limit >= resp.TotalCount {
26+
break
27+
}
28+
}
29+
return list, nil
30+
}
31+
32+
func getAllUGNIdNames(ctx *cli.Context, project string) []string {
33+
ugnInsList, err := getAllUGNIns(ctx, project)
34+
if err != nil {
35+
return nil
36+
}
37+
idNameList := []string{}
38+
for _, ugn := range ugnInsList {
39+
idNameList = append(idNameList, fmt.Sprintf("%s/%s", ugn.UGNID, ugn.Name))
40+
}
41+
return idNameList
42+
}

0 commit comments

Comments
 (0)