Skip to content

Commit 3adff9d

Browse files
authored
Merge pull request #16 from lixiaojun629/develop
Develop ULB
2 parents 826d588 + 2c019c8 commit 3adff9d

60 files changed

Lines changed: 5015 additions & 249 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export VERSION=0.1.7
1+
export VERSION=0.1.8
22

33
.PHONY : build
44
build:

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ Taking create uhost in Nigeria (region: air-nigeria) and bind a public IP as an
8383
First to create an uhost instance:
8484

8585
```
86-
$ ucloud uhost create --cpu 1 --memory 1 --password mypassword123 --image-id uimage-fya3qr
86+
$ ucloud uhost create --cpu 1 --memory 1 --password **** --image-id uimage-fya3qr
8787
88-
UHost:[uhost-tr1eau] created successfully!
88+
UHost:[uhost-tr1e] created successfully!
8989
```
9090

9191
*Note*
@@ -116,7 +116,7 @@ Configure the GlobalSSH to the uhost instance and login the instance via GlobalS
116116

117117
```
118118
$ ucloud gssh create --location Washington --target-ip 152.32.140.92
119-
ResourceID: uga-pdhxvs
119+
gssh[uga-0psxxx] created
120120
121121
$ ssh root@152.32.140.92.ipssh.net
122122
root@152.32.140.92.ipssh.net's password: password of the uhost instance

ansi/code.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
// Reference https://github.com/sindresorhus/ansi-escapes
1+
//Package ansi reference https://github.com/sindresorhus/ansi-escapes
22
package ansi
33

44
import (
55
"fmt"
66
)
77

8-
const ESC = "\x1b["
9-
const OSC = "\x1b]"
10-
const BEL = "\x07"
11-
const SEP = ";"
8+
const csi = "\x1b["
129

13-
var CursorLeft = fmt.Sprintf("%sG", ESC)
14-
var EraseDown = fmt.Sprintf("%sJ", ESC)
10+
// const OSC = "\x1b]"
11+
// const BEL = "\x07"
12+
const sep = ";"
13+
14+
//CursorLeft move cursor to the left side
15+
var CursorLeft = fmt.Sprintf("%sG", csi)
16+
17+
//EraseDown Erase the screen from the current line down to the bottom of the
18+
var EraseDown = fmt.Sprintf("%sJ", csi)
1519

1620
func CursorUp(count int) string {
17-
return fmt.Sprintf("%s%dA", ESC, count)
21+
return fmt.Sprintf("%s%dA", csi, count)
22+
}
23+
24+
//CursorTo
25+
func CursorTo(x, y int) string {
26+
return fmt.Sprintf("%s%d;%dH", csi, y+1, x+1)
1827
}

base/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/ucloud/ucloud-sdk-go/services/udisk"
99
"github.com/ucloud/ucloud-sdk-go/services/udpn"
1010
"github.com/ucloud/ucloud-sdk-go/services/uhost"
11+
"github.com/ucloud/ucloud-sdk-go/services/ulb"
1112
"github.com/ucloud/ucloud-sdk-go/services/unet"
1213
"github.com/ucloud/ucloud-sdk-go/services/vpc"
1314
"github.com/ucloud/ucloud-sdk-go/ucloud"
@@ -29,6 +30,7 @@ type Client struct {
2930
udpn.UDPNClient
3031
pathx.PathXClient
3132
udisk.UDiskClient
33+
ulb.ULBClient
3234
PrivateUHostClient
3335
}
3436

@@ -42,6 +44,7 @@ func NewClient(config *ucloud.Config, credential *auth.Credential) *Client {
4244
*udpn.NewClient(config, credential),
4345
*pathx.NewClient(config, credential),
4446
*udisk.NewClient(config, credential),
47+
*ulb.NewClient(config, credential),
4548
*puhost.NewClient(config, credential),
4649
}
4750
}

base/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
2929
const DefaultProfile = "default"
3030

3131
//Version 版本号
32-
const Version = "0.1.7"
32+
const Version = "0.1.8"
3333

3434
//ConfigIns 配置实例, 程序加载时生成
3535
var ConfigIns = &AggConfig{}

base/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ func PrintTableS(dataSet interface{}) {
156156
}
157157
}
158158

159+
//PrintList 打印表格或者JSON
160+
func PrintList(dataSet interface{}, json bool) {
161+
if json {
162+
PrintJSON(dataSet)
163+
} else {
164+
PrintTableS(dataSet)
165+
}
166+
}
167+
168+
//PrintDescribe 打印详情
169+
func PrintDescribe(attrs []DescribeTableRow, json bool) {
170+
if json {
171+
PrintJSON(attrs)
172+
} else {
173+
for _, attr := range attrs {
174+
fmt.Println(attr.Attribute)
175+
fmt.Println(attr.Content)
176+
fmt.Println()
177+
}
178+
}
179+
}
180+
159181
//PrintTable 以表格方式打印数据集合
160182
func PrintTable(dataSet interface{}, fieldList []string) {
161183
dataSetVal := reflect.ValueOf(dataSet)
@@ -223,6 +245,12 @@ func printTable(rowList []map[string]interface{}, fieldList []string, fieldWidth
223245
}
224246
}
225247

248+
//DescribeTableRow 详情表格通用表格行
249+
type DescribeTableRow struct {
250+
Attribute string
251+
Content string
252+
}
253+
226254
func calcCutWidth(text string) int {
227255
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
228256
width := 0

cmd/completion.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"strings"
2424

2525
"github.com/spf13/cobra"
26-
. "github.com/ucloud/ucloud-cli/base"
26+
27+
"github.com/ucloud/ucloud-cli/base"
2728
)
2829

2930
// NewCmdCompletion ucloud completion
@@ -78,15 +79,15 @@ func zshCompletion(cmd *cobra.Command) {
7879
autoload -U +X bashcompinit && bashcompinit
7980
complete -F /usr/local/bin/ucloud ucloud`)
8081
fmt.Println("If the following scripts are included in '~/.bash_profile' or '~/.bashrc', please remove it. The scripts used to auto complete words before ucloud cli v0.1.3")
81-
fmt.Printf("fpath=(~/%s $fpath)\n", ConfigPath)
82+
fmt.Printf("fpath=(~/%s $fpath)\n", base.ConfigPath)
8283
fmt.Println("autoload -U +X compinit && compinit")
8384
}
8485

