@@ -21,13 +21,12 @@ import (
2121 "strings"
2222 "time"
2323
24- "github.com/ucloud/ucloud-sdk-go/services/unet"
25-
2624 "github.com/spf13/cobra"
2725
26+ "github.com/ucloud/ucloud-sdk-go/services/unet"
2827 sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
2928
30- . "github.com/ucloud/ucloud-cli/base"
29+ "github.com/ucloud/ucloud-cli/base"
3130)
3231
3332//NewCmdEIP ucloud eip
@@ -43,6 +42,8 @@ func NewCmdEIP() *cobra.Command {
4342 cmd .AddCommand (NewCmdEIPRelease ())
4443 cmd .AddCommand (NewCmdEIPBind ())
4544 cmd .AddCommand (NewCmdEIPUnbind ())
45+ cmd .AddCommand (NewCmdEIPModifyBandwidth ())
46+ cmd .AddCommand (NewCmdEIPSetChargeMode ())
4647 return cmd
4748}
4849
@@ -52,7 +53,7 @@ type EIPRow struct {
5253 IP string
5354 ResourceID string
5455 Group string
55- Billing string
56+ ChargeMode string
5657 Bandwidth string
5758 BindResource string
5859 Status string
@@ -61,7 +62,7 @@ type EIPRow struct {
6162
6263//NewCmdEIPList ucloud eip list
6364func NewCmdEIPList () * cobra.Command {
64- req := BizClient .NewDescribeEIPRequest ()
65+ req := base . BizClient .NewDescribeEIPRequest ()
6566 fetchAll := sdk .Bool (false )
6667 cmd := & cobra.Command {
6768 Use : "list" ,
@@ -73,21 +74,21 @@ func NewCmdEIPList() *cobra.Command {
7374 if * fetchAll == true {
7475 list , err := fetchAllEip (* req .ProjectId , * req .Region )
7576 if err != nil {
76- HandleError (err )
77+ base . HandleError (err )
7778 return
7879 }
7980 eipList = list
8081 } else {
81- resp , err := BizClient .DescribeEIP (req )
82+ resp , err := base . BizClient .DescribeEIP (req )
8283 if err != nil {
83- HandleError (err )
84+ base . HandleError (err )
8485 return
8586 }
8687 eipList = resp .EIPSet
8788 }
8889
8990 if global .json {
90- PrintJSON (eipList )
91+ base . PrintJSON (eipList )
9192 } else {
9293 list := make ([]EIPRow , 0 )
9394 for _ , eip := range eipList {
@@ -98,7 +99,7 @@ func NewCmdEIPList() *cobra.Command {
9899 }
99100 row .ResourceID = eip .EIPId
100101 row .Group = eip .Tag
101- row .Billing = eip .PayMode
102+ row .ChargeMode = eip .PayMode
102103 row .Bandwidth = strconv .Itoa (eip .Bandwidth ) + "Mb"
103104 if eip .Resource .ResourceId != "" {
104105 row .BindResource = fmt .Sprintf ("%s|%s(%s)" , eip .Resource .ResourceName , eip .Resource .ResourceId , eip .Resource .ResourceType )
@@ -107,13 +108,13 @@ func NewCmdEIPList() *cobra.Command {
107108 row .ExpirationTime = time .Unix (int64 (eip .ExpireTime ), 0 ).Format ("2006-01-02" )
108109 list = append (list , row )
109110 }
110- PrintTable (list , [] string { "Name" , "IP" , "ResourceID" , "Group" , "Billing" , "Bandwidth" , "BindResource" , "Status" , "ExpirationTime" } )
111+ base . PrintTableS (list )
111112 }
112113 },
113114 }
114115
115- req .ProjectId = cmd .Flags ().String ("project-id" , ConfigInstance .ProjectID , "Assign project-id" )
116- req .Region = cmd .Flags ().String ("region" , ConfigInstance .Region , "Assign region" )
116+ req .ProjectId = cmd .Flags ().String ("project-id" , base . ConfigInstance .ProjectID , "Assign project-id" )
117+ req .Region = cmd .Flags ().String ("region" , base . ConfigInstance .Region , "Assign region" )
117118 req .Offset = cmd .Flags ().Int ("offset" , 0 , "Optional. Offset default 0" )
118119 req .Limit = cmd .Flags ().Int ("limit" , 50 , "Optional. Limit default 50, max value 100" )
119120 fetchAll = cmd .Flags ().Bool ("list-all" , false , "List all eip" )
@@ -138,14 +139,14 @@ func getEIPIDbyIP(ip net.IP, projectID, region string) (string, error) {
138139}
139140
140141func fetchAllEip (projectID , region string ) ([]unet.UnetEIPSet , error ) {
141- req := BizClient .NewDescribeEIPRequest ()
142+ req := base . BizClient .NewDescribeEIPRequest ()
142143 list := []unet.UnetEIPSet {}
143144 req .ProjectId = sdk .String (projectID )
144145 req .Region = sdk .String (region )
145146 for offset , step := 0 , 100 ; ; offset += step {
146147 req .Offset = & offset
147148 req .Limit = & step
148- resp , err := BizClient .DescribeEIP (req )
149+ resp , err := base . BizClient .DescribeEIP (req )
149150 if err != nil {
150151 return nil , err
151152 }
@@ -159,13 +160,39 @@ func fetchAllEip(projectID, region string) ([]unet.UnetEIPSet, error) {
159160 return list , nil
160161}
161162
162- func getAllEip (states []string , projectID , region string ) []string {
163+ //states,paymodes 为nil时,不作为过滤条件
164+ func getAllEip (projectID , region string , states , paymodes []string ) []string {
163165 list , err := fetchAllEip (projectID , region )
164166 if err != nil {
165167 return nil
166168 }
167169 strs := []string {}
168170 for _ , item := range list {
171+ rightState := false
172+ if states == nil {
173+ rightState = true
174+ } else {
175+ for _ , s := range states {
176+ if item .Status == s {
177+ rightState = true
178+ }
179+ }
180+ }
181+
182+ rightPayMode := false
183+ if paymodes == nil {
184+ rightPayMode = true
185+ } else {
186+ for _ , m := range paymodes {
187+ if item .PayMode == m {
188+ rightPayMode = true
189+ }
190+ }
191+ }
192+ if ! rightPayMode || ! rightState {
193+ continue
194+ }
195+
169196 ips := []string {}
170197 for _ , ip := range item .EIPAddr {
171198 ips = append (ips , ip .IP )
@@ -175,10 +202,23 @@ func getAllEip(states []string, projectID, region string) []string {
175202 return strs
176203}
177204
205+ func getEIP (eipID string ) (* unet.UnetEIPSet , error ) {
206+ req := base .BizClient .NewDescribeEIPRequest ()
207+ req .EIPIds = append (req .EIPIds , eipID )
208+ resp , err := base .BizClient .DescribeEIP (req )
209+ if err != nil {
210+ return nil , err
211+ }
212+ if len (resp .EIPSet ) == 1 {
213+ return & resp .EIPSet [0 ], nil
214+ }
215+ return nil , fmt .Errorf ("eip[%s] may not exist" , eipID )
216+ }
217+
178218//NewCmdEIPAllocate ucloud eip allocate
179219func NewCmdEIPAllocate () * cobra.Command {
180220 var count * int
181- var req = BizClient .NewAllocateEIPRequest ()
221+ var req = base . BizClient .NewAllocateEIPRequest ()
182222 var cmd = & cobra.Command {
183223 Use : "allocate" ,
184224 Short : "Allocate EIP" ,
@@ -189,14 +229,14 @@ func NewCmdEIPAllocate() *cobra.Command {
189229 * req .OperatorName = "Bgp"
190230 }
191231 for i := 0 ; i < * count ; i ++ {
192- resp , err := BizClient .AllocateEIP (req )
232+ resp , err := base . BizClient .AllocateEIP (req )
193233 if err != nil {
194- HandleError (err )
234+ base . HandleError (err )
195235 } else {
196236 for _ , eip := range resp .EIPSet {
197- Cxt .Printf ("allocate EIP[%s] " , eip .EIPId )
237+ base . Cxt .Printf ("allocate EIP[%s] " , eip .EIPId )
198238 for _ , ip := range eip .EIPAddr {
199- Cxt .Printf ("IP:%s Line:%s \n " , ip .IP , ip .OperatorName )
239+ base . Cxt .Printf ("IP:%s Line:%s \n " , ip .IP , ip .OperatorName )
200240 }
201241 }
202242 }
@@ -206,8 +246,8 @@ func NewCmdEIPAllocate() *cobra.Command {
206246 cmd .Flags ().SortFlags = false
207247 req .OperatorName = cmd .Flags ().String ("line" , "" , "Required. 'BGP' or 'International'. 'BGP' could be set in China mainland regions, such as cn-bj2 etc. 'International' could be set in the regions beyond mainland, such as hk, tw-kh, us-ws etc." )
208248 req .Bandwidth = cmd .Flags ().Int ("bandwidth-mb" , 0 , "Required. Bandwidth(Unit:Mbps).The range of value related to network charge mode. By traffic [1, 200]; by bandwidth [1,800] (Unit: Mbps); it could be 0 if the eip belong to the shared bandwidth" )
209- req .ProjectId = cmd .Flags ().String ("project-id" , ConfigInstance .ProjectID , "Optional. Assign project-id" )
210- req .Region = cmd .Flags ().String ("region" , ConfigInstance .Region , "Optional. Assign region" )
249+ req .ProjectId = cmd .Flags ().String ("project-id" , base . ConfigInstance .ProjectID , "Optional. Assign project-id" )
250+ req .Region = cmd .Flags ().String ("region" , base . ConfigInstance .Region , "Optional. Assign region" )
211251 req .PayMode = cmd .Flags ().String ("charge-mode" , "Bandwidth" , "Optional. charge-mode is an enumeration value. 'Traffic','Bandwidth' or 'ShareBandwidth'" )
212252 req .ShareBandwidthId = cmd .Flags ().String ("share-bandwidth-id" , "" , "Optional. ShareBandwidthId, required only when charge-mode is 'ShareBandwidth'" )
213253 req .Quantity = cmd .Flags ().Int ("quantity" , 1 , "Optional. The duration of the instance. N years/months." )
@@ -242,53 +282,53 @@ func NewCmdEIPBind() *cobra.Command {
242282 eipID = cmd .Flags ().String ("eip-id" , "" , "Required. EIPId to bind" )
243283 resourceID = cmd .Flags ().String ("resource-id" , "" , "Required. ResourceID , which is the UHostId of uhost" )
244284 resourceType = cmd .Flags ().String ("resource-type" , "uhost" , "Requried. ResourceType, type of resource to bind with eip. 'uhost','vrouter','ulb','upm','hadoophost'.eg.." )
245- projectID = cmd .Flags ().String ("project-id" , ConfigInstance .ProjectID , "Optional. Assign project-id" )
246- region = cmd .Flags ().String ("region" , ConfigInstance .Region , "Optional. Assign region" )
285+ projectID = cmd .Flags ().String ("project-id" , base . ConfigInstance .ProjectID , "Optional. Assign project-id" )
286+ region = cmd .Flags ().String ("region" , base . ConfigInstance .Region , "Optional. Assign region" )
247287 cmd .MarkFlagRequired ("eip-id" )
248288 cmd .MarkFlagRequired ("resource-id" )
249289 cmd .Flags ().SetFlagValues ("resource-type" , "uhost" , "vrouter" , "ulb" , "upm" , "hadoophost" , "fortresshost" , "udockhost" , "udhost" , "natgw" , "udb" , "vpngw" , "ucdr" , "dbaudit" )
250290 return cmd
251291}
252292
253293func bindEIP (resourceID , resourceType , eipID , projectID , region * string ) {
254- req := BizClient .NewBindEIPRequest ()
294+ req := base . BizClient .NewBindEIPRequest ()
255295 req .ResourceId = resourceID
256296 req .ResourceType = resourceType
257297 req .EIPId = eipID
258298 req .ProjectId = projectID
259299 req .Region = region
260- _ , err := BizClient .BindEIP (req )
300+ _ , err := base . BizClient .BindEIP (req )
261301 if err != nil {
262- HandleError (err )
302+ base . HandleError (err )
263303 } else {
264- Cxt .Printf ("bind EIP[%s] with %s[%s]\n " , * req .EIPId , * req .ResourceType , * req .ResourceId )
304+ base . Cxt .Printf ("bind EIP[%s] with %s[%s]\n " , * req .EIPId , * req .ResourceType , * req .ResourceId )
265305 }
266306}
267307
268308//NewCmdEIPUnbind ucloud eip unbind
269309func NewCmdEIPUnbind () * cobra.Command {
270310
271- var req = BizClient .NewUnBindEIPRequest ()
311+ var req = base . BizClient .NewUnBindEIPRequest ()
272312 var cmd = & cobra.Command {
273313 Use : "unbind" ,
274314 Short : "Unbind EIP with uhost" ,
275315 Long : "Unbind EIP with uhost" ,
276316 Example : "ucloud eip unbind --eip-id eip-xxx --resource-id uhost-xxx" ,
277317 Run : func (cmd * cobra.Command , args []string ) {
278318 req .ResourceType = sdk .String ("uhost" )
279- _ , err := BizClient .UnBindEIP (req )
319+ _ , err := base . BizClient .UnBindEIP (req )
280320 if err != nil {
281- HandleError (err )
321+ base . HandleError (err )
282322 } else {
283- Cxt .Printf ("unbind EIP[%s] with %s[%s]\n " , * req .EIPId , * req .ResourceType , * req .ResourceId )
323+ base . Cxt .Printf ("unbind EIP[%s] with %s[%s]\n " , * req .EIPId , * req .ResourceType , * req .ResourceId )
284324 }
285325 },
286326 }
287327 cmd .Flags ().SortFlags = false
288328 req .EIPId = cmd .Flags ().String ("eip-id" , "" , "Required. EIPId to unbind" )
289329 req .ResourceId = cmd .Flags ().String ("resource-id" , "" , "Required. ResourceID , which is the UHostId of uhost" )
290- req .ProjectId = cmd .Flags ().String ("project-id" , ConfigInstance .ProjectID , "Optional. Assign project-id" )
291- req .Region = cmd .Flags ().String ("region" , ConfigInstance .Region , "Optional. Assign region" )
330+ req .ProjectId = cmd .Flags ().String ("project-id" , base . ConfigInstance .ProjectID , "Optional. Assign project-id" )
331+ req .Region = cmd .Flags ().String ("region" , base . ConfigInstance .Region , "Optional. Assign region" )
292332 cmd .MarkFlagRequired ("eip-id" )
293333 cmd .MarkFlagRequired ("resource-id" )
294334
@@ -298,7 +338,7 @@ func NewCmdEIPUnbind() *cobra.Command {
298338//NewCmdEIPRelease ucloud eip release
299339func NewCmdEIPRelease () * cobra.Command {
300340 var ids []string
301- var req = BizClient .NewReleaseEIPRequest ()
341+ var req = base . BizClient .NewReleaseEIPRequest ()
302342 var cmd = & cobra.Command {
303343 Use : "release" ,
304344 Short : "Release EIP" ,
@@ -307,20 +347,96 @@ func NewCmdEIPRelease() *cobra.Command {
307347 Run : func (cmd * cobra.Command , args []string ) {
308348 for _ , id := range ids {
309349 req .EIPId = & id
310- _ , err := BizClient .ReleaseEIP (req )
350+ _ , err := base . BizClient .ReleaseEIP (req )
311351 if err != nil {
312- HandleError (err )
352+ base . HandleError (err )
313353 } else {
314- Cxt .Printf ("released EIP[%v]\n " , * req .EIPId )
354+ base . Cxt .Printf ("released EIP[%v]\n " , * req .EIPId )
315355 }
316356 }
317357 },
318358 }
319359 cmd .Flags ().SortFlags = false
320360 cmd .Flags ().StringSliceVarP (& ids , "eip-id" , "" , make ([]string , 0 ), "Required. EIPIds of the EIP you want to release" )
321- req .ProjectId = cmd .Flags ().String ("project-id" , ConfigInstance .ProjectID , "Optional. Assign project-id" )
322- req .Region = cmd .Flags ().String ("region" , ConfigInstance .Region , "Optional. Assign region" )
361+ req .ProjectId = cmd .Flags ().String ("project-id" , base . ConfigInstance .ProjectID , "Optional. Assign project-id" )
362+ req .Region = cmd .Flags ().String ("region" , base . ConfigInstance .Region , "Optional. Assign region" )
323363 cmd .MarkFlagRequired ("eip-id" )
324364 cmd .MarkFlagRequired ("bandwidth" )
325365 return cmd
326366}
367+
368+ //NewCmdEIPModifyBandwidth ucloud eip modify-bandwidth
369+ func NewCmdEIPModifyBandwidth () * cobra.Command {
370+ ids := []string {}
371+ req := base .BizClient .NewModifyEIPBandwidthRequest ()
372+ cmd := & cobra.Command {
373+ Use : "modify-bandwidth" ,
374+ Short : "Modify bandwith of EIP instances" ,
375+ Long : "Modify bandwith of EIP instances" ,
376+ Run : func (cmd * cobra.Command , args []string ) {
377+ for _ , id := range ids {
378+ id = base .PickResourceID (id )
379+ req .EIPId = & id
380+ _ , err := base .BizClient .ModifyEIPBandwidth (req )
381+ if err != nil {
382+ base .HandleError (err )
383+ } else {
384+ base .Cxt .Printf ("eip[%s]'s bandwidth modified\n " , id )
385+ }
386+ }
387+ },
388+ }
389+ cmd .Flags ().SortFlags = false
390+ cmd .Flags ().StringSliceVarP (& ids , "eip-id" , "" , nil , "Required, Resource ID of EIPs to modify bandwidth" )
391+ req .Bandwidth = cmd .Flags ().Int ("bandwidth-mb" , 0 , "Required. Bandwidth of EIP after modifed. Charge by traffic, range [1,300]; charge by bandwidth, range [1,800]" )
392+ req .ProjectId = cmd .Flags ().String ("project-id" , base .ConfigInstance .ProjectID , "Optional. Assign project-id" )
393+ req .Region = cmd .Flags ().String ("region" , base .ConfigInstance .Region , "Optional. Assign region" )
394+ cmd .Flags ().SetFlagValuesFunc ("eip-id" , func () []string {
395+ return getAllEip (* req .ProjectId , * req .Region , nil , nil )
396+ })
397+ cmd .MarkFlagRequired ("eip-id" )
398+ cmd .MarkFlagRequired ("bandwidth-mb" )
399+ return cmd
400+ }
401+
402+ //NewCmdEIPSetChargeMode ucloud eip modify-charge-mode
403+ func NewCmdEIPSetChargeMode () * cobra.Command {
404+ ids := []string {}
405+ req := base .BizClient .NewSetEIPPayModeRequest ()
406+ cmd := & cobra.Command {
407+ Use : "modify-charge-mode" ,
408+ Short : "Modify charge mode of EIP instances" ,
409+ Long : "Modify charge mode of EIP instances" ,
410+ Run : func (cmd * cobra.Command , args []string ) {
411+ for _ , id := range ids {
412+ id = base .PickResourceID (id )
413+ req .EIPId = & id
414+ eipIns , err := getEIP (id )
415+ if err != nil {
416+ base .HandleError (err )
417+ return
418+ }
419+ req .Bandwidth = sdk .Int (eipIns .Bandwidth )
420+ _ , err = base .BizClient .SetEIPPayMode (req )
421+ if err != nil {
422+ base .HandleError (err )
423+ } else {
424+ base .Cxt .Printf ("eip[%s]'s charge mode modified to %s\n " , id , * req .PayMode )
425+ }
426+ }
427+ },
428+ }
429+
430+ cmd .Flags ().SortFlags = false
431+ cmd .Flags ().StringSliceVarP (& ids , "eip-id" , "" , nil , "Required, Resource ID of EIPs to modify charge mode" )
432+ req .PayMode = cmd .Flags ().String ("charge-mode" , "" , "Required, Charge mode of eip, 'traffic' or 'bandwidth'" )
433+ req .ProjectId = cmd .Flags ().String ("project-id" , base .ConfigInstance .ProjectID , "Optional. Assign project-id" )
434+ req .Region = cmd .Flags ().String ("region" , base .ConfigInstance .Region , "Optional. Assign region" )
435+ cmd .Flags ().SetFlagValues ("charge-mode" , "Bandwidth" , "Traffic" )
436+ cmd .Flags ().SetFlagValuesFunc ("eip-id" , func () []string {
437+ return getAllEip (* req .ProjectId , * req .Region , nil , nil )
438+ })
439+ cmd .MarkFlagRequired ("eip-id" )
440+ cmd .MarkFlagRequired ("charge-mode" )
441+ return cmd
442+ }
0 commit comments