Skip to content

Commit 6254109

Browse files
committed
shared bandwidth
1 parent 20b3313 commit 6254109

5 files changed

Lines changed: 351 additions & 9 deletions

File tree

base/util.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,48 @@ func displaySlice(listVal reflect.Value, fieldList []string) {
175175
for i := 0; i < listVal.Len(); i++ {
176176
elemVal := listVal.Index(i)
177177
elemType := elemVal.Type()
178-
row := make(map[string]interface{})
178+
rows := []map[string]interface{}{}
179179
for j := 0; j < elemVal.NumField(); j++ {
180180
field := elemVal.Field(j)
181181
fieldName := elemType.Field(j).Name
182182
if _, ok := showFieldMap[fieldName]; ok {
183-
row[fieldName] = field.Interface()
184183
text := fmt.Sprintf("%v", field.Interface())
185-
width := calcWidth(text)
186-
if showFieldMap[fieldName] < width {
187-
showFieldMap[fieldName] = width
184+
cells := strings.Split(text, "\n")
185+
for i, cell := range cells {
186+
width := calcWidth(cell)
187+
if showFieldMap[fieldName] < width {
188+
showFieldMap[fieldName] = width
189+
}
190+
if len(rows) == i {
191+
rows = append(rows, make(map[string]interface{}))
192+
}
193+
rows[i][fieldName] = cell
188194
}
189195
}
190196
}
191-
rowList = append(rowList, row)
197+
rowList = append(rowList, rows...)
192198
}
193199
printTable(rowList, fieldList, showFieldMap)
194200
}
195201

196202
func printTable(rowList []map[string]interface{}, fieldList []string, fieldWidthMap map[string]int) {
203+
//打印表头
197204
for _, field := range fieldList {
198205
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]+GAP) + "s"
199206
fmt.Printf(tmpl, field)
200207
}
201208
fmt.Printf("\n")
202209

210+
//打印数据
203211
for _, row := range rowList {
204212
for _, field := range fieldList {
205213
cutWidth := calcCutWidth(fmt.Sprintf("%v", row[field]))
206214
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]-cutWidth+GAP) + "v"
207-
fmt.Printf(tmpl, row[field])
215+
if row[field] != nil {
216+
fmt.Printf(tmpl, row[field])
217+
} else {
218+
fmt.Printf(tmpl, "")
219+
}
208220
}
209221
fmt.Printf("\n")
210222
}

cmd/eip.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
2828

2929
"github.com/ucloud/ucloud-cli/base"
30+
"github.com/ucloud/ucloud-cli/model/status"
3031
)
3132

3233
//NewCmdEIP ucloud eip
@@ -44,6 +45,8 @@ func NewCmdEIP() *cobra.Command {
4445
cmd.AddCommand(NewCmdEIPUnbind())
4546
cmd.AddCommand(NewCmdEIPModifyBandwidth())
4647
cmd.AddCommand(NewCmdEIPSetChargeMode())
48+
cmd.AddCommand(NewCmdEIPJoinSharedBW())
49+
cmd.AddCommand(NewCmdEIPLeaveSharedBW())
4750
return cmd
4851
}
4952