8586
func getBashVersion() (version string, err error) {
8687
lookupBashVersion := exec.Command("bash", "-version")
8788
out, err := lookupBashVersion.Output()
8889
if err != nil {
89-
Cxt.PrintErr(err)
90+
base.Cxt.PrintErr(err)
9091
}
9192

9293
// Example

cmd/eip.go

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func NewCmdEIPAllocate() *cobra.Command {
226226
Use: "allocate",
227227
Short: "Allocate EIP",
228228
Long: "Allocate EIP",
229-
Example: "ucloud eip allocate --line BGP --bandwidth 2",
229+
Example: "ucloud eip allocate --line BGP --bandwidth-mb 2",
230230
Run: func(cmd *cobra.Command, args []string) {
231231
if *req.OperatorName == "BGP" {
232232
*req.OperatorName = "Bgp"
@@ -258,7 +258,6 @@ func NewCmdEIPAllocate() *cobra.Command {
258258
req.Tag = cmd.Flags().String("group", "Default", "Optional. Group of your EIP.")
259259
req.Name = cmd.Flags().String("name", "EIP", "Optional. Name of your EIP.")
260260
req.Remark = cmd.Flags().String("remark", "", "Optional. Remark of your EIP.")
261-
req.CouponId = cmd.Flags().String("coupon-id", "", "Optional. Coupon ID, The Coupon can deducte part of the payment")
262261
count = cmd.Flags().Int("count", 1, "Optional. Count of EIP to allocate")
263262

264263
cmd.Flags().SetFlagValues("line", "BGP", "International")
@@ -287,13 +286,28 @@ func NewCmdEIPBind() *cobra.Command {
287286
resourceType = cmd.Flags().String("resource-type", "uhost", "Requried. ResourceType, type of resource to bind with eip. 'uhost','vrouter','ulb','upm','hadoophost'.eg..")
288287
projectID = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
289288
region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
289+
290+
cmd.Flags().SetFlagValues("resource-type", "uhost", "vrouter", "ulb", "upm", "hadoophost", "fortresshost", "udockhost", "udhost", "natgw", "udb", "vpngw", "ucdr", "dbaudit")
291+
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
292+
return getAllEip(*projectID, *region, []string{status.EIP_FREE}, nil)
293+
})
294+
290295
cmd.MarkFlagRequired("eip-id")
291296
cmd.MarkFlagRequired("resource-id")
292-
cmd.Flags().SetFlagValues("resource-type", "uhost", "vrouter", "ulb", "upm", "hadoophost", "fortresshost", "udockhost", "udhost", "natgw", "udb", "vpngw", "ucdr", "dbaudit")
297+
293298
return cmd
294299
}
295300

296301
func bindEIP(resourceID, resourceType, eipID, projectID, region *string) {
302+
ip := net.ParseIP(*resourceID)
303+
if ip != nil {
304+
eipID, err := getEIPIDbyIP(ip, *projectID, *region)
305+
if err != nil {
306+
base.HandleError(err)
307+
} else {
308+
*resourceID = eipID
309+
}
310+
}
297311
req := base.BizClient.NewBindEIPRequest()
298312
req.ResourceId = resourceID
299313
req.ResourceType = resourceType
@@ -310,61 +324,80 @@ func bindEIP(resourceID, resourceType, eipID, projectID, region *string) {
310324

311325
//NewCmdEIPUnbind ucloud eip unbind
312326
func NewCmdEIPUnbind() *cobra.Command {
313-
314-
var req = base.BizClient.NewUnBindEIPRequest()
315-
var cmd = &cobra.Command{
327+
eipIDs := []string{}
328+
req := base.BizClient.NewUnBindEIPRequest()
329+
cmd := &cobra.Command{
316330
Use: "unbind",
317331
Short: "Unbind EIP with uhost",
318332
Long: "Unbind EIP with uhost",
319-
Example: "ucloud eip unbind --eip-id eip-xxx --resource-id uhost-xxx",
333+
Example: "ucloud eip unbind --eip-id eip-xxx",
320334
Run: func(cmd *cobra.Command, args []string) {
321-
req.ResourceType = sdk.String("uhost")
322-
_, err := base.BizClient.UnBindEIP(req)
323-
if err != nil {
324-
base.HandleError(err)
325-
} else {
335+
req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId))
336+
for _, eip := range eipIDs {
337+
eipIns, err := getEIP(base.PickResourceID(eip))
338+
if err != nil {
339+
base.HandleError(err)
340+
return
341+
}
342+
req.EIPId = sdk.String(base.PickResourceID(eip))
343+
req.ResourceId = sdk.String(eipIns.Resource.ResourceId)
344+
req.ResourceType = sdk.String(eipIns.Resource.ResourceType)
345+
_, err = base.BizClient.UnBindEIP(req)
346+
if err != nil {
347+
base.HandleError(err)
348+
return
349+
}
326350
base.Cxt.Printf("unbind EIP[%s] with %s[%s]\n", *req.EIPId, *req.ResourceType, *req.ResourceId)
327351
}
328352
},
329353
}
330-
cmd.Flags().SortFlags = false
331-
req.EIPId = cmd.Flags().String("eip-id", "", "Required. EIPId to unbind")
332-
req.ResourceId = cmd.Flags().String("resource-id", "", "Required. ResourceID , which is the UHostId of uhost")
333-
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
334-
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
354+
flags := cmd.Flags()
355+
flags.SortFlags = false
356+
357+
flags.StringSliceVar(&eipIDs, "eip-id", nil, "Required. Resource ID of eips to unbind with some resource")
358+
bindRegion(req, flags)
359+
bindProjectID(req, flags)
360+
335361
cmd.MarkFlagRequired("eip-id")
336-
cmd.MarkFlagRequired("resource-id")
362+
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
363+
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, nil)
364+
})
337365

338366
return cmd
339367
}
340368

341369
//NewCmdEIPRelease ucloud eip release
342370
func NewCmdEIPRelease() *cobra.Command {
343371
var ids []string
344-
var req = base.BizClient.NewReleaseEIPRequest()
345-
var cmd = &cobra.Command{
372+
req := base.BizClient.NewReleaseEIPRequest()
373+
cmd := &cobra.Command{
346374
Use: "release",
347375
Short: "Release EIP",
348376
Long: "Release EIP",
349377
Example: "ucloud eip release --eip-id eip-xx1,eip-xx2",
350378
Run: func(cmd *cobra.Command, args []string) {
379+
req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId))
351380
for _, id := range ids {
352-
req.EIPId = sdk.String(id)
381+
req.EIPId = sdk.String(base.PickResourceID(id))
353382
_, err := base.BizClient.ReleaseEIP(req)
354383
if err != nil {
355384
base.HandleError(err)
356385
} else {
357-
base.Cxt.Printf("released EIP[%v]\n", *req.EIPId)
386+
base.Cxt.Printf("eip[%s] released\n", *req.EIPId)
358387
}
359388
}
360389
},
361390
}
362-
cmd.Flags().SortFlags = false
363-
cmd.Flags().StringSliceVarP(&ids, "eip-id", "", make([]string, 0), "Required. EIPIds of the EIP you want to release")
364-
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
365-
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
391+
flags := cmd.Flags()
392+
flags.SortFlags = false
393+
flags.StringSliceVarP(&ids, "eip-id", "", nil, "Required. Resource ID of the EIPs you want to release")
394+
bindProjectID(req, flags)
395+
bindRegion(req, flags)
366396
cmd.MarkFlagRequired("eip-id")
367-
cmd.MarkFlagRequired("bandwidth")
397+
flags.SetFlagValuesFunc("eip-id", func() []string {
398+
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_FREE}, nil)
399+
})
400+
368401
return cmd
369402
}
370403

0 commit comments

Comments
 (0)