@@ -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+ }
0 commit comments