@@ -440,3 +443,110 @@ func NewCmdEIPSetChargeMode() *cobra.Command {
440443
cmd.MarkFlagRequired("charge-mode")
441444
return cmd
442445
}
446+
447+
//NewCmdEIPJoinSharedBW ucloud eip join-shared-bw
448+
func NewCmdEIPJoinSharedBW() *cobra.Command {
449+
eipIDs := []string{}
450+
req := base.BizClient.NewAssociateEIPWithShareBandwidthRequest()
451+
cmd := &cobra.Command{
452+
Use: "join-shared-bw",
453+
Short: "Join shared bandwidth",
454+
Long: "Join shared bandwidth",
455+
Run: func(c *cobra.Command, args []string) {
456+
for _, eip := range eipIDs {
457+
req.EIPIds = append(req.EIPIds, base.PickResourceID(eip))
458+
}
459+
req.ShareBandwidthId = sdk.String(base.PickResourceID(*req.ShareBandwidthId))
460+
_, err := base.BizClient.AssociateEIPWithShareBandwidth(req)
461+
if err != nil {
462+
base.HandleError(err)
463+
return
464+
}
465+
base.Cxt.Printf("eip%v joined shared bandwidth[%s]\n", req.EIPIds, *req.ShareBandwidthId)
466+
},
467+
}
468+
flags := cmd.Flags()
469+
flags.SortFlags = false
470+
flags.StringSliceVar(&eipIDs, "eip-id", nil, "Required. Resource ID of EIPs to join shared bandwdith")
471+
req.ShareBandwidthId = flags.String("shared-bw-id", "", "Required. Resource ID of shared bandwidth to be joined")
472+
req.Region = flags.String("region", base.ConfigInstance.Region, "Optional. Region, see 'ucloud region'")
473+
req.ProjectId = flags.String("project-id", base.ConfigInstance.ProjectID, "Optional. Project-id, see 'ucloud project list'")
474+
flags.SetFlagValuesFunc("eip-id", func() []string {
475+
return getAllEip(*req.ProjectId, *req.Region, nil, []string{status.EIP_CHARGE_BANDWIDTH, status.EIP_CHARGE_TRAFFIC})
476+
})
477+
flags.SetFlagValuesFunc("shared-bw-id", func() []string {
478+
list, _ := getAllSharedBW(*req.ProjectId, *req.Region)
479+
return list
480+
})
481+
cmd.MarkFlagRequired("eip-id")
482+
cmd.MarkFlagRequired("shared-bw-id")
483+
484+
return cmd
485+
}
486+
487+
//NewCmdEIPLeaveSharedBW ucloud eip leave-shared-bw
488+
func NewCmdEIPLeaveSharedBW() *cobra.Command {
489+
eipIDs := []string{}
490+
req := base.BizClient.NewDisassociateEIPWithShareBandwidthRequest()
491+
cmd := &cobra.Command{
492+
Use: "leave-shared-bw",
493+
Short: "Leave shared bandwidth",
494+
Long: "Leave shared bandwidth",
495+
Run: func(c *cobra.Command, args []string) {
496+
if *req.ShareBandwidthId == "" {
497+
for _, eipID := range eipIDs {
498+
eipIns, err := getEIP(base.PickResourceID(eipID))
499+
if err != nil {
500+
base.HandleError(err)
501+
continue
502+
}
503+
sharedBWID := eipIns.ShareBandwidthSet.ShareBandwidthId
504+
if sharedBWID == "" {
505+
base.Cxt.Printf("eip[%s] doesn't join any shared bandwidth\n", eipID)
506+
continue
507+
}
508+
req.ShareBandwidthId = sdk.String(sharedBWID)
509+
req.EIPIds = []string{base.PickResourceID(eipID)}
510+
_, err = base.BizClient.DisassociateEIPWithShareBandwidth(req)
511+
if err != nil {
512+
base.HandleError(err)
513+
continue
514+
}
515+
base.Cxt.Printf("eip[%s] left shared bandwidth[%s]\n", eipID, sharedBWID)
516+
}
517+
} else {
518+
for _, id := range eipIDs {
519+
req.EIPIds = append(req.EIPIds, base.PickResourceID(id))
520+
}
521+
*req.ShareBandwidthId = base.PickResourceID(*req.ShareBandwidthId)
522+
_, err := base.BizClient.DisassociateEIPWithShareBandwidth(req)
523+
if err != nil {
524+
base.HandleError(err)
525+
return
526+
}
527+
base.Cxt.Printf("eip%v left shared bandwidth[%s]\n", eipIDs, *req.ShareBandwidthId)
528+
}
529+
},
530+
}
531+
flags := cmd.Flags()
532+
flags.SortFlags = false
533+
flags.StringSliceVar(&eipIDs, "eip-id", nil, "Required. Resource ID of EIPs to leave shared bandwidth")
534+
req.Bandwidth = flags.Int("bandwidth-mb", 1, "Required. Bandwidth of EIP after leaving shared bandwidth, ranging [1,300] for 'Traffic' charge mode, ranging [1,800] for 'Bandwidth' charge mode. Unit:Mb")
535+
req.PayMode = flags.String("charge-mode", "Bandwidth", "Optional. Charge mode of the EIP after leaving shared bandwidth, 'Bandwidth' or 'Traffic'")
536+
req.ShareBandwidthId = flags.String("shared-bw-id", "", "Optional. Resource ID of shared bandwidth instance, assign this flag to make the operation faster")
537+
req.Region = flags.String("region", base.ConfigInstance.Region, "Optional. Region, see 'ucloud region'")
538+
req.ProjectId = flags.String("project-id", base.ConfigInstance.ProjectID, "Optional. Project-id, see 'ucloud project list'")
539+
540+
flags.SetFlagValues("charge-mode", "Bandwidth", "Traffic")
541+
flags.SetFlagValuesFunc("eip-id", func() []string {
542+
return getAllEip(*req.ProjectId, *req.Region, nil, []string{status.EIP_CHARGE_SHARE})
543+
})
544+
flags.SetFlagValuesFunc("shared-bw-id", func() []string {
545+
list, _ := getAllSharedBW(*req.ProjectId, *req.Region)
546+
return list
547+
})
548+
549+
cmd.MarkFlagRequired("bandwidth")
550+
cmd.MarkFlagRequired("eip-id")
551+
return cmd
552+
}

cmd/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
func NewCmdUImage() *cobra.Command {
3333
cmd := &cobra.Command{
3434
Use: "image",
35-
Short: "List images",
36-
Long: `List images`,
35+
Short: "List and manipulate images",
36+
Long: `List and manipulate images`,
3737
Args: cobra.NoArgs,
3838
}
3939
writer := base.Cxt.GetWriter()

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func NewCmdRoot() *cobra.Command {
7878
cmd.AddCommand(NewCmdFirewall())
7979
cmd.AddCommand(NewCmdDisk())
8080
cmd.AddCommand(NewCmdBandwidthPkg())
81+
cmd.AddCommand(NewCmdSharedBW())
8182
return cmd
8283
}
8384

0 commit comments

Comments
 (0)