Skip to content

Commit

Permalink
Merge pull request #16 from lixiaojun629/develop
Browse files Browse the repository at this point in the history
Develop ULB
  • Loading branch information
lixiaojun629 authored Jan 24, 2019
2 parents 826d588 + 2c019c8 commit 3adff9d
Show file tree
Hide file tree
Showing 60 changed files with 5,015 additions and 249 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export VERSION=0.1.7
export VERSION=0.1.8

.PHONY : build
build:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ Taking create uhost in Nigeria (region: air-nigeria) and bind a public IP as an
First to create an uhost instance:

```
$ ucloud uhost create --cpu 1 --memory 1 --password mypassword123 --image-id uimage-fya3qr
$ ucloud uhost create --cpu 1 --memory 1 --password **** --image-id uimage-fya3qr
UHost:[uhost-tr1eau] created successfully!
UHost:[uhost-tr1e] created successfully!
```

*Note*
Expand Down Expand Up @@ -116,7 +116,7 @@ Configure the GlobalSSH to the uhost instance and login the instance via GlobalS

```
$ ucloud gssh create --location Washington --target-ip 152.32.140.92
ResourceID: uga-pdhxvs
gssh[uga-0psxxx] created
$ ssh [email protected]
[email protected]'s password: password of the uhost instance
Expand Down
25 changes: 17 additions & 8 deletions ansi/code.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
// Reference https://github.com/sindresorhus/ansi-escapes
//Package ansi reference https://github.com/sindresorhus/ansi-escapes
package ansi

import (
"fmt"
)

const ESC = "\x1b["
const OSC = "\x1b]"
const BEL = "\x07"
const SEP = ";"
const csi = "\x1b["

var CursorLeft = fmt.Sprintf("%sG", ESC)
var EraseDown = fmt.Sprintf("%sJ", ESC)
// const OSC = "\x1b]"
// const BEL = "\x07"
const sep = ";"

//CursorLeft move cursor to the left side
var CursorLeft = fmt.Sprintf("%sG", csi)

//EraseDown Erase the screen from the current line down to the bottom of the
var EraseDown = fmt.Sprintf("%sJ", csi)

func CursorUp(count int) string {
return fmt.Sprintf("%s%dA", ESC, count)
return fmt.Sprintf("%s%dA", csi, count)
}

//CursorTo
func CursorTo(x, y int) string {
return fmt.Sprintf("%s%d;%dH", csi, y+1, x+1)
}
3 changes: 3 additions & 0 deletions base/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ucloud/ucloud-sdk-go/services/udisk"
"github.com/ucloud/ucloud-sdk-go/services/udpn"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
"github.com/ucloud/ucloud-sdk-go/services/ulb"
"github.com/ucloud/ucloud-sdk-go/services/unet"
"github.com/ucloud/ucloud-sdk-go/services/vpc"
"github.com/ucloud/ucloud-sdk-go/ucloud"
Expand All @@ -29,6 +30,7 @@ type Client struct {
udpn.UDPNClient
pathx.PathXClient
udisk.UDiskClient
ulb.ULBClient
PrivateUHostClient
}

Expand All @@ -42,6 +44,7 @@ func NewClient(config *ucloud.Config, credential *auth.Credential) *Client {
*udpn.NewClient(config, credential),
*pathx.NewClient(config, credential),
*udisk.NewClient(config, credential),
*ulb.NewClient(config, credential),
*puhost.NewClient(config, credential),
}
}
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.7"
const Version = "0.1.8"

//ConfigIns 配置实例, 程序加载时生成
var ConfigIns = &AggConfig{}
Expand Down
28 changes: 28 additions & 0 deletions base/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ func PrintTableS(dataSet interface{}) {
}
}

//PrintList 打印表格或者JSON
func PrintList(dataSet interface{}, json bool) {
if json {
PrintJSON(dataSet)
} else {
PrintTableS(dataSet)
}
}

//PrintDescribe 打印详情
func PrintDescribe(attrs []DescribeTableRow, json bool) {
if json {
PrintJSON(attrs)
} else {
for _, attr := range attrs {
fmt.Println(attr.Attribute)
fmt.Println(attr.Content)
fmt.Println()
}
}
}

//PrintTable 以表格方式打印数据集合
func PrintTable(dataSet interface{}, fieldList []string) {
dataSetVal := reflect.ValueOf(dataSet)
Expand Down Expand Up @@ -223,6 +245,12 @@ func printTable(rowList []map[string]interface{}, fieldList []string, fieldWidth
}
}

//DescribeTableRow 详情表格通用表格行
type DescribeTableRow struct {
Attribute string
Content string
}

func calcCutWidth(text string) int {
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
width := 0
Expand Down
7 changes: 4 additions & 3 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"strings"

"github.com/spf13/cobra"
. "github.com/ucloud/ucloud-cli/base"

"github.com/ucloud/ucloud-cli/base"
)

// NewCmdCompletion ucloud completion
Expand Down Expand Up @@ -78,15 +79,15 @@ func zshCompletion(cmd *cobra.Command) {
autoload -U +X bashcompinit && bashcompinit
complete -F /usr/local/bin/ucloud ucloud`)
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")
fmt.Printf("fpath=(~/%s $fpath)\n", ConfigPath)
fmt.Printf("fpath=(~/%s $fpath)\n", base.ConfigPath)
fmt.Println("autoload -U +X compinit && compinit")
}

