@@ -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
851855func 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