From 8e6ed54217b095cca6f552026f8e6eab5e9672d5 Mon Sep 17 00:00:00 2001 From: lixiaojun Date: Thu, 31 Jan 2019 18:41:27 +0800 Subject: [PATCH] optimize &bugfix --- README.md | 27 ++- base/util.go | 39 ++++ cmd/{share_bw.go => bandwidth.go} | 185 +++++++++++++++- cmd/completion.go | 25 +-- cmd/eip.go | 32 +-- cmd/firewall.go | 6 + cmd/project.go | 6 +- cmd/root.go | 7 +- cmd/uhost.go | 12 +- cmd/ulb.go | 202 ++++++++++++------ cmd/unet.go | 166 -------------- vendor/github.com/spf13/cobra/command.go | 93 +++++--- .../services/ulb/update_backend_attribute.go | 1 + 13 files changed, 473 insertions(+), 328 deletions(-) rename cmd/{share_bw.go => bandwidth.go} (55%) diff --git a/README.md b/README.md index e416cefd3c..f4c1f8fc93 100644 --- a/README.md +++ b/README.md @@ -42,18 +42,33 @@ $ make install ## Command Completion -The ucloud-cli include command completion feature and need configure it manually. Add following scripts to ~/.bash_profile or ~/.bashrc +The ucloud-cli include command completion feature and need configure it manually. + +**Bash shell** Add following scripts to ~/.bash_profile or ~/.bashrc ``` -complete -C /usr/local/bin/ucloud ucloud +complete -C $(which ucloud) ucloud ``` **Zsh shell** please add following scripts to ~/.zshrc ``` autoload -U +X bashcompinit && bashcompinit -complete -F /usr/local/bin/ucloud ucloud +complete -F $(which ucloud) ucloud +``` +Zsh builtin command bashcompinit may not work on some platform. If the scripts don't work on your OS, try following scripts ``` +_ucloud() { + read -l; + local cl="$REPLY"; + read -ln; + local cp="$REPLY"; + reply=(`COMP_SHELL=zsh COMP_LINE="$cl" COMP_POINT="$cp" ucloud`) +} + +compctl -K _ucloud ucloud +``` + ## Getting Started @@ -106,10 +121,10 @@ Secondly, we're going to allocate an EIP and bind to the instance created above. ``` $ ucloud eip allocate --line International --bandwidth 1 -EIPId:eip-xov13b,IP:152.32.140.92,Line:International +allocate EIP[eip-xxx] IP:106.75.xx.xx Line:BGP -$ ucloud eip bind --eip-id eip-xov13b --resource-id uhost-tr1eau -EIP: [eip-xov13b] bind with uhost:[uhost-tr1eau] successfully +$ ucloud eip bind --eip-id eip-xxx --resource-id uhost-xxx +bind EIP[eip-xxx] with uhost[uhost-xxx] ``` Configure the GlobalSSH to the uhost instance and login the instance via GlobalSSH diff --git a/base/util.go b/base/util.go index d769a68a12..1fd2d1cdf6 100644 --- a/base/util.go +++ b/base/util.go @@ -490,3 +490,42 @@ func WriteJSONFile(list interface{}, fileName string) error { } return nil } + +//GetFileList 补全文件名 +func GetFileList(suffix string) []string { + cmdLine := strings.TrimSpace(os.Getenv("COMP_LINE")) + words := strings.Split(cmdLine, " ") + last := words[len(words)-1] + pathPrefix := "." + + if !strings.HasPrefix(last, "-") { + pathPrefix = last + } + hasTilde := false + //https://tiswww.case.edu/php/chet/bash/bashref.html#Tilde-Expansion + if strings.HasPrefix(pathPrefix, "~") { + pathPrefix = strings.Replace(pathPrefix, "~", GetHomePath(), 1) + hasTilde = true + } + files, err := ioutil.ReadDir(pathPrefix) + if err != nil { + log.Fatal(err) + return nil + } + names := []string{} + for _, f := range files { + name := f.Name() + if !strings.HasSuffix(name, suffix) { + continue + } + if hasTilde { + pathPrefix = strings.Replace(pathPrefix, GetHomePath(), "~", 1) + } + if strings.HasSuffix(pathPrefix, "/") { + names = append(names, pathPrefix+name) + } else { + names = append(names, pathPrefix+"/"+name) + } + } + return names +} diff --git a/cmd/share_bw.go b/cmd/bandwidth.go similarity index 55% rename from cmd/share_bw.go rename to cmd/bandwidth.go index a3e1465ee1..e011527a5c 100644 --- a/cmd/share_bw.go +++ b/cmd/bandwidth.go @@ -18,18 +18,32 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/spf13/cobra" sdk "github.com/ucloud/ucloud-sdk-go/ucloud" "github.com/ucloud/ucloud-cli/base" + "github.com/ucloud/ucloud-cli/model/status" ) +//NewCmdBandwidth ucloud bw +func NewCmdBandwidth() *cobra.Command { + cmd := &cobra.Command{ + Use: "bw", + Short: "Manipulate bandwidth package and shared bandwidth", + Long: "Manipulate bandwidth package and shared bandwidth", + } + cmd.AddCommand(NewCmdBandwidthPkg()) + cmd.AddCommand(NewCmdSharedBW()) + return cmd +} + //NewCmdSharedBW ucloud shared-bw func NewCmdSharedBW() *cobra.Command { cmd := &cobra.Command{ - Use: "shared-bw", + Use: "shared", Short: "Create and manipulate shared bandwidth instances", Long: "Create and manipulate shared bandwidth instances", } @@ -201,14 +215,14 @@ func NewCmdSharedBWDelete() *cobra.Command { flags.StringSliceVar(&ids, "shared-bw-id", nil, "Required. Resource ID of shared bandwidth instances to delete") req.EIPBandwidth = flags.Int("eip-bandwidth-mb", 1, "Optional. Bandwidth of the joined EIPs,after deleting the shared bandwidth instance") - req.PayMode = flags.String("charge-mode", "", "Optional. Charge mode of joined EIPs,after deleting the shared bandwidth") + req.PayMode = flags.String("traffic-mode", "", "Optional. The charge mode of joined EIPs after deleting the shared bandwidth. Accept values:Bandwidth,Traffic") req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") flags.SetFlagValuesFunc("shared-bw-id", func() []string { list, _ := getAllSharedBW(*req.ProjectId, *req.Region) return list }) - flags.SetFlagValues("charge-mode", "Bandwidth", "Traffic") + flags.SetFlagValues("traffic-mode", "Bandwidth", "Traffic") cmd.MarkFlagRequired("shared-bw-id") @@ -229,3 +243,168 @@ func getAllSharedBW(project, region string) ([]string, error) { } return list, nil } + +//NewCmdBandwidthPkg ucloud bw-pkg +func NewCmdBandwidthPkg() *cobra.Command { + cmd := &cobra.Command{ + Use: "pkg", + Short: "List, create and delete bandwidth package instances", + Long: "List, create and delete bandwidth package instances", + } + cmd.AddCommand(NewCmdBandwidthPkgCreate()) + cmd.AddCommand(NewCmdBandwidthPkgList()) + cmd.AddCommand(NewCmdBandwidthPkgDelete()) + return cmd +} + +//NewCmdBandwidthPkgCreate ucloud bw-pkg create +func NewCmdBandwidthPkgCreate() *cobra.Command { + var start, end *string + timeLayout := "2006-01-02/15:04:05" + ids := []string{} + req := base.BizClient.NewCreateBandwidthPackageRequest() + loc, _ := time.LoadLocation("Local") + cmd := &cobra.Command{ + Use: "create", + Short: "Create bandwidth package", + Long: "Create bandwidth package", + Example: "ucloud bw pkg create --eip-id eip-xxx --bandwidth-mb 20 --start-time 2018-12-15/09:20:00 --end-time 2018-12-16/09:20:00", + Run: func(c *cobra.Command, args []string) { + st, err := time.ParseInLocation(timeLayout, *start, loc) + if err != nil { + base.HandleError(err) + return + } + et, err := time.ParseInLocation(timeLayout, *end, loc) + if err != nil { + base.HandleError(err) + return + } + if st.Sub(time.Now()) < 0 { + base.Cxt.Println("start-time must be after the current time") + return + } + du := et.Unix() - st.Unix() + if du <= 0 { + base.Cxt.Println("end-time must be after the start-time") + return + } + req.EnableTime = sdk.Int(int(st.Unix())) + req.TimeRange = sdk.Int(int(du)) + + for _, id := range ids { + id = base.PickResourceID(id) + req.EIPId = &id + resp, err := base.BizClient.CreateBandwidthPackage(req) + if err != nil { + base.HandleError(err) + continue + } + base.Cxt.Printf("bandwidth package[%s] created for eip[%s]\n", resp.BandwidthPackageId, id) + } + }, + } + flags := cmd.Flags() + flags.SortFlags = false + flags.StringSliceVar(&ids, "eip-id", nil, "Required. Resource ID of eip to be bound with created bandwidth package") + start = flags.String("start-time", "", "Required. The time to enable bandwidth package. Local time, for example '2018-12-25/08:30:00'") + end = flags.String("end-time", "", "Required. The time to disable bandwidth package. Local time, for example '2018-12-26/08:30:00'") + req.Bandwidth = flags.Int("bandwidth-mb", 0, "Required. bandwidth of the bandwidth package to create.Range [1,800]. Unit:'Mb'.") + req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") + req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") + + cmd.Flags().SetFlagValuesFunc("eip-id", func() []string { + return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, []string{status.EIP_CHARGE_BANDWIDTH}) + }) + + cmd.MarkFlagRequired("eip-id") + cmd.MarkFlagRequired("start-time") + cmd.MarkFlagRequired("end-time") + cmd.MarkFlagRequired("bandwidth-mb") + return cmd +} + +//BandwidthPkgRow 表格行 +type BandwidthPkgRow struct { + ResourceID string + EIP string + Bandwidth string + StartTime string + EndTime string +} + +//NewCmdBandwidthPkgList ucloud bw-pkg list +func NewCmdBandwidthPkgList() *cobra.Command { + req := base.BizClient.NewDescribeBandwidthPackageRequest() + cmd := &cobra.Command{ + Use: "list", + Short: "List bandwidth packages", + Long: "List bandwidth packages", + Run: func(c *cobra.Command, args []string) { + resp, err := base.BizClient.DescribeBandwidthPackage(req) + if err != nil { + base.HandleError(err) + return + } + list := []BandwidthPkgRow{} + for _, bp := range resp.DataSets { + row := BandwidthPkgRow{ + ResourceID: bp.BandwidthPackageId, + Bandwidth: strconv.Itoa(bp.Bandwidth) + "MB", + StartTime: base.FormatDateTime(bp.EnableTime), + EndTime: base.FormatDateTime(bp.DisableTime), + } + eip := bp.EIPId + for _, addr := range bp.EIPAddr { + eip += "/" + addr.IP + "/" + addr.OperatorName + } + row.EIP = eip + list = append(list, row) + } + if global.json { + base.PrintJSON(list) + } else { + base.PrintTableS(list) + } + }, + } + flags := cmd.Flags() + flags.SortFlags = false + req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") + req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") + req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset") + req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit range [0,10000000]") + + return cmd +} + +//NewCmdBandwidthPkgDelete ucloud bw-pkg delete +func NewCmdBandwidthPkgDelete() *cobra.Command { + ids := []string{} + req := base.BizClient.NewDeleteBandwidthPackageRequest() + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete bandwidth packages", + Long: "Delete bandwidth packages", + Example: "ucloud bw pkg delete --resource-id bwpack-xxx", + Run: func(c *cobra.Command, args []string) { + for _, id := range ids { + id := base.PickResourceID(id) + req.BandwidthPackageId = &id + _, err := base.BizClient.DeleteBandwidthPackage(req) + if err != nil { + base.HandleError(err) + return + } + base.Cxt.Printf("bandwidth package[%s] deleted\n", id) + } + }, + } + flags := cmd.Flags() + flags.SortFlags = false + flags.StringSliceVar(&ids, "resource-id", nil, "Required, Resource ID of bandwidth package to delete") + req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") + req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") + + return cmd +} diff --git a/cmd/completion.go b/cmd/completion.go index 619bd7a61d..969e070c82 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -41,7 +41,7 @@ func NewCmdCompletion() *cobra.Command { } else if strings.HasSuffix(shell, "zsh") { zshCompletion(cmd) } else { - fmt.Println("Unknow shell: %", shell) + fmt.Printf("So far, shell %s is not supported\n", shell) } } else { fmt.Println("Lookup shell failed") @@ -54,33 +54,18 @@ func NewCmdCompletion() *cobra.Command { func bashCompletion(cmd *cobra.Command) { platform := runtime.GOOS if platform == "darwin" { - fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bash_profile' -If the following scripts are included in '~/.bash_profile', please remove it. Those scripts used to auto complete words before ucloud cli v0.1.3" - -if [ -f $(brew --prefix)/etc/bash_completion ]; then - . $(brew --prefix)/etc/bash_completion -fi -source ~/.ucloud/ucloud.sh`) + fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bash_profile'`) } else if platform == "linux" { - fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bashrc' -If the following scripts are included in '~/.bashrc', please remove it. Those scripts used to auto complete words before ucloud cli v0.1.3" - -if [ -f /etc/bash_completion ]; then - . /etc/bash_completion -fi - -source ~/.ucloud/ucloud.sh`) + fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bashrc'`) } } func zshCompletion(cmd *cobra.Command) { fmt.Println(`Please append the following scripts to file '~/.zshrc'. + 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", base.ConfigPath) - fmt.Println("autoload -U +X compinit && compinit") +complete -F $(which ucloud) ucloud`) } func getBashVersion() (version string, err error) { diff --git a/cmd/eip.go b/cmd/eip.go index f92e402f15..07c73f84b7 100644 --- a/cmd/eip.go +++ b/cmd/eip.go @@ -251,8 +251,8 @@ func NewCmdEIPAllocate() *cobra.Command { 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") 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") - req.PayMode = cmd.Flags().String("charge-mode", "Bandwidth", "Optional. charge-mode is an enumeration value. 'Traffic','Bandwidth' or 'ShareBandwidth'") - req.ShareBandwidthId = cmd.Flags().String("share-bandwidth-id", "", "Optional. ShareBandwidthId, required only when charge-mode is 'ShareBandwidth'") + req.PayMode = cmd.Flags().String("traffic-mode", "Bandwidth", "Optional. traffic-mode is an enumeration value. 'Traffic','Bandwidth' or 'ShareBandwidth'") + req.ShareBandwidthId = cmd.Flags().String("share-bandwidth-id", "", "Optional. ShareBandwidthId, required only when traffic-mode is 'ShareBandwidth'") req.Quantity = cmd.Flags().Int("quantity", 1, "Optional. The duration of the instance. N years/months.") req.ChargeType = cmd.Flags().String("charge-type", "Month", "Optional. Enumeration value.'Year',pay yearly;'Month',pay monthly;'Dynamic', pay hourly(requires permission),'Trial', free trial(need permission)") req.Tag = cmd.Flags().String("group", "Default", "Optional. Group of your EIP.") @@ -261,7 +261,7 @@ func NewCmdEIPAllocate() *cobra.Command { count = cmd.Flags().Int("count", 1, "Optional. Count of EIP to allocate") cmd.Flags().SetFlagValues("line", "BGP", "International") - cmd.Flags().SetFlagValues("charge-mode", "Bandwidth", "Traffic", "ShareBandwidth") + cmd.Flags().SetFlagValues("traffic-mode", "Bandwidth", "Traffic", "ShareBandwidth") cmd.Flags().SetFlagValues("charge-type", "Month", "Year", "Dynamic", "Trial") cmd.MarkFlagRequired("line") cmd.MarkFlagRequired("bandwidth-mb") @@ -280,7 +280,9 @@ func NewCmdEIPBind() *cobra.Command { bindEIP(resourceID, resourceType, eipID, projectID, region) }, } - cmd.Flags().SortFlags = false + flags := cmd.Flags() + flags.SortFlags = false + eipID = cmd.Flags().String("eip-id", "", "Required. EIPId to bind") resourceID = cmd.Flags().String("resource-id", "", "Required. ResourceID , which is the UHostId of uhost") resourceType = cmd.Flags().String("resource-type", "uhost", "Requried. ResourceType, type of resource to bind with eip. 'uhost','vrouter','ulb','upm','hadoophost'.eg..") @@ -299,7 +301,7 @@ func NewCmdEIPBind() *cobra.Command { } func bindEIP(resourceID, resourceType, eipID, projectID, region *string) { - ip := net.ParseIP(*resourceID) + ip := net.ParseIP(*eipID) if ip != nil { eipID, err := getEIPIDbyIP(ip, *projectID, *region) if err != nil { @@ -311,8 +313,8 @@ func bindEIP(resourceID, resourceType, eipID, projectID, region *string) { req := base.BizClient.NewBindEIPRequest() req.ResourceId = resourceID req.ResourceType = resourceType - req.EIPId = eipID - req.ProjectId = projectID + req.EIPId = sdk.String(base.PickResourceID(*eipID)) + req.ProjectId = sdk.String(base.PickResourceID(*projectID)) req.Region = region _, err := base.BizClient.BindEIP(req) if err != nil { @@ -436,15 +438,15 @@ func NewCmdEIPModifyBandwidth() *cobra.Command { return cmd } -//NewCmdEIPSetChargeMode ucloud eip modify-charge-mode +//NewCmdEIPSetChargeMode ucloud eip modify-traffic-mode func NewCmdEIPSetChargeMode() *cobra.Command { ids := []string{} req := base.BizClient.NewSetEIPPayModeRequest() cmd := &cobra.Command{ - Use: "modify-charge-mode", + Use: "modify-traffic-mode", Short: "Modify charge mode of EIP instances", Long: "Modify charge mode of EIP instances", - Example: "ucloud eip modify-charge-mode --eip-id eip-xxx --charge-mode Traffic", + Example: "ucloud eip modify-traffic-mode --eip-id eip-xxx --traffic-mode Traffic", Run: func(cmd *cobra.Command, args []string) { for _, id := range ids { id = base.PickResourceID(id) @@ -467,15 +469,15 @@ func NewCmdEIPSetChargeMode() *cobra.Command { cmd.Flags().SortFlags = false cmd.Flags().StringSliceVarP(&ids, "eip-id", "", nil, "Required, Resource ID of EIPs to modify charge mode") - req.PayMode = cmd.Flags().String("charge-mode", "", "Required, Charge mode of eip, 'traffic' or 'bandwidth'") + req.PayMode = cmd.Flags().String("traffic-mode", "", "Required, Charge mode of eip, 'traffic' or 'bandwidth'") 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") - cmd.Flags().SetFlagValues("charge-mode", "Bandwidth", "Traffic") + cmd.Flags().SetFlagValues("traffic-mode", "Bandwidth", "Traffic") cmd.Flags().SetFlagValuesFunc("eip-id", func() []string { return getAllEip(*req.ProjectId, *req.Region, nil, nil) }) cmd.MarkFlagRequired("eip-id") - cmd.MarkFlagRequired("charge-mode") + cmd.MarkFlagRequired("traffic-mode") return cmd } @@ -569,12 +571,12 @@ func NewCmdEIPLeaveSharedBW() *cobra.Command { flags.SortFlags = false flags.StringSliceVar(&eipIDs, "eip-id", nil, "Required. Resource ID of EIPs to leave shared bandwidth") req.Bandwidth = flags.Int("bandwidth-mb", 1, "Required. Bandwidth of EIP after leaving shared bandwidth, ranging [1,300] for 'Traffic' charge mode, ranging [1,800] for 'Bandwidth' charge mode. Unit:Mb") - req.PayMode = flags.String("charge-mode", "Bandwidth", "Optional. Charge mode of the EIP after leaving shared bandwidth, 'Bandwidth' or 'Traffic'") + req.PayMode = flags.String("traffic-mode", "Bandwidth", "Optional. Charge mode of the EIP after leaving shared bandwidth, 'Bandwidth' or 'Traffic'") req.ShareBandwidthId = flags.String("shared-bw-id", "", "Optional. Resource ID of shared bandwidth instance, assign this flag to make the operation faster") req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") - flags.SetFlagValues("charge-mode", "Bandwidth", "Traffic") + flags.SetFlagValues("traffic-mode", "Bandwidth", "Traffic") flags.SetFlagValuesFunc("eip-id", func() []string { return getAllEip(*req.ProjectId, *req.Region, nil, []string{status.EIP_CHARGE_SHARE}) }) diff --git a/cmd/firewall.go b/cmd/firewall.go index 4767c6cea7..3a5359c0cf 100644 --- a/cmd/firewall.go +++ b/cmd/firewall.go @@ -173,6 +173,9 @@ func NewCmdFirewallCreate(out io.Writer) *cobra.Command { req.Tag = flags.String("group", "", "Optional. Group of the firewall to create") req.Remark = flags.String("remark", "", "Optional. Remark of the firewall to create") cmd.MarkFlagRequired("name") + flags.SetFlagValuesFunc("rules-file", func() []string { + return base.GetFileList("") + }) return cmd } @@ -241,6 +244,9 @@ func NewCmdFirewallAddRule(out io.Writer) *cobra.Command { flags.SetFlagValuesFunc("fw-id", func() []string { return getFirewallIDNames(*req.ProjectId, *req.Region) }) + flags.SetFlagValuesFunc("rules-file", func() []string { + return base.GetFileList("") + }) cmd.MarkFlagRequired("fw-id") return cmd diff --git a/cmd/project.go b/cmd/project.go index c89fdbbe3a..93f837bc3e 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -67,7 +67,7 @@ func NewCmdProjectCreate() *cobra.Command { if resp.RetCode != 0 { base.HandleBizError(resp) } else { - base.Cxt.Printf("Project:%q created successfully.\n", resp.ProjectId) + base.Cxt.Printf("Project:%q created\n", resp.ProjectId) } } }, @@ -94,7 +94,7 @@ func NewCmdProjectUpdate() *cobra.Command { if resp.RetCode != 0 { base.HandleBizError(resp) } else { - base.Cxt.Printf("Project:%s updated successfully.\n", *req.ProjectId) + base.Cxt.Printf("Project:%s updated\n", *req.ProjectId) } } }, @@ -122,7 +122,7 @@ func NewCmdProjectDelete() *cobra.Command { if resp.RetCode != 0 { base.HandleBizError(resp) } else { - base.Cxt.Printf("Project:%s deleted successfully.\n", *req.ProjectId) + base.Cxt.Printf("Project:%s deleted\n", *req.ProjectId) } } }, diff --git a/cmd/root.go b/cmd/root.go index 93cbff7ced..88bcd6a548 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,12 +78,9 @@ func NewCmdRoot() *cobra.Command { cmd.AddCommand(NewCmdVpc()) cmd.AddCommand(NewCmdFirewall()) cmd.AddCommand(NewCmdDisk()) - cmd.AddCommand(NewCmdBandwidthPkg()) - cmd.AddCommand(NewCmdSharedBW()) + cmd.AddCommand(NewCmdBandwidth()) cmd.AddCommand(NewCmdUDPN(out)) cmd.AddCommand(NewCmdULB()) - cmd.AddCommand(NewCmdULBVserver()) - cmd.AddCommand(NewCmdULBSSL()) return cmd } @@ -181,8 +178,6 @@ func initialize(cmd *cobra.Command) { if err == nil { base.Cxt.AppendInfo("userName", userInfo.UserEmail) base.Cxt.AppendInfo("companyName", userInfo.CompanyName) - } else { - base.Cxt.PrintErr(err) } if (cmd.Name() != "config" && cmd.Name() != "init" && cmd.Name() != "version") && (cmd.Parent() != nil && cmd.Parent().Name() != "config") { diff --git a/cmd/uhost.go b/cmd/uhost.go index e04360d40e..fbc1d54a8b 100644 --- a/cmd/uhost.go +++ b/cmd/uhost.go @@ -222,7 +222,7 @@ func NewCmdUHostCreate(out io.Writer) *cobra.Command { bindEipID = flags.String("bind-eip", "", "Optional. Resource ID or IP Address of eip that will be bound to the new created uhost") eipReq.OperatorName = flags.String("create-eip-line", "", "Optional. Required if you want to create new EIP. Line of the created eip to be bound with the new created uhost") eipReq.Bandwidth = cmd.Flags().Int("create-eip-bandwidth-mb", 0, "Optional. Required if you want to create new EIP. Bandwidth(Unit:Mbps).The range of value related to network charge mode. By traffic [1, 300]; by bandwidth [1,800] (Unit: Mbps); it could be 0 if the eip belong to the shared bandwidth") - eipReq.PayMode = cmd.Flags().String("create-eip-charge-mode", "Bandwidth", "Optional. 'Traffic','Bandwidth' or 'ShareBandwidth'") + eipReq.PayMode = cmd.Flags().String("create-eip-traffic-mode", "Bandwidth", "Optional. 'Traffic','Bandwidth' or 'ShareBandwidth'") eipReq.Name = flags.String("create-eip-name", "", "Optional. Name of created eip to bind with the uhost") eipReq.Remark = cmd.Flags().String("create-eip-remark", "", "Optional.Remark of your EIP.") @@ -253,7 +253,7 @@ func NewCmdUHostCreate(out io.Writer) *cobra.Command { flags.SetFlagValues("data-disk-type", "LOCAL_NORMAL", "CLOUD_NORMAL", "LOCAL_SSD", "CLOUD_SSD", "EXCLUSIVE_LOCAL_DISK") flags.SetFlagValues("data-disk-backup-type", "NONE", "DATAARK") flags.SetFlagValues("create-eip-line", "BGP", "International") - flags.SetFlagValues("create-eip-charge-mode", "Bandwidth", "Traffic", "ShareBandwidth") + flags.SetFlagValues("create-eip-traffic-mode", "Bandwidth", "Traffic", "ShareBandwidth") flags.SetFlagValuesFunc("image-id", func() []string { return getImageList([]string{status.IMAGE_AVAILABLE}, cli.IMAGE_BASE, *req.ProjectId, *req.Region, *req.Zone) @@ -332,10 +332,12 @@ func NewCmdUHostDelete(out io.Writer) *cobra.Command { } }, } - cmd.Flags().SortFlags = false + flags := cmd.Flags() + flags.SortFlags = false + uhostIDs = cmd.Flags().StringSlice("uhost-id", nil, "Requried. ResourceIDs(UhostIds) of the uhost instance") - 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") + bindRegion(req, flags) + bindProjectID(req, flags) req.Zone = cmd.Flags().String("zone", "", "Optional. availability zone") isDestory = cmd.Flags().Bool("destory", false, "Optional. false,the uhost instance will be thrown to UHost recycle if you have permission; true,the uhost instance will be deleted directly") req.ReleaseEIP = cmd.Flags().Bool("release-eip", false, "Optional. false,Unbind EIP only; true, Unbind EIP and release it") diff --git a/cmd/ulb.go b/cmd/ulb.go index 4bfd8a9f67..684ad18ece 100644 --- a/cmd/ulb.go +++ b/cmd/ulb.go @@ -42,6 +42,8 @@ func NewCmdULB() *cobra.Command { cmd.AddCommand(NewCmdULBCreate(out)) cmd.AddCommand(NewCmdULBUpdate(out)) cmd.AddCommand(NewCmdULBDelete(out)) + cmd.AddCommand(NewCmdULBVserver()) + cmd.AddCommand(NewCmdULBSSL()) return cmd } @@ -141,6 +143,8 @@ func NewCmdULBCreate(out io.Writer) *cobra.Command { fmt.Fprintln(out, "Error, flag mode should be 'outer' or 'inner'") return } + req.VPCId = sdk.String(base.PickResourceID(*req.VPCId)) + req.SubnetId = sdk.String(base.PickResourceID(*req.SubnetId)) resp, err := base.BizClient.CreateULB(req) if err != nil { base.HandleError(err) @@ -151,6 +155,9 @@ func NewCmdULBCreate(out io.Writer) *cobra.Command { return } bindEipID = sdk.String(base.PickResourceID(*bindEipID)) + if mode == "outer" { + return + } if *bindEipID != "" { bindEIP(sdk.String(resp.ULBId), sdk.String("ulb"), bindEipID, req.ProjectId, req.Region) } else if *eipReq.OperatorName != "" && *eipReq.Bandwidth != 0 { @@ -182,23 +189,31 @@ func NewCmdULBCreate(out io.Writer) *cobra.Command { flags.StringVar(&mode, "mode", "outer", "Required. Network mode of ULB instance, outer or inner.") bindRegion(req, flags) bindProjectID(req, flags) + req.VPCId = flags.String("vpc-id", "", "Optional. Resource ID of VPC which the ULB to create belong to. See 'ucloud vpc list'") + req.SubnetId = flags.String("subnet-id", "", "Optional. Resource ID of subnet. This flag will be discarded when you are creating an outter mode ULB. See 'ucloud subnet list'") req.ChargeType = flags.String("charge-type", "Month", "Optional.'Year',pay yearly;'Month',pay monthly;'Dynamic', pay hourly") req.Tag = flags.String("group", "Default", "Optional. Business group") req.Remark = flags.String("remark", "", "Optional. Remark of instance to create.") - bindEipID = flags.String("bind-eip", "", "Optional. Resource ID or IP Address of eip that will be bound to the new created ulb") - eipReq.OperatorName = flags.String("create-eip-line", "", "Optional. Required if you want to create new EIP. Line of created eip to bind with the new created ulb") + bindEipID = flags.String("bind-eip", "", "Optional. Resource ID or IP Address of eip that will be bound to the new created outer mode ulb") + eipReq.OperatorName = flags.String("create-eip-line", "", "Optional. Required if you want to create new EIP. Line of created eip to bind with the new created outer mode ulb") eipReq.Bandwidth = cmd.Flags().Int("create-eip-bandwidth-mb", 0, "Optional. Required if you want to create new EIP. Bandwidth(Unit:Mbps).The range of value related to network charge mode. By traffic [1, 300]; by bandwidth [1,800] (Unit: Mbps); it could be 0 if the eip belong to the shared bandwidth") - eipReq.PayMode = cmd.Flags().String("create-eip-charge-mode", "Bandwidth", "Optional. 'Traffic','Bandwidth' or 'ShareBandwidth'") - eipReq.Name = flags.String("create-eip-name", "", "Optional. Name of created eip to bind with the uhost") + eipReq.PayMode = cmd.Flags().String("create-eip-traffic-mode", "Bandwidth", "Optional. 'Traffic','Bandwidth' or 'ShareBandwidth'") + eipReq.Name = flags.String("create-eip-name", "", "Optional. Name of created eip to bind with the new created outer mode ulb") eipReq.Remark = cmd.Flags().String("create-eip-remark", "", "Optional. Remark of your EIP.") flags.SetFlagValues("mode", "outer", "inner") flags.SetFlagValues("charge-type", "Month", "Year", "Dynamic") flags.SetFlagValues("create-eip-line", "BGP", "International") - flags.SetFlagValues("create-eip-charge-mode", "Bandwidth", "Traffic", "ShareBandwidth") + flags.SetFlagValues("create-eip-traffic-mode", "Bandwidth", "Traffic", "ShareBandwidth") flags.SetFlagValuesFunc("bind-eip", func() []string { return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_FREE}, nil) }) + flags.SetFlagValuesFunc("vpc-id", func() []string { + return getAllVPCIdNames(*req.ProjectId, *req.Region) + }) + flags.SetFlagValuesFunc("subnet-id", func() []string { + return getAllSubnetIDNames(*req.VPCId, *req.ProjectId, *req.Region) + }) cmd.MarkFlagRequired("mode") cmd.MarkFlagRequired("name") @@ -338,7 +353,7 @@ func getAllULBIDNames(project, region string) []string { //NewCmdULBVserver ucloud ulb-vserver func NewCmdULBVserver() *cobra.Command { cmd := &cobra.Command{ - Use: "ulb-vserver", + Use: "vserver", Short: "List and manipulate ULB Vserver instances", Long: "List and manipulate ULB Vserver instances", } @@ -348,14 +363,8 @@ func NewCmdULBVserver() *cobra.Command { cmd.AddCommand(NewCmdULBVServerCreate(out)) cmd.AddCommand(NewCmdULBVServerUpdate(out)) cmd.AddCommand(NewCmdULBVServerDelete(out)) - cmd.AddCommand(NewCmdULBVServerListNode(out)) - cmd.AddCommand(NewCmdULBVServerAddNode(out)) - cmd.AddCommand(NewCmdULBVServerUpdateNode(out)) - cmd.AddCommand(NewCmdULBVServerDeleteNode(out)) - cmd.AddCommand(NewCmdULBVServerCreatePolicy(out)) - cmd.AddCommand(NewCmdULBVServerListPolicy(out)) - cmd.AddCommand(NewCmdULBVServerUpdatePolicy(out)) - cmd.AddCommand(NewCmdULBVServerDeletePolicy(out)) + cmd.AddCommand(NewCmdULBVServerNode()) + cmd.AddCommand(NewCmdULBVServerPolicy()) return cmd } @@ -524,8 +533,8 @@ func NewCmdULBVServerUpdate(out io.Writer) *cobra.Command { vserverIDs := []string{} cmd := &cobra.Command{ Use: "update", - Short: "Update attributes of VServer instance", - Long: "Update attributes of VServer instance", + Short: "Update attributes of VServer instances", + Long: "Update attributes of VServer instances", Run: func(c *cobra.Command, args []string) { if *req.VServerName == "" { req.VServerName = nil @@ -643,11 +652,26 @@ func NewCmdULBVServerDelete(out io.Writer) *cobra.Command { return cmd } +//NewCmdULBVServerNode ucloud ulb vserver node +func NewCmdULBVServerNode() *cobra.Command { + out := base.Cxt.GetWriter() + cmd := &cobra.Command{ + Use: "backend", + Short: "List and manipulate VServer backend nodes", + Long: "List and manipulate VServer backend nodes", + } + cmd.AddCommand(NewCmdULBVServerListNode(out)) + cmd.AddCommand(NewCmdULBVServerAddNode(out)) + cmd.AddCommand(NewCmdULBVServerUpdateNode(out)) + cmd.AddCommand(NewCmdULBVServerDeleteNode(out)) + return cmd +} + //ULBVServerNode 表格行 type ULBVServerNode struct { Name string ResourceID string - NodeID string + BackendID string PrivateIP string Port int HealthCheck string @@ -659,7 +683,7 @@ type ULBVServerNode struct { func NewCmdULBVServerListNode(out io.Writer) *cobra.Command { req := base.BizClient.NewDescribeVServerRequest() cmd := &cobra.Command{ - Use: "list-node", + Use: "list", Short: "List ULB VServer backend nodes", Long: "List ULB VServer backend nodes", Run: func(c *cobra.Command, args []string) { @@ -681,7 +705,7 @@ func NewCmdULBVServerListNode(out io.Writer) *cobra.Command { row := ULBVServerNode{} row.Name = node.ResourceName row.ResourceID = node.ResourceId - row.NodeID = node.BackendId + row.BackendID = node.BackendId row.PrivateIP = node.PrivateIP row.Weight = node.Weight row.Port = node.Port @@ -703,8 +727,8 @@ func NewCmdULBVServerListNode(out io.Writer) *cobra.Command { flags := cmd.Flags() flags.SortFlags = false - req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the nodes belong to") - req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer which the nodes belong to") + req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the backend nodes belong to") + req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer which the backend nodes belong to") bindRegion(req, flags) bindProjectID(req, flags) @@ -728,20 +752,20 @@ func NewCmdULBVServerAddNode(out io.Writer) *cobra.Command { var weight *int req := base.BizClient.NewAllocateBackendRequest() cmd := &cobra.Command{ - Use: "add-node", - Short: "Add nodes for ULB Vserver instance", - Long: "Add nodes for ULB Vserver instance", + Use: "add", + Short: "Add backend nodes for ULB Vserver instance", + Long: "Add backend nodes for ULB Vserver instance", Run: func(c *cobra.Command, args []string) { if *enable == "enable" { req.Enabled = sdk.Int(1) } else if *enable == "disable" { req.Enabled = sdk.Int(0) } else { - fmt.Fprintln(out, "Error, node-mode must be enable or disable") + fmt.Fprintln(out, "Error, backend-mode must be enable or disable") return } - if *weight < 1 || *weight > 100 { - fmt.Fprintln(out, "Error, weight must be between 1 and 100") + if *weight < 0 || *weight > 100 { + fmt.Fprintln(out, "Error, weight must be between 0 and 100") return } req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId)) @@ -752,24 +776,24 @@ func NewCmdULBVServerAddNode(out io.Writer) *cobra.Command { base.HandleError(err) return } - fmt.Fprintf(out, "node[%s] added, node-id:%s\n", *req.ResourceId, resp.BackendId) + fmt.Fprintf(out, "backend node[%s] added, backend-id:%s\n", *req.ResourceId, resp.BackendId) }, } flags := cmd.Flags() flags.SortFlags = false - req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the nodes belong to") - req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer which the nodes belong to") - req.ResourceId = flags.String("resource-id", "", "Required. Resource ID of the node to add") + req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the backend nodes belong to") + req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer which the backend nodes belong to") + req.ResourceId = flags.String("resource-id", "", "Required. Resource ID of the backend node to add") bindRegion(req, flags) bindProjectID(req, flags) - req.ResourceType = flags.String("resource-type", "UHost", "Optional. Resource type of the node to add. Accept values: UHost,UPM,UDHost,UDocker") - req.Port = flags.Int("port", 80, "Optional. Which port the server on the node listening on") - enable = flags.String("node-mode", "enable", "Optional. Enable node or not. Accept values: enable, disable") + req.ResourceType = flags.String("resource-type", "UHost", "Optional. Resource type of the backend node to add. Accept values: UHost,UPM,UDHost,UDocker") + req.Port = flags.Int("port", 80, "Optional. The port of your real server on the backend node listening on") + enable = flags.String("backend-mode", "enable", "Optional. Enable backend node or not. Accept values: enable, disable") weight = flags.Int("weight", 1, "Optional. effective for lb-method WeightRoundrobin. Rnage [0,100]") flags.SetFlagValues("resource-type", "Uhost", "UPM", "UDHost", "UDocker") - flags.SetFlagValues("node-mode", "enable", "disable") + flags.SetFlagValues("backend-mode", "enable", "disable") flags.SetFlagValuesFunc("ulb-id", func() []string { return getAllULBIDNames(*req.ProjectId, *req.Region) }) @@ -787,12 +811,13 @@ func NewCmdULBVServerAddNode(out io.Writer) *cobra.Command { //NewCmdULBVServerUpdateNode ucloud ulb-vserver update-node func NewCmdULBVServerUpdateNode(out io.Writer) *cobra.Command { var mode *string + var weight *int backendIDs := []string{} req := base.BizClient.NewUpdateBackendAttributeRequest() cmd := &cobra.Command{ - Use: "update-node", - Short: "Update attributes of ULB nodes", - Long: "Update attributes of ULB nodes", + Use: "update", + Short: "Update attributes of ULB backend nodes", + Long: "Update attributes of ULB backend nodes", Run: func(c *cobra.Command, args []string) { if *mode == "enable" { req.Enabled = sdk.Int(1) @@ -801,9 +826,16 @@ func NewCmdULBVServerUpdateNode(out io.Writer) *cobra.Command { } else if *mode == "" { req.Enabled = nil } else { - fmt.Fprintln(out, "Error, node-mode must be enable or disable") + fmt.Fprintln(out, "Error, backend-mode must be enable or disable") return } + if *weight != -1 && (*weight < 0 || *weight > 100) { + fmt.Fprintln(out, "Error, weight must be between 0 and 100") + return + } + if *weight != -1 { + req.Weight = weight + } if *req.Port == 0 { req.Port = nil @@ -817,31 +849,32 @@ func NewCmdULBVServerUpdateNode(out io.Writer) *cobra.Command { base.HandleError(err) continue } - fmt.Fprintf(out, "node[%s] updated\n", bid) + fmt.Fprintf(out, "backend node[%s] updated\n", bid) } }, } flags := cmd.Flags() flags.SortFlags = false - req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the nodes belong to") - flags.StringSliceVar(&backendIDs, "node-id", nil, "Required. BackendID of nodes to update") - req.Port = flags.Int("port", 0, "Optional. Port of nodes to update. Rnage [1,65535]") - mode = flags.String("node-mode", "", "Optional. Enable node or not. Accept values: enable, disable") + req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the backend nodes belong to") + flags.StringSliceVar(&backendIDs, "backend-id", nil, "Required. BackendID of backend nodes to update") + req.Port = flags.Int("port", 0, "Optional. Port of your real server listening on backend nodes to update. Rnage [1,65535]") + mode = flags.String("backend-mode", "", "Optional. Enable backend node or not. Accept values: enable, disable") + weight = flags.Int("weight", -1, "Optional. effective for lb-method WeightRoundrobin. Rnage [0,100], -1 meaning no update") bindRegion(req, flags) bindProjectID(req, flags) - flags.SetFlagValues("node-mode", "enable", "disable") + flags.SetFlagValues("backend-mode", "enable", "disable") flags.SetFlagValuesFunc("ulb-id", func() []string { return getAllULBIDNames(*req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("node-id", func() []string { + flags.SetFlagValuesFunc("backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, "", *req.ProjectId, *req.Region) }) cmd.MarkFlagRequired("ulb-id") - cmd.MarkFlagRequired("node-id") + cmd.MarkFlagRequired("backend-id") return cmd } @@ -851,9 +884,9 @@ func NewCmdULBVServerDeleteNode(out io.Writer) *cobra.Command { backendIDs := []string{} req := base.BizClient.NewReleaseBackendRequest() cmd := &cobra.Command{ - Use: "delete-node", - Short: "Delete ULB VServer nodes", - Long: "Delete ULB VServer nodes", + Use: "delete", + Short: "Delete ULB VServer backend nodes", + Long: "Delete ULB VServer backend nodes", Run: func(c *cobra.Command, args []string) { req.ProjectId = sdk.String(base.PickResourceID(*req.ProjectId)) req.ULBId = sdk.String(base.PickResourceID(*req.ULBId)) @@ -864,35 +897,50 @@ func NewCmdULBVServerDeleteNode(out io.Writer) *cobra.Command { base.HandleError(err) continue } - fmt.Fprintf(out, "node[%s] deleted\n", idname) + fmt.Fprintf(out, "backend node[%s] deleted\n", idname) } }, } flags := cmd.Flags() flags.SortFlags = false - req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the nodes belong to") - flags.StringSliceVar(&backendIDs, "node-id", nil, "Required. BackendID of nodes to update") + req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB which the backend nodes belong to") + flags.StringSliceVar(&backendIDs, "backend-id", nil, "Required. BackendID of backend nodes to update") bindRegion(req, flags) bindProjectID(req, flags) cmd.MarkFlagRequired("ulb-id") - cmd.MarkFlagRequired("node-id") + cmd.MarkFlagRequired("backend-id") flags.SetFlagValuesFunc("ulb-id", func() []string { return getAllULBIDNames(*req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("node-id", func() []string { + flags.SetFlagValuesFunc("backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, "", *req.ProjectId, *req.Region) }) return cmd } +//NewCmdULBVServerPolicy ucloud ulb vserver policy +func NewCmdULBVServerPolicy() *cobra.Command { + out := base.Cxt.GetWriter() + cmd := &cobra.Command{ + Use: "policy", + Short: "List and manipulate forward policy for VServer", + Long: "List and manipulate forward policy for VServer", + } + cmd.AddCommand(NewCmdULBVServerCreatePolicy(out)) + cmd.AddCommand(NewCmdULBVServerListPolicy(out)) + cmd.AddCommand(NewCmdULBVServerUpdatePolicy(out)) + cmd.AddCommand(NewCmdULBVServerDeletePolicy(out)) + return cmd +} + //NewCmdULBVServerCreatePolicy ucloud ulb-vserver create-policy func NewCmdULBVServerCreatePolicy(out io.Writer) *cobra.Command { backendIDs := []string{} req := base.BizClient.NewCreatePolicyRequest() cmd := &cobra.Command{ - Use: "add-policy", + Use: "add", Short: "Add content forward policy for VServer", Long: "Add content forward policy for VServer", Run: func(c *cobra.Command, args []string) { @@ -920,7 +968,7 @@ func NewCmdULBVServerCreatePolicy(out io.Writer) *cobra.Command { req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB") req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer") - flags.StringSliceVar(&backendIDs, "node-id", nil, "Required. NodeID of the VServer's nodes") + flags.StringSliceVar(&backendIDs, "backend-id", nil, "Required. BackendID of the VServer's backend nodes") req.Type = flags.String("forward-method", "", "Required. Forward method, accept values:Domain and Path; Both forwarding methods can be described by using regular expressions or wildcards") req.Match = flags.String("expression", "", "Required. Expression of domain or path, such as \"www.[123].demo.com\" or \"/path/img/*.jpg\"") bindRegion(req, flags) @@ -933,13 +981,13 @@ func NewCmdULBVServerCreatePolicy(out io.Writer) *cobra.Command { flags.SetFlagValuesFunc("vserver-id", func() []string { return getAllULBVServerIDNames(*req.ULBId, *req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("node-id", func() []string { + flags.SetFlagValuesFunc("backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, *req.VServerId, *req.ProjectId, *req.Region) }) cmd.MarkFlagRequired("ulb-id") cmd.MarkFlagRequired("vserver-id") - cmd.MarkFlagRequired("node-id") + cmd.MarkFlagRequired("backend-id") cmd.MarkFlagRequired("forward-method") cmd.MarkFlagRequired("expression") @@ -952,7 +1000,7 @@ type ULBVServerPolicy struct { Expression string PolicyID string PolicyType string - Nodes string + Backends string } //NewCmdULBVServerListPolicy ucloud ulb-vserver list-policy @@ -961,7 +1009,7 @@ func NewCmdULBVServerListPolicy(out io.Writer) *cobra.Command { region := base.ConfigIns.Region project := base.ConfigIns.ProjectID cmd := &cobra.Command{ - Use: "list-policy", + Use: "list", Short: "List content forward policies of the VServer instance", Long: "List content forward policies of the VServer instance", Run: func(c *cobra.Command, args []string) { @@ -985,7 +1033,7 @@ func NewCmdULBVServerListPolicy(out io.Writer) *cobra.Command { for _, b := range p.BackendSet { nodes = append(nodes, fmt.Sprintf("%s|%s:%d|%s", b.BackendId, b.PrivateIP, b.Port, b.ResourceName)) } - row.Nodes = strings.Join(nodes, ",") + row.Backends = strings.Join(nodes, ",") list = append(list, row) } base.PrintList(list, global.json) @@ -1020,7 +1068,7 @@ func NewCmdULBVServerUpdatePolicy(out io.Writer) *cobra.Command { removeBackendIDs := []string{} req := base.BizClient.NewUpdatePolicyRequest() cmd := &cobra.Command{ - Use: "update-policy", + Use: "update", Short: "Update content forward policies of ULB VServer", Long: "Update content forward policies ULB VServer", Run: func(c *cobra.Command, args []string) { @@ -1096,9 +1144,9 @@ func NewCmdULBVServerUpdatePolicy(out io.Writer) *cobra.Command { req.ULBId = flags.String("ulb-id", "", "Required. Resource ID of ULB") req.VServerId = flags.String("vserver-id", "", "Required. Resource ID of VServer") flags.StringSliceVar(&policyIDs, "policy-id", nil, "Required. PolicyID of policies to update") - flags.StringSliceVar(&backendIDs, "node-id", nil, "Optional. NodeID of nodes. If assign this flag, it will rewrite all nodes of the policy") - flags.StringSliceVar(&addBackendIDs, "add-node-id", nil, "Optional. NodeID of nodes. Add nodes to the policy") - flags.StringSliceVar(&removeBackendIDs, "remove-node-id", nil, "Optional. NodeID of nodes. Remove those nodes from the policy") + flags.StringSliceVar(&backendIDs, "backend-id", nil, "Optional. BackendID of backend nodes. If assign this flag, it will rewrite all backend nodes of the policy") + flags.StringSliceVar(&addBackendIDs, "add-backend-id", nil, "Optional. BackendID of backend nodes. Add backend nodes to the policy") + flags.StringSliceVar(&removeBackendIDs, "remove-backend-id", nil, "Optional. BackendID of backend nodes. Remove those backend nodes from the policy") req.Type = flags.String("forward-method", "", "Optional. Forward method of policy, accept values:Domain and Path") req.Match = flags.String("expression", "", "Optional. Expression of domain or path, such as \"www.[123].demo.com\" or \"/path/img/*.jpg\"") @@ -1114,13 +1162,13 @@ func NewCmdULBVServerUpdatePolicy(out io.Writer) *cobra.Command { flags.SetFlagValuesFunc("vserver-id", func() []string { return getAllULBVServerIDNames(*req.ULBId, *req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("node-id", func() []string { + flags.SetFlagValuesFunc("backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, *req.VServerId, *req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("add-node-id", func() []string { + flags.SetFlagValuesFunc("add-backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, *req.VServerId, *req.ProjectId, *req.Region) }) - flags.SetFlagValuesFunc("remove-node-id", func() []string { + flags.SetFlagValuesFunc("remove-backend-id", func() []string { return getAllULBVServerNodeIDNames(*req.ULBId, *req.VServerId, *req.ProjectId, *req.Region) }) @@ -1132,7 +1180,7 @@ func NewCmdULBVServerDeletePolicy(out io.Writer) *cobra.Command { policyIDs := []string{} req := base.BizClient.NewDeletePolicyRequest() cmd := &cobra.Command{ - Use: "delete-policy", + Use: "delete", Short: "Delete content forward policies of ULB VServer", Long: "Delete content forward policies of ULB VServer", Run: func(c *cobra.Command, args []string) { @@ -1164,7 +1212,7 @@ func NewCmdULBVServerDeletePolicy(out io.Writer) *cobra.Command { //NewCmdULBSSL ucloud ulb-ssl-certificate func NewCmdULBSSL() *cobra.Command { cmd := &cobra.Command{ - Use: "ulb-ssl-certificate", + Use: "ssl", Short: "List and manipulate SSL Certificates for ULB", Long: "List and manipulate SSL Certificates for ULB", } @@ -1370,6 +1418,18 @@ func NewCmdSSLAdd(out io.Writer) *cobra.Command { keyPath = flags.String("private-key-file", "", "Optional. Path of private key file, *.key. Required if all-in-one-file is omitted") caPath = flags.String("ca-certificate-file", "", "Optional. Path of CA certificate file, *.crt") cmd.MarkFlagRequired("name") + flags.SetFlagValuesFunc("all-in-one-file", func() []string { + return base.GetFileList("") + }) + flags.SetFlagValuesFunc("private-key-file", func() []string { + return base.GetFileList(".key") + }) + flags.SetFlagValuesFunc("ca-certificate-file", func() []string { + return base.GetFileList(".crt") + }) + flags.SetFlagValuesFunc("site-certificate-file", func() []string { + return base.GetFileList(".crt") + }) return cmd } diff --git a/cmd/unet.go b/cmd/unet.go index 1119577631..a132fa40c1 100644 --- a/cmd/unet.go +++ b/cmd/unet.go @@ -17,8 +17,6 @@ package cmd import ( "fmt" "io" - "strconv" - "time" "github.com/spf13/cobra" @@ -26,172 +24,8 @@ import ( sdk "github.com/ucloud/ucloud-sdk-go/ucloud" "github.com/ucloud/ucloud-cli/base" - "github.com/ucloud/ucloud-cli/model/status" ) -//NewCmdBandwidthPkg ucloud bw-pkg -func NewCmdBandwidthPkg() *cobra.Command { - cmd := &cobra.Command{ - Use: "bw-pkg", - Short: "List, create and delete bandwidth package", - Long: "List, create and delete bandwidth package", - } - cmd.AddCommand(NewCmdBandwidthPkgCreate()) - cmd.AddCommand(NewCmdBandwidthPkgList()) - cmd.AddCommand(NewCmdBandwidthPkgDelete()) - return cmd -} - -//NewCmdBandwidthPkgCreate ucloud bw-pkg create -func NewCmdBandwidthPkgCreate() *cobra.Command { - var start, end *string - timeLayout := "2006-01-02/15:04:05" - ids := []string{} - req := base.BizClient.NewCreateBandwidthPackageRequest() - loc, _ := time.LoadLocation("Local") - cmd := &cobra.Command{ - Use: "create", - Short: "Create bandwidth package", - Long: "Create bandwidth package", - Example: "ucloud bw-pkg create --eip-id eip-xxx --bandwidth-mb 20 --start-time 2018-12-15/09:20:00 --end-time 2018-12-16/09:20:00", - Run: func(c *cobra.Command, args []string) { - st, err := time.ParseInLocation(timeLayout, *start, loc) - if err != nil { - base.HandleError(err) - return - } - et, err := time.ParseInLocation(timeLayout, *end, loc) - if err != nil { - base.HandleError(err) - return - } - if st.Sub(time.Now()) < 0 { - base.Cxt.Println("start-time must be after the current time") - return - } - du := et.Unix() - st.Unix() - if du <= 0 { - base.Cxt.Println("end-time must be after the start-time") - return - } - req.EnableTime = sdk.Int(int(st.Unix())) - req.TimeRange = sdk.Int(int(du)) - - for _, id := range ids { - id = base.PickResourceID(id) - req.EIPId = &id - resp, err := base.BizClient.CreateBandwidthPackage(req) - if err != nil { - base.HandleError(err) - continue - } - base.Cxt.Printf("bandwidth package[%s] created for eip[%s]\n", resp.BandwidthPackageId, id) - } - }, - } - flags := cmd.Flags() - flags.SortFlags = false - req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") - req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") - flags.StringSliceVar(&ids, "eip-id", nil, "Resource ID of eip to be bound with created bandwidth package") - start = flags.String("start-time", "", "The time to enable bandwidth package. Local time, for example '2018-12-25/08:30:00'") - end = flags.String("end-time", "", "The time to disable bandwidth package. Local time, for example '2018-12-26/08:30:00'") - req.Bandwidth = flags.Int("bandwidth-mb", 0, "Optional, bandwidth of the bandwidth package to create, unit:'Mb'") - cmd.Flags().SetFlagValuesFunc("eip-id", func() []string { - return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, []string{status.EIP_CHARGE_BANDWIDTH}) - }) - cmd.MarkFlagRequired("eip-id") - cmd.MarkFlagRequired("start-time") - cmd.MarkFlagRequired("end-time") - cmd.MarkFlagRequired("bandwidth-mb") - return cmd -} - -//BandwidthPkgRow 表格行 -type BandwidthPkgRow struct { - ResourceID string - EIP string - Bandwidth string - StartTime string - EndTime string -} - -//NewCmdBandwidthPkgList ucloud bw-pkg list -func NewCmdBandwidthPkgList() *cobra.Command { - req := base.BizClient.NewDescribeBandwidthPackageRequest() - cmd := &cobra.Command{ - Use: "list", - Short: "List bandwidth packages", - Long: "List bandwidth packages", - Run: func(c *cobra.Command, args []string) { - resp, err := base.BizClient.DescribeBandwidthPackage(req) - if err != nil { - base.HandleError(err) - return - } - list := []BandwidthPkgRow{} - for _, bp := range resp.DataSets { - row := BandwidthPkgRow{ - ResourceID: bp.BandwidthPackageId, - Bandwidth: strconv.Itoa(bp.Bandwidth) + "MB", - StartTime: base.FormatDateTime(bp.EnableTime), - EndTime: base.FormatDateTime(bp.DisableTime), - } - eip := bp.EIPId - for _, addr := range bp.EIPAddr { - eip += "/" + addr.IP + "/" + addr.OperatorName - } - row.EIP = eip - list = append(list, row) - } - if global.json { - base.PrintJSON(list) - } else { - base.PrintTableS(list) - } - }, - } - flags := cmd.Flags() - flags.SortFlags = false - req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") - req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") - req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset") - req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit range [0,10000000]") - - return cmd -} - -//NewCmdBandwidthPkgDelete ucloud bw-pkg delete -func NewCmdBandwidthPkgDelete() *cobra.Command { - ids := []string{} - req := base.BizClient.NewDeleteBandwidthPackageRequest() - cmd := &cobra.Command{ - Use: "delete", - Short: "Delete bandwidth packages", - Long: "Delete bandwidth packages", - Example: "ucloud bw-pkg delete --resource-id bwpack-xxx", - Run: func(c *cobra.Command, args []string) { - for _, id := range ids { - id := base.PickResourceID(id) - req.BandwidthPackageId = &id - _, err := base.BizClient.DeleteBandwidthPackage(req) - if err != nil { - base.HandleError(err) - return - } - base.Cxt.Printf("bandwidth package[%s] deleted\n", id) - } - }, - } - flags := cmd.Flags() - flags.SortFlags = false - flags.StringSliceVar(&ids, "resource-id", nil, "Required, Resource ID of bandwidth package to delete") - req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'") - req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'") - - return cmd -} - //NewCmdUDPN ucloud udpn func NewCmdUDPN(out io.Writer) *cobra.Command { cmd := &cobra.Command{ diff --git a/vendor/github.com/spf13/cobra/command.go b/vendor/github.com/spf13/cobra/command.go index 45b64c4b45..76c3c87706 100644 --- a/vendor/github.com/spf13/cobra/command.go +++ b/vendor/github.com/spf13/cobra/command.go @@ -1011,11 +1011,12 @@ func (c *Command) complete() error { compCmd.InitDefaultHelpFlag() //complete flags, flag values or sub commands - if length := len(args); length > 0 && strings.HasPrefix(args[length-1], "-") { //需要被补全的单词开头是‘-’时,补全flag和value + //需要被补全的单词或前一个单词开头是‘-’时,补全flag和value + if length := len(args); (length >= 1 && strings.HasPrefix(args[length-1], "-")) || (length >= 2 && strings.HasPrefix(args[length-2], "-")) { completeFlagValues(compCmd, args, currentWord) } else if len(compCmd.Commands()) > 0 { //需要补全的单词开头没有‘-’,补全子命令 completeSubCommands(compCmd, currentWord) - } else { //需要补全的单词开头没有‘-’,前一个单词也不是命令,光标当前位置非空格,尝试补全参数值(value) + } else { //需要补全的单词开头没有‘-’,k前一个单词也不是命令,光标当前位置非空格,尝试补全参数值(value) completeFlagValues(compCmd, args, currentWord) } return nil @@ -1042,7 +1043,6 @@ func completeSubCommands(cmd *Command, word string) { func completeFlagValues(cmd *Command, args []string, word string) { cmd.mergePersistentFlags() - var _flag *flag.Flag var lastArg = args[len(args)-1] @@ -1055,83 +1055,110 @@ func completeFlagValues(cmd *Command, args []string, word string) { _flag = cmd.Flags().Lookup(arg[2:]) } - hasCompleted := false if _flag == nil { completedWords := completeFlags(cmd, word) if completedWords != nil { for _, word := range completedWords { fmt.Fprintln(cmd.OutOrStdout(), word) } - hasCompleted = true return } } values := cmd.Flags().GetFlagValues(_flag.Name) - for _, v := range values { - if strings.HasPrefix(v, word) && v != word { - fmt.Fprintln(cmd.OutOrStdout(), v) - hasCompleted = true + if values != nil { + for _, v := range values { + if strings.HasPrefix(v, word) && v != word { + fmt.Fprintln(cmd.OutOrStdout(), v) + } } + return } compLine := strings.Join(args, " ") if args[len(args)-1] == word { compLine = strings.Join(args[:len(args)-1], " ") } + cache, err := readCompCache() if err != nil || cache[compLine] == nil { fetchFn := cmd.Flags().GetFlagValuesFunc(_flag.Name) //async completing is costly, only execute once. if fetchFn != nil { - fetchChan := make(chan string, 0) + fetchChan := make(chan []string, 0) go func() { words := fetchFn() - for _, item := range words { - fetchChan <- item - } - close(fetchChan) cache = make(map[string][]string) cache[compLine] = words + fetchChan <- words + cache[compLine] = words saveCompCache(cache) + close(fetchChan) }() timeoutDur, _ := time.ParseDuration("3s") timeoutChan := time.After(timeoutDur) intervalChan := time.Tick(time.Second / 100) - loop: for range intervalChan { select { - case v, ok := <-fetchChan: - if ok == false { - break loop + case compValues, ok := <-fetchChan: + if !ok { + return } - if strings.HasPrefix(v, word) && v != word { - fmt.Fprintln(cmd.OutOrStdout(), v) - os.Stdout.Sync() - hasCompleted = true + if strings.Contains(word, ",") { + values := strings.Split(word, ",") + list := completeMultipleValues(values[:len(values)-1], compValues) + for _, s := range list { + fmt.Fprintln(cmd.OutOrStdout(), s) + } + } else { + for _, v := range compValues { + if strings.Contains(v, word) && v != word { + fmt.Fprintln(cmd.OutOrStdout(), v) + } + } } case <-timeoutChan: - break loop + return } } } } else { - words := cache[compLine] - for _, word := range words { - fmt.Fprintln(cmd.OutOrStdout(), word) + var list []string + compValues := cache[compLine] + if strings.Contains(word, ",") { + values := strings.Split(word, ",") + values = values[:len(values)-1] + list = completeMultipleValues(values, compValues) + } else { + list = compValues + } + for _, v := range list { + fmt.Fprintln(cmd.OutOrStdout(), v) } - hasCompleted = true + return } - if !hasCompleted { - completeWords := completeFlags(cmd, word) - for _, word := range completeWords { - fmt.Fprintln(cmd.OutOrStdout(), word) - } + completeWords := completeFlags(cmd, word) + for _, word := range completeWords { + fmt.Fprintln(cmd.OutOrStdout(), word) } +} +func completeMultipleValues(values, compValues []string) []string { + vMap := map[string]bool{} + for _, v := range values { + vMap[v] = true + } + flagValue := strings.Join(values, ",") + list := []string{} + for _, cv := range compValues { + if !vMap[cv] { + list = append(list, flagValue+","+cv) + } + } + return list } func readCompCache() (map[string][]string, error) { @@ -1141,7 +1168,7 @@ func readCompCache() (map[string][]string, error) { if err != nil { return nil, err } - if ct := fileInfo.ModTime(); time.Now().Sub(ct) <= 10*1e9 { + if ct := fileInfo.ModTime(); time.Now().Sub(ct) <= 30*1e9 { byts, err := ioutil.ReadFile(filePath) if err != nil { return nil, err diff --git a/vendor/github.com/ucloud/ucloud-sdk-go/services/ulb/update_backend_attribute.go b/vendor/github.com/ucloud/ucloud-sdk-go/services/ulb/update_backend_attribute.go index 29ffb8dc18..d143d0bb68 100644 --- a/vendor/github.com/ucloud/ucloud-sdk-go/services/ulb/update_backend_attribute.go +++ b/vendor/github.com/ucloud/ucloud-sdk-go/services/ulb/update_backend_attribute.go @@ -23,6 +23,7 @@ type UpdateBackendAttributeRequest struct { // 后端实例状态开关 Enabled *int `required:"false"` + Weight *int } // UpdateBackendAttributeResponse is response schema for UpdateBackendAttribute action