func getBashVersion() (version string, err error) {
lookupBashVersion := exec.Command("bash", "-version")
out, err := lookupBashVersion.Output()
if err != nil {
Cxt.PrintErr(err)
base.Cxt.PrintErr(err)
}

// Example
Expand Down
87 changes: 60 additions & 27 deletions cmd/eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func NewCmdEIPAllocate() *cobra.Command {
Use: "allocate",
Short: "Allocate EIP",
Long: "Allocate EIP",
Example: "ucloud eip allocate --line BGP --bandwidth 2",
Example: "ucloud eip allocate --line BGP --bandwidth-mb 2",
Run: func(cmd *cobra.Command, args []string) {
if *req.OperatorName == "BGP" {
*req.OperatorName = "Bgp"
Expand Down Expand Up @@ -258,7 +258,6 @@ func NewCmdEIPAllocate() *cobra.Command {
req.Tag = cmd.Flags().String("group", "Default", "Optional. Group of your EIP.")
req.Name = cmd.Flags().String("name", "EIP", "Optional. Name of your EIP.")
req.Remark = cmd.Flags().String("remark", "", "Optional. Remark of your EIP.")
req.CouponId = cmd.Flags().String("coupon-id", "", "Optional. Coupon ID, The Coupon can deducte part of the payment")
count = cmd.Flags().Int("count", 1, "Optional. Count of EIP to allocate")

cmd.Flags().SetFlagValues("line", "BGP", "International")
Expand Down Expand Up @@ -287,13 +286,28 @@ func NewCmdEIPBind() *cobra.Command {
resourceType = cmd.Flags().String("resource-type", "uhost", "Requried. ResourceType, type of resource to bind with eip. 'uhost','vrouter','ulb','upm','hadoophost'.eg..")
projectID = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")

cmd.Flags().SetFlagValues("resource-type", "uhost", "vrouter", "ulb", "upm", "hadoophost", "fortresshost", "udockhost", "udhost", "natgw", "udb", "vpngw", "ucdr", "dbaudit")
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
return getAllEip(*projectID, *region, []string{status.EIP_FREE}, nil)
})

cmd.MarkFlagRequired("eip-id")
cmd.MarkFlagRequired("resource-id")
cmd.Flags().SetFlagValues("resource-type", "uhost", "vrouter", "ulb", "upm", "hadoophost", "fortresshost", "udockhost", "udhost", "natgw", "udb", "vpngw", "ucdr", "dbaudit")

return cmd
}

func bindEIP(resourceID, resourceType, eipID, projectID, region *string) {
ip := net.ParseIP(*resourceID)
if ip != nil {
eipID, err := getEIPIDbyIP(ip, *projectID, *region)
if err != nil {
base.HandleError(err)
} else {
*resourceID = eipID
}
}
req := base.BizClient.NewBindEIPRequest()
req.ResourceId = resourceID
req.ResourceType = resourceType
Expand All @@ -310,61 +324,80 @@ func bindEIP(resourceID, resourceType, eipID, projectID, region *string) {

//NewCmdEIPUnbind ucloud eip unbind
func NewCmdEIPUnbind() *cobra.Command {

var req = base.BizClient.NewUnBindEIPRequest()
var cmd = &cobra.Command{
eipIDs := []string{}
req := base.BizClient.NewUnBindEIPRequest()
cmd := &cobra.Command{
Use: "unbind",
Short: "Unbind EIP with uhost",
Long: "Unbind EIP with uhost",
Example: "ucloud eip unbind --eip-id eip-xxx --resource-id uhost-xxx",
Example: "ucloud eip unbind --eip-id eip-xxx",
Run: func(cmd *cobra.Command, args []string) {
req.ResourceType = sdk.String("uhost")
_, err := base.BizClient.UnBindEIP(req)
if err != nil {
base.HandleError(err)
} else {
req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId))
for _, eip := range eipIDs {
eipIns, err := getEIP(base.PickResourceID(eip))
if err != nil {
base.HandleError(err)
return
}
req.EIPId = sdk.String(base.PickResourceID(eip))
req.ResourceId = sdk.String(eipIns.Resource.ResourceId)
req.ResourceType = sdk.String(eipIns.Resource.ResourceType)
_, err = base.BizClient.UnBindEIP(req)
if err != nil {
base.HandleError(err)
return
}
base.Cxt.Printf("unbind EIP[%s] with %s[%s]\n", *req.EIPId, *req.ResourceType, *req.ResourceId)
}
},
}
cmd.Flags().SortFlags = false
req.EIPId = cmd.Flags().String("eip-id", "", "Required. EIPId to unbind")
req.ResourceId = cmd.Flags().String("resource-id", "", "Required. ResourceID , which is the UHostId of uhost")
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
flags := cmd.Flags()
flags.SortFlags = false

flags.StringSliceVar(&eipIDs, "eip-id", nil, "Required. Resource ID of eips to unbind with some resource")
bindRegion(req, flags)
bindProjectID(req, flags)

cmd.MarkFlagRequired("eip-id")
cmd.MarkFlagRequired("resource-id")
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, nil)
})

return cmd
}

//NewCmdEIPRelease ucloud eip release
func NewCmdEIPRelease() *cobra.Command {
var ids []string
var req = base.BizClient.NewReleaseEIPRequest()
var cmd = &cobra.Command{
req := base.BizClient.NewReleaseEIPRequest()
cmd := &cobra.Command{
Use: "release",
Short: "Release EIP",
Long: "Release EIP",
Example: "ucloud eip release --eip-id eip-xx1,eip-xx2",
Run: func(cmd *cobra.Command, args []string) {
req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId))
for _, id := range ids {
req.EIPId = sdk.String(id)
req.EIPId = sdk.String(base.PickResourceID(id))
_, err := base.BizClient.ReleaseEIP(req)
if err != nil {
base.HandleError(err)
} else {
base.Cxt.Printf("released EIP[%v]\n", *req.EIPId)
base.Cxt.Printf("eip[%s] released\n", *req.EIPId)
}
}
},
}
cmd.Flags().SortFlags = false
cmd.Flags().StringSliceVarP(&ids, "eip-id", "", make([]string, 0), "Required. EIPIds of the EIP you want to release")
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
flags := cmd.Flags()
flags.SortFlags = false
flags.StringSliceVarP(&ids, "eip-id", "", nil, "Required. Resource ID of the EIPs you want to release")
bindProjectID(req, flags)
bindRegion(req, flags)
cmd.MarkFlagRequired("eip-id")
cmd.MarkFlagRequired("bandwidth")
flags.SetFlagValuesFunc("eip-id", func() []string {
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_FREE}, nil)
})

return cmd
}

Expand Down
Loading

0 comments on commit 3adff9d

Please sign in to comment.