Skip to content

Commit 8d86a50

Browse files
authored
Merge pull request #46 from lixiaojun629/develop
fixbug: resize uhost with disk-size failed
2 parents 1e2b87a + c32f9f8 commit 8d86a50

6 files changed

Lines changed: 187 additions & 85 deletions

File tree

Makefile

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

33
.PHONY : install
44
install:

base/config.go

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

3838
//Version 版本号
39-
const Version = "0.1.27"
39+
const Version = "0.1.28"
4040

4141
//ConfigIns 配置实例, 程序加载时生成
4242
var ConfigIns = &AggConfig{

cmd/disk.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,6 @@ func NewCmdDiskExpand() *cobra.Command {
489489
Short: "Expand udisk size",
490490
Long: "Expand udisk size",
491491
Run: func(cmd *cobra.Command, args []string) {
492-
if *req.Size > 8000 || *req.Size < 1 {
493-
base.Cxt.Println("size-gb should be between 1 and 8000")
494-
return
495-
}
496492
for _, id := range *udiskIDs {
497493
id = base.PickResourceID(id)
498494
req.UDiskId = &id

cmd/doc_md.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright © 2018 NAME HERE tony.li@ucloud.cn
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
"github.com/spf13/cobra/doc"
24+
25+
"github.com/ucloud/ucloud-sdk-go/ucloud/log"
26+
27+
"github.com/ucloud/ucloud-cli/base"
28+
)
29+
30+
//NewCmdDoc ucloud doc
31+
func NewCmdDoc(out io.Writer) *cobra.Command {
32+
var dir, format string
33+
cmd := &cobra.Command{
34+
Use: "gendoc",
35+
Short: "Generate documents for all commands",
36+
Long: "Generate documents for all commands. Support markdown, rst and douku",
37+
Run: func(c *cobra.Command, args []string) {
38+
base.ConfigIns.Region = ""
39+
base.ConfigIns.ProjectID = ""
40+
base.ConfigIns.Zone = ""
41+
rootCmd := NewCmdRoot()
42+
addChildren(rootCmd)
43+
switch format {
44+
case "rst":
45+
emptyStr := func(s string) string { return "" }
46+
linkHandler := func(name, ref string) string {
47+
return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
48+
}
49+
err := doc.GenReSTTreeCustom(rootCmd, dir, emptyStr, linkHandler)
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
54+
case "markdown":
55+
err := doc.GenMarkdownTree(rootCmd, dir)
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
case "douku":
60+
prefix := "developer/cli/cmd/"
61+
err := doc.GenDoukuTree(rootCmd, dir, prefix)
62+
printCmdIndex(rootCmd, 0, "developer/cli/cmd")
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
default:
67+
fmt.Fprintf(out, "format %s is not supported\n", format)
68+
}
69+
},
70+
}
71+
72+
cmd.Flags().StringVar(&dir, "dir", "", "Required. The directory where documents of commands are stored")
73+
cmd.Flags().StringVar(&format, "format", "douku", "Required. Format of the doucments. Accept values: markdown, rst and douku")
74+
75+
cmd.Flags().SetFlagValues("format", "douku", "markdown", "rst")
76+
cmd.Flags().SetFlagValuesFunc("dir", func() []string {
77+
return base.GetFileList("")
78+
})
79+
80+
cmd.MarkFlagRequired("dir")
81+
82+
return cmd
83+
}
84+
85+
func printCmdIndex(curr *cobra.Command, indent int, prefix string) {
86+
if curr.Name() == "help" {
87+
return
88+
}
89+
fmt.Printf("%s* [%s](%s%s)\n", strings.Repeat(" ", indent), curr.Name(), prefix, "/"+curr.Name())
90+
for _, cmd := range curr.Commands() {
91+
printCmdIndex(cmd, indent+1, prefix+"/"+curr.Name())
92+
}
93+
}

cmd/root.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io"
2019
"os"
2120
"strconv"
2221

2322
"github.com/spf13/cobra"
24-
"github.com/spf13/cobra/doc"
2523

2624
"github.com/ucloud/ucloud-sdk-go/ucloud/log"
2725

@@ -68,59 +66,6 @@ func NewCmdRoot() *cobra.Command {
6866
return cmd
6967
}
7068

71-
//NewCmdDoc ucloud doc
72-
func NewCmdDoc(out io.Writer) *cobra.Command {
73-
var dir, format string
74-
cmd := &cobra.Command{
75-
Use: "gendoc",
76-
Short: "Generate documents for all commands",
77-
Long: "Generate documents for all commands. Support markdown, rst and douku",
78-
Run: func(c *cobra.Command, args []string) {
79-
base.ConfigIns.Region = ""
80-
base.ConfigIns.ProjectID = ""
81-
base.ConfigIns.Zone = ""
82-
rootCmd := NewCmdRoot()
83-
addChildren(rootCmd)
84-
switch format {
85-
case "rst":
86-
emptyStr := func(s string) string { return "" }
87-
linkHandler := func(name, ref string) string {
88-
return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
89-
}
90-
err := doc.GenReSTTreeCustom(rootCmd, dir, emptyStr, linkHandler)
91-
if err != nil {
92-
log.Fatal(err)
93-
}
94-
95-
case "markdown":
96-
err := doc.GenMarkdownTree(rootCmd, dir)
97-
if err != nil {
98-
log.Fatal(err)
99-
}
100-
case "douku":
101-
err := doc.GenDoukuTree(rootCmd, dir, "developer/cli/cmd/")
102-
if err != nil {
103-
log.Fatal(err)
104-
}
105-
default:
106-
fmt.Fprintf(out, "format %s is not supported\n", format)
107-
}
108-
},
109-
}
110-
111-
cmd.Flags().StringVar(&dir, "dir", "", "Required. The directory where documents of commands are stored")
112-
cmd.Flags().StringVar(&format, "format", "douku", "Required. Format of the doucments. Accept values: markdown, rst and douku")
113-
114-
cmd.Flags().SetFlagValues("format", "douku", "markdown", "rst")
115-
cmd.Flags().SetFlagValuesFunc("dir", func() []string {
116-
return base.GetFileList("")
117-
})
118-
119-
cmd.MarkFlagRequired("dir")
120-
121-
return cmd
122-
}
123-
12469
const helpTmpl = `Usage:{{if .Runnable}}
12570
12671
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} [command]{{end}}{{if gt (len .Aliases) 0}}

cmd/uhost.go

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,15 @@ func NewCmdUHostPoweroff(out io.Writer) *cobra.Command {
847847
return cmd
848848
}
849849

850+
func resizeUhost(req *uhost.ResizeUHostInstanceRequest) {
851+
852+
}
853+
850854
//NewCmdUHostResize ucloud uhost resize
851855
func NewCmdUHostResize(out io.Writer) *cobra.Command {
852856
var yes, async *bool
857+
var bootDiskSize, dataDiskSize int
858+
var dataDiskID string
853859
var uhostIDs *[]string
854860
req := base.BizClient.NewResizeUHostInstanceRequest()
855861
cmd := &cobra.Command{
@@ -866,12 +872,6 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
866872
} else {
867873
*req.Memory *= 1024
868874
}
869-
if *req.DiskSpace == 0 {
870-
req.DiskSpace = nil
871-
}
872-
if *req.BootDiskSpace == 0 {
873-
req.BootDiskSpace = nil
874-
}
875875
for _, id := range *uhostIDs {
876876
id = base.PickResourceID(id)
877877
req.UHostId = &id
@@ -883,17 +883,17 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
883883
inst := host.(*uhost.UHostInstanceSet)
884884
if inst.State == "Running" {
885885
if !*yes {
886-
confirmText := "Resize uhost must be after stop it. Do you want to stop this uhost?"
886+
confirmText := "Resize uhost must be done after the uhost is stopped. Do you want to stop this uhost?"
887887
if len(*uhostIDs) > 1 {
888-
confirmText = "Resize uhost must be after stop it. Do you want to stop those uhosts?"
888+
confirmText = "Resize uhost must be done after the uhost is stopped. Do you want to stop those uhosts?"
889889
}
890890
agreeClose, err := ux.Prompt(confirmText)
891891
if err != nil {
892892
base.Cxt.Println(err)
893893
return
894894
}
895895
if !agreeClose {
896-
continue
896+
return
897897
}
898898
}
899899
_req := base.BizClient.NewStopUHostInstanceRequest()
@@ -903,31 +903,99 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
903903
_req.UHostId = &id
904904
stopUhostIns(_req, false, out)
905905
}
906+
if req.CPU != nil || req.Memory != nil || *req.NetCapValue != 0 {
907+
resp, err := base.BizClient.ResizeUHostInstance(req)
908+
if err != nil {
909+
base.HandleError(err)
910+
} else {
911+
text := fmt.Sprintf("uhost [%v] cpu, memory resized", resp.UhostId)
912+
if *async {
913+
fmt.Fprintln(out, text)
914+
} else {
915+
poller := base.NewPoller(describeUHostByID, out)
916+
poller.Poll(resp.UhostId, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
917+
}
918+
}
919+
}
906920

907-
resp, err := base.BizClient.ResizeUHostInstance(req)
908-
if err != nil {
909-
base.HandleError(err)
910-
} else {
911-
text := fmt.Sprintf("UHost:[%v] resized", resp.UhostId)
912-
if *async {
913-
fmt.Fprintln(out, text)
921+
if dataDiskSize != 0 || bootDiskSize != 0 {
922+
_req := base.BizClient.NewResizeAttachedDiskRequest()
923+
var bootDisk uhost.UHostDiskSet
924+
var dataDisks = map[string]uhost.UHostDiskSet{}
925+
for _, disk := range inst.DiskSet {
926+
if disk.IsBoot == "True" {
927+
bootDisk = disk
928+
} else if disk.IsBoot == "False" {
929+
dataDisks[disk.DiskId] = disk
930+
}
931+
}
932+
if bootDiskSize != 0 {
933+
if bootDiskSize <= bootDisk.Size {
934+
base.LogError(fmt.Sprintf("Error, disk does not support shrinkage. current system-disk-size %dg", bootDisk.Size))
935+
continue
936+
} else {
937+
_req.DiskSpace = &bootDiskSize
938+
_req.DiskId = &bootDisk.DiskId
939+
}
940+
} else if dataDiskSize != 0 {
941+
var dataDisk uhost.UHostDiskSet
942+
if len(dataDisks) > 1 {
943+
if dataDiskID == "" {
944+
base.LogError(fmt.Sprintf("Error, the uhost %s have %d data disks. data-disk-id should be assigned", id, len(dataDisks)))
945+
continue
946+
}
947+
var ok bool
948+
dataDisk, ok = dataDisks[dataDiskID]
949+
if !ok {
950+
base.LogError(fmt.Sprintf("Error, the disk %s does not exist", dataDiskID))
951+
continue
952+
}
953+
} else if len(dataDisks) == 1 {
954+
for _, disk := range dataDisks {
955+
dataDisk = disk
956+
}
957+
} else if len(dataDisks) == 0 {
958+
base.LogError(fmt.Sprintf("Error, the uhost %s have no data disk. data-disk-id should be assigned", id))
959+
continue
960+
}
961+
if dataDiskSize <= dataDisk.Size {
962+
base.LogError(fmt.Sprintf("Error, disk does not support shrinkage. current data-disk-size %dg", dataDisk.Size))
963+
continue
964+
}
965+
_req.DiskSpace = &dataDiskSize
966+
_req.DiskId = &dataDisk.DiskId
967+
}
968+
_req.ProjectId = req.ProjectId
969+
_req.Region = req.Region
970+
_req.Zone = req.Zone
971+
_req.UHostId = &id
972+
_, err := base.BizClient.ResizeAttachedDisk(_req)
973+
if err != nil {
974+
base.HandleError(err)
914975
} else {
915-
poller := base.NewPoller(describeUHostByID, out)
916-
poller.Poll(resp.UhostId, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
976+
text := fmt.Sprintf("uhost [%v] disk resized", id)
977+
if *async {
978+
fmt.Fprintln(out, text)
979+
} else {
980+
poller := base.NewPoller(describeUHostByID, out)
981+
poller.Poll(id, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
982+
}
917983
}
918984
}
919985
}
920986
},
921987
}
922-
cmd.Flags().SortFlags = false
988+
flags := cmd.Flags()
989+
flags.SortFlags = false
923990
uhostIDs = cmd.Flags().StringSlice("uhost-id", nil, "Required. ResourceIDs(or UhostIDs) of the uhost instances")
924-
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
925-
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
926-
req.Zone = cmd.Flags().String("zone", "", "Optional. Assign availability zone")
991+
bindProjectID(req, flags)
992+
bindRegion(req, flags)
993+
bindZone(req, flags)
927994
req.CPU = cmd.Flags().Int("cpu", 0, "Optional. The number of virtual CPU cores. Series1 {1, 2, 4, 8, 12, 16, 24, 32}. Series2 {1,2,4,8,16}")
928995
req.Memory = cmd.Flags().Int("memory-gb", 0, "Optional. memory size. Unit: GB. Range: [1, 128], multiple of 2")
929-
req.DiskSpace = cmd.Flags().Int("data-disk-size-gb", 0, "Optional. Data disk size,unit GB. Range[10,1000], SSD disk range[100,500]. Step 10")
930-
req.BootDiskSpace = cmd.Flags().Int("system-disk-size-gb", 0, "Optional. System disk size, unit GB. Range[20,100]. Step 10. System disk does not support shrinkage")
996+
cmd.Flags().IntVar(&bootDiskSize, "system-disk-size-gb", 0, "Optional. System disk size, unit GB. Range[20,100]. Step 10. System disk does not support shrinkage")
997+
cmd.Flags().IntVar(&dataDiskSize, "data-disk-size-gb", 0, "Optional. Data disk size,unit GB. Step 10. disk does not support shrinkage")
998+
cmd.Flags().StringVar(&dataDiskID, "data-disk-id", "", "Optional. If the uhost specified has two or more data disks, this parameter should be assigned")
931999
req.NetCapValue = cmd.Flags().Int("net-cap", 0, "Optional. NIC scale. 1,upgrade; 2,downgrade; 0,unchanged")
9321000
yes = cmd.Flags().BoolP("yes", "y", false, "Optional. Do not prompt for confirmation.")
9331001
async = cmd.Flags().BoolP("async", "a", false, "Optional. Do not wait for the long-running operation to finish.")

0 commit comments

Comments
 (0)