Skip to content

Commit 8940f0d

Browse files
authored
Merge pull request #38 from lixiaojun629/develop
fixbug & refactor
2 parents b327a6b + 501e3c4 commit 8940f0d

10 files changed

Lines changed: 192 additions & 129 deletions

File tree

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM ubuntu:18.04
22
RUN apt-get update && apt-get install wget -y
3-
RUN wget https://github.com/ucloud/ucloud-cli/releases/download/0.1.21/ucloud-cli-linux-0.1.21-amd64.tgz
4-
RUN tar -zxf ucloud-cli-linux-0.1.21-amd64.tgz -C /usr/local/bin/
3+
RUN wget https://github.com/ucloud/ucloud-cli/releases/download/0.1.22/ucloud-cli-linux-0.1.22-amd64.tgz
4+
RUN tar -zxf ucloud-cli-linux-0.1.22-amd64.tgz -C /usr/local/bin/
55
RUN echo "complete -C $(which ucloud) ucloud" >> ~/.bashrc

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ https://docs.ucloud.cn/software/cli/index
77

88
## Installing ucloud-cli on macOS or Linux
99

10-
**Using Homebrew(Preferred)**
10+
**Using Homebrew(Recommended on macOS)**
1111

1212
The [Homebrew](https://docs.brew.sh/Installation) package manager may be used on macOS and Linux.
1313
It could install ucloud-cli and its dependencies automatically by running command below.
@@ -34,7 +34,7 @@ If the error is still unresolved, try the following command for help.
3434
brew doctor
3535
```
3636

37-
**Building from source**
37+
**Building from source(Recommended if you have golang installed)**
3838

3939
If you have installed git and golang on your platform, you can fetch the source code of ucloud cli from github and complie it by yourself.
4040

@@ -44,16 +44,24 @@ cd ucloud-cli
4444
make install
4545
```
4646

47-
**Downloading binary release**
47+
Upgrade to latest version
48+
49+
```
50+
cd ucloud-cli
51+
git pull
52+
make install
53+
```
54+
55+
**Downloading binary release(Recommended on Linux)**
4856

4957
Vist the [releases page](https://github.com/ucloud/ucloud-cli/releases) of ucloud cli, and find the appropriate archive for your operating system and architecture.
5058
Download the archive , check the shasum256 hashcode and extract it to your $PATH
5159

5260
For example
5361
```
54-
curl -OL https://github.com/ucloud/ucloud-cli/releases/download/0.1.20/ucloud-cli-macosx-0.1.20-amd64.tgz
55-
echo "6953232b20f3474973cf234218097006a2e0d1d049c115da6c0e09c103762d4d *ucloud-cli-macosx-0.1.20-amd64.tgz" | shasum -c
56-
tar zxf ucloud-cli-macosx-0.1.20-amd64.tgz -C /usr/local/bin/
62+
curl -OL https://github.com/ucloud/ucloud-cli/releases/download/0.1.22/ucloud-cli-linux-0.1.22-amd64.tgz
63+
echo "efbfb6d36d99f692b1f9cc7c9e3858047bb7b4fca6205c454098267e660b41d9 *ucloud-cli-linux-0.1.22-amd64.tgz" | shasum -c //check shasum to verify whether the downloaded tarball was hijacked. get the shasum from release page
64+
tar zxf ucloud-cli-linux-0.1.22-amd64.tgz -C /usr/local/bin/
5765
```
5866

5967
## Installing ucloud cli on Windows
@@ -74,22 +82,22 @@ Download the archive , and extract it. Add binary file ucloud.exe to your enviro
7482
## Using ucloud cli in a Docker container
7583
If you have installed docker on your platform, pull the docker image embeded ucloud cli by follow command.
7684
```
77-
docker pull uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20
85+
docker pull uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.22
7886
```
7987

8088
The docker image was built by the Dockerfile as follow.
8189
```
8290
FROM ubuntu:18.04
8391
RUN apt-get update && apt-get install wget -y
84-
RUN wget https://github.com/ucloud/ucloud-cli/releases/download/0.1.20/ucloud-cli-linux-0.1.20-amd64.tgz
85-
RUN tar -zxf ucloud-cli-linux-0.1.20-amd64.tgz -C /usr/local/bin/
92+
RUN wget https://github.com/ucloud/ucloud-cli/releases/download/0.1.22/ucloud-cli-linux-0.1.22-amd64.tgz
93+
RUN tar -zxf ucloud-cli-linux-0.1.22-amd64.tgz -C /usr/local/bin/
8694
RUN echo "complete -C $(which ucloud) ucloud" >> ~/.bashrc #enable auto completion
8795
```
8896

8997
Create a docker container named ucloud-cli using the docker image your have pulled by following command.
9098

9199
```
92-
docker run --name ucloud-cli -it -d uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.20
100+
docker run --name ucloud-cli -it -d uhub.service.ucloud.cn/ucloudcli/ucloud-cli:0.1.22
93101
```
94102
Run bash command in ucloud-cli container, and then you could play with ucloud cli.
95103

@@ -175,7 +183,7 @@ $ ucloud uhost create --help
175183
Secondly, we're going to allocate an EIP and then bind it to the uhost created above.
176184

177185
```
178-
$ ucloud eip allocate --line International --bandwidth 1
186+
$ ucloud eip allocate --line International --bandwidth-mb 1
179187
allocate EIP[eip-xxx] IP:106.75.xx.xx Line:BGP
180188
181189
$ ucloud eip bind --eip-id eip-xxx --resource-id uhost-xxx

base/client.go

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616
"github.com/ucloud/ucloud-sdk-go/services/unet"
1717
"github.com/ucloud/ucloud-sdk-go/services/uphost"
1818
"github.com/ucloud/ucloud-sdk-go/services/vpc"
19-
"github.com/ucloud/ucloud-sdk-go/ucloud"
19+
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
2020
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
21+
"github.com/ucloud/ucloud-sdk-go/ucloud/request"
2122
)
2223

2324
//PrivateUHostClient 私有模块的uhost client 即未在官网开放的接口
@@ -52,22 +53,60 @@ type Client struct {
5253
}
5354

5455
// NewClient will return a aggregate client
55-
func NewClient(config *ucloud.Config, credential *auth.Credential) *Client {
56+
func NewClient(config *sdk.Config, credential *auth.Credential) *Client {
57+
var handler sdk.RequestHandler = func(c *sdk.Client, req request.Common) (request.Common, error) {
58+
err := req.SetProjectId(PickResourceID(req.GetProjectId()))
59+
return req, err
60+
}
61+
var (
62+
uaccountClient = *uaccount.NewClient(config, credential)
63+
uhostClient = *uhost.NewClient(config, credential)
64+
unetClient = *unet.NewClient(config, credential)
65+
vpcClient = *vpc.NewClient(config, credential)
66+
udpnClient = *udpn.NewClient(config, credential)
67+
pathxClient = *pathx.NewClient(config, credential)
68+
udiskClient = *udisk.NewClient(config, credential)
69+
ulbClient = *ulb.NewClient(config, credential)
70+
udbClient = *udb.NewClient(config, credential)
71+
umemClient = *umem.NewClient(config, credential)
72+
uphostClient = *uphost.NewClient(config, credential)
73+
puhostClient = *puhost.NewClient(config, credential)
74+
pudbClient = *pudb.NewClient(config, credential)
75+
pumemClient = *pumem.NewClient(config, credential)
76+
ppathxClient = *ppathx.NewClient(config, credential)
77+
)
78+
79+
uaccountClient.Client.AddRequestHandler(handler)
80+
uhostClient.Client.AddRequestHandler(handler)
81+
unetClient.Client.AddRequestHandler(handler)
82+
vpcClient.Client.AddRequestHandler(handler)
83+
udpnClient.Client.AddRequestHandler(handler)
84+
pathxClient.Client.AddRequestHandler(handler)
85+
udiskClient.Client.AddRequestHandler(handler)
86+
ulbClient.Client.AddRequestHandler(handler)
87+
udbClient.Client.AddRequestHandler(handler)
88+
umemClient.Client.AddRequestHandler(handler)
89+
uphostClient.Client.AddRequestHandler(handler)
90+
puhostClient.Client.AddRequestHandler(handler)
91+
pudbClient.Client.AddRequestHandler(handler)
92+
pumemClient.Client.AddRequestHandler(handler)
93+
ppathxClient.Client.AddRequestHandler(handler)
94+
5695
return &Client{
57-
*uaccount.NewClient(config, credential),
58-
*uhost.NewClient(config, credential),
59-
*unet.NewClient(config, credential),
60-
*vpc.NewClient(config, credential),
61-
*udpn.NewClient(config, credential),
62-
*pathx.NewClient(config, credential),
63-
*udisk.NewClient(config, credential),
64-
*ulb.NewClient(config, credential),
65-
*udb.NewClient(config, credential),
66-
*umem.NewClient(config, credential),
67-
*uphost.NewClient(config, credential),
68-
*puhost.NewClient(config, credential),
69-
*pudb.NewClient(config, credential),
70-
*pumem.NewClient(config, credential),
71-
*ppathx.NewClient(config, credential),
96+
uaccountClient,
97+
uhostClient,
98+
unetClient,
99+
vpcClient,
100+
udpnClient,
101+
pathxClient,
102+
udiskClient,
103+
ulbClient,
104+
udbClient,
105+
umemClient,
106+
uphostClient,
107+
puhostClient,
108+
pudbClient,
109+
pumemClient,
110+
ppathxClient,
72111
}
73112
}

base/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ func GetBizClient(ac *AggConfig) (*Client, error) {
530530
UserAgent: fmt.Sprintf("UCloud-CLI/%s", Version),
531531
LogLevel: log.FatalLevel,
532532
Region: ac.Region,
533-
Zone: ac.Zone,
534533
ProjectId: ac.ProjectID,
535534
MaxRetries: *ac.MaxRetryTimes,
536535
}

base/log.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (hook *LogRotateHook) Fire(entry *log.Entry) error {
122122
return err
123123
}
124124
entry.Logger.SetOutput(mfile)
125-
initLog()
126125
return nil
127126
}
128127

cmd/bandwidth.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func NewCmdSharedBWCreate() *cobra.Command {
8080
flags.SortFlags = false
8181
req.Name = flags.String("name", "", "Required. Name of the shared bandwidth instance")
8282
req.ShareBandwidth = flags.Int("bandwidth-mb", 20, "Optional. Unit:Mb. Bandwidth of the shared bandwidth. Range [20,5000]")
83-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
84-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
83+
bindRegion(req, flags)
84+
bindProjectID(req, flags)
8585
req.ChargeType = flags.String("charge-type", "Month", "Optional.'Year',pay yearly;'Month',pay monthly;'Dynamic', pay hourly")
8686
req.Quantity = flags.Int("quantity", 1, "Optional. The duration of the instance. N years/months.")
8787
flags.SetFlagValues("charge-type", "Month", "Year", "Dynamic")
@@ -140,8 +140,8 @@ func NewCmdSharedBWList(out io.Writer) *cobra.Command {
140140
flags := cmd.Flags()
141141
flags.SortFlags = false
142142

143-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
144-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
143+
bindRegion(req, flags)
144+
bindProjectID(req, flags)
145145
flags.StringSliceVar(&req.ShareBandwidthIds, "shared-bw-id", nil, "Resource ID of shared bandwidth instances to list")
146146

147147
return cmd
@@ -174,8 +174,8 @@ func NewCmdSharedBWResize() *cobra.Command {
174174

175175
req.ShareBandwidthId = flags.String("shared-bw-id", "", "Required. Resource ID of shared bandwidth instance to resize")
176176
req.ShareBandwidth = flags.Int("bandwidth-mb", 0, "Required. Unit:Mb. resize to bandwidth value")
177-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
178-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
177+
bindRegion(req, flags)
178+
bindProjectID(req, flags)
179179

180180
flags.SetFlagValuesFunc("shared-bw-id", func() []string {
181181
list, _ := getAllSharedBW(*req.ProjectId, *req.Region)
@@ -214,8 +214,8 @@ func NewCmdSharedBWDelete() *cobra.Command {
214214
flags.StringSliceVar(&ids, "shared-bw-id", nil, "Required. Resource ID of shared bandwidth instances to delete")
215215
req.EIPBandwidth = flags.Int("eip-bandwidth-mb", 1, "Optional. Bandwidth of the joined EIPs,after deleting the shared bandwidth instance")
216216
req.PayMode = flags.String("traffic-mode", "", "Optional. The charge mode of joined EIPs after deleting the shared bandwidth. Accept values:Bandwidth,Traffic")
217-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
218-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
217+
bindRegion(req, flags)
218+
bindProjectID(req, flags)
219219
flags.SetFlagValuesFunc("shared-bw-id", func() []string {
220220
list, _ := getAllSharedBW(*req.ProjectId, *req.Region)
221221
return list
@@ -309,8 +309,8 @@ func NewCmdBandwidthPkgCreate() *cobra.Command {
309309
start = flags.String("start-time", "", "Required. The time to enable bandwidth package. Local time, for example '2018-12-25/08:30:00'")
310310
end = flags.String("end-time", "", "Required. The time to disable bandwidth package. Local time, for example '2018-12-26/08:30:00'")
311311
req.Bandwidth = flags.Int("bandwidth-mb", 0, "Required. bandwidth of the bandwidth package to create.Range [1,800]. Unit:'Mb'.")
312-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
313-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
312+
bindRegion(req, flags)
313+
bindProjectID(req, flags)
314314

315315
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
316316
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, []string{status.EIP_CHARGE_BANDWIDTH})
@@ -365,8 +365,8 @@ func NewCmdBandwidthPkgList(out io.Writer) *cobra.Command {
365365
}
366366
flags := cmd.Flags()
367367
flags.SortFlags = false
368-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
369-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
368+
bindRegion(req, flags)
369+
bindProjectID(req, flags)
370370
req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset")
371371
req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit range [0,10000000]")
372372

@@ -398,8 +398,8 @@ func NewCmdBandwidthPkgDelete() *cobra.Command {
398398
flags := cmd.Flags()
399399
flags.SortFlags = false
400400
flags.StringSliceVar(&ids, "resource-id", nil, "Required, Resource ID of bandwidth package to delete")
401-
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
402-
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
401+
bindRegion(req, flags)
402+
bindProjectID(req, flags)
403403

404404
return cmd
405405
}

cmd/eip.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ type EIPRow struct {
6868
//NewCmdEIPList ucloud eip list
6969
func NewCmdEIPList(out io.Writer) *cobra.Command {
7070
req := base.BizClient.NewDescribeEIPRequest()
71-
fetchAll := sdk.Bool(false)
71+
fetchAll := false
72+
pageOff := false
7273
cmd := &cobra.Command{
7374
Use: "list",
7475
Short: "List all EIP instances",
7576
Long: `List all EIP instances`,
7677
Example: "ucloud eip list",
7778
Run: func(cmd *cobra.Command, args []string) {
7879
var eipList []unet.UnetEIPSet
79-
if *fetchAll == true {
80+
if fetchAll || pageOff {
8081
list, err := fetchAllEip(*req.ProjectId, *req.Region)
8182
if err != nil {
8283
base.HandleError(err)
@@ -114,12 +115,15 @@ func NewCmdEIPList(out io.Writer) *cobra.Command {
114115
},
115116
}
116117

117-
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Assign project-id")
118-
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Assign region")
119-
req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset default 0")
120-
req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit default 50, max value 100")
121-
fetchAll = cmd.Flags().Bool("list-all", false, "List all eip")
122-
cmd.Flags().SetFlagValues("list-all", "true", "false")
118+
flags := cmd.Flags()
119+
bindRegion(req, flags)
120+
bindProjectID(req, flags)
121+
req.Offset = flags.Int("offset", 0, "Optional. Offset default 0")
122+
req.Limit = flags.Int("limit", 50, "Optional. Limit default 50, max value 100")
123+
flags.BoolVar(&fetchAll, "list-all", false, "List all eip")
124+
flags.BoolVar(&pageOff, "page-off", false, "Optional. Paging or not. Accept values: true or false")
125+
flags.SetFlagValues("list-all", "true", "false")
126+
flags.MarkDeprecated("list-all", "please use '--page-off' instead")
123127

124128
return cmd
125129
}
@@ -226,29 +230,29 @@ func NewCmdEIPAllocate() *cobra.Command {
226230
Long: "Allocate EIP",
227231
Example: "ucloud eip allocate --line BGP --bandwidth-mb 2",
228232
Run: func(cmd *cobra.Command, args []string) {
229-
if *req.OperatorName == "BGP" {
230-
*req.OperatorName = "Bgp"
233+
if *req.OperatorName == "" {
234+
*req.OperatorName = getEIPLine(*req.Region)
231235
}
232236
for i := 0; i < *count; i++ {
233237
resp, err := base.BizClient.AllocateEIP(req)
234238
if err != nil {
235239
base.HandleError(err)
236-
} else {
237-
for _, eip := range resp.EIPSet {
238-
base.Cxt.Printf("allocate EIP[%s] ", eip.EIPId)
239-
for _, ip := range eip.EIPAddr {
240-
base.Cxt.Printf("IP:%s Line:%s \n", ip.IP, ip.OperatorName)
241-
}
240+
continue
241+
}
242+
for _, eip := range resp.EIPSet {
243+
base.Cxt.Printf("allocate EIP[%s] ", eip.EIPId)
244+
for _, ip := range eip.EIPAddr {
245+
base.Cxt.Printf("IP:%s Line:%s \n", ip.IP, ip.OperatorName)
242246
}
243247
}
244248
}
245249
},
246250
}
247251
cmd.Flags().SortFlags = false
248-
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.")
249252
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")
250-
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
251-
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
253+
req.OperatorName = cmd.Flags().String("line", "", "Optional. '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.")
254+
bindProjectID(req, cmd.Flags())
255+
bindRegion(req, cmd.Flags())
252256
req.PayMode = cmd.Flags().String("traffic-mode", "Bandwidth", "Optional. traffic-mode is an enumeration value. 'Traffic','Bandwidth' or 'ShareBandwidth'")
253257
req.ShareBandwidthId = cmd.Flags().String("share-bandwidth-id", "", "Optional. ShareBandwidthId, required only when traffic-mode is 'ShareBandwidth'")
254258
req.Quantity = cmd.Flags().Int("quantity", 1, "Optional. The duration of the instance. N years/months.")
@@ -261,7 +265,6 @@ func NewCmdEIPAllocate() *cobra.Command {
261265
cmd.Flags().SetFlagValues("line", "BGP", "International")
262266
cmd.Flags().SetFlagValues("traffic-mode", "Bandwidth", "Traffic", "ShareBandwidth")
263267
cmd.Flags().SetFlagValues("charge-type", "Month", "Year", "Dynamic", "Trial")
264-
cmd.MarkFlagRequired("line")
265268
cmd.MarkFlagRequired("bandwidth-mb")
266269
return cmd
267270
}

0 commit comments

Comments
 (0)