Skip to content

Commit 291bbdc

Browse files
committed
ulb vserver ssl-certificate
1 parent 4ef75ef commit 291bbdc

55 files changed

Lines changed: 4795 additions & 171 deletions

Some content is hidden

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

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/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/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

cmd/globalssh.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23+
"github.com/ucloud/ucloud-cli/base"
2324
. "github.com/ucloud/ucloud-cli/base"
2425
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
2526
)
@@ -71,26 +72,22 @@ func NewCmdGsshList() *cobra.Command {
7172
if err != nil {
7273
HandleError(err)
7374
} else {
74-
if global.json {
75-
PrintJSON(resp.InstanceSet)
76-
} else {
77-
list := make([]GSSHRow, 0)
78-
for _, gssh := range resp.InstanceSet {
79-
row := GSSHRow{}
80-
row.ResourceID = gssh.InstanceId
81-
row.SSHServerIP = gssh.TargetIP
82-
row.AcceleratingDomain = gssh.AcceleratingDomain
83-
row.SSHPort = gssh.Port
84-
row.Remark = gssh.Remark
85-
if val, ok := areaMap[gssh.Area]; ok {
86-
row.SSHServerLocation = val
87-
} else {
88-
row.SSHServerLocation = gssh.Area
89-
}
90-
list = append(list, row)
75+
list := make([]GSSHRow, 0)
76+
for _, gssh := range resp.InstanceSet {
77+
row := GSSHRow{}
78+
row.ResourceID = gssh.InstanceId
79+
row.SSHServerIP = gssh.TargetIP
80+
row.AcceleratingDomain = gssh.AcceleratingDomain
81+
row.SSHPort = gssh.Port
82+
row.Remark = gssh.Remark
83+
if val, ok := areaMap[gssh.Area]; ok {
84+
row.SSHServerLocation = val
85+
} else {
86+
row.SSHServerLocation = gssh.Area
9187
}
92-
PrintTable(list, []string{"ResourceID", "SSHServerIP", "AcceleratingDomain", "SSHServerLocation", "SSHPort", "Remark"})
88+
list = append(list, row)
9389
}
90+
base.PrintList(list, global.json)
9491
}
9592
},
9693
}

cmd/image.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,20 @@ func NewCmdUImageList() *cobra.Command {
7171
base.HandleError(err)
7272
return
7373
}
74-
if global.json {
75-
base.PrintJSON(resp.ImageSet)
76-
} else {
77-
list := make([]ImageRow, 0)
78-
for _, image := range resp.ImageSet {
79-
row := ImageRow{}
80-
row.ImageName = image.ImageName
81-
row.ImageID = image.ImageId
82-
row.BasicImage = image.OsName
83-
row.ExtensibleFeature = strings.Join(image.Features, ",")
84-
row.CreationTime = base.FormatDate(image.CreateTime)
85-
row.State = image.State
86-
if row.State == "Available" {
87-
list = append(list, row)
88-
}
74+
list := make([]ImageRow, 0)
75+
for _, image := range resp.ImageSet {
76+
row := ImageRow{}
77+
row.ImageName = image.ImageName
78+
row.ImageID = image.ImageId
79+
row.BasicImage = image.OsName
80+
row.ExtensibleFeature = strings.Join(image.Features, ",")
81+
row.CreationTime = base.FormatDate(image.CreateTime)
82+
row.State = image.State
83+
if row.State == "Available" {
84+
list = append(list, row)
8985
}
90-
base.PrintTable(list, []string{"ImageName", "ImageID", "BasicImage", "ExtensibleFeature", "CreationTime"})
9186
}
87+
base.PrintList(list, global.json)
9288
},
9389
}
9490
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")

cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewCmdRoot() *cobra.Command {
6161

6262
cmd.PersistentFlags().BoolVarP(&global.debug, "debug", "d", false, "Running in debug mode")
6363
cmd.PersistentFlags().BoolVarP(&global.json, "json", "j", false, "Print result in JSON format whenever possible")
64-
cmd.Flags().BoolVar(&global.version, "version", false, "Display version")
64+
cmd.Flags().BoolVarP(&global.version, "version", "v", false, "Display version")
6565
cmd.Flags().BoolVar(&global.completion, "completion", false, "Turn on auto completion according to the prompt")
6666
cmd.Flags().BoolVar(&global.config, "config", false, "Display configuration")
6767
cmd.Flags().BoolVar(&global.signup, "signup", false, "Launch UCloud sign up page in browser")
@@ -81,6 +81,9 @@ func NewCmdRoot() *cobra.Command {
8181
cmd.AddCommand(NewCmdBandwidthPkg())
8282
cmd.AddCommand(NewCmdSharedBW())
8383
cmd.AddCommand(NewCmdUDPN(out))
84+
cmd.AddCommand(NewCmdULB())
85+
cmd.AddCommand(NewCmdULBVserver())
86+
cmd.AddCommand(NewCmdULBSSL())
8487

8588
return cmd
8689
}
@@ -132,6 +135,8 @@ func Execute() {
132135
rootCmd.SetUsageTemplate(usageTmpl)
133136
resetHelpFunc(rootCmd)
134137

138+
// err := doc.GenReSTTree(rootCmd, "./doc")
139+
// fmt.Println(err)
135140
if err := rootCmd.Execute(); err != nil {
136141
os.Exit(1)
137142
}

0 commit comments

Comments
 (0)