@@ -326,7 +326,7 @@ func NewCmdUHostCreate() *cobra.Command {
326326 }
327327
328328 wg := & sync.WaitGroup {}
329- tokens := make (chan struct {}, 10 )
329+ tokens := make (chan struct {}, 20 )
330330 wg .Add (count )
331331 if count <= 5 {
332332 for i := 0 ; i < count ; i ++ {
@@ -582,7 +582,7 @@ func NewCmdUHostDelete(out io.Writer) *cobra.Command {
582582 _req .UHostId = sdk .String (id )
583583 reqs [idx ] = & _req
584584 }
585- coAction := newConcurrentAction (reqs , deleteUHost )
585+ coAction := newConcurrentAction (reqs , 50 , deleteUHost )
586586 coAction .Do ()
587587 },
588588 }
@@ -595,7 +595,7 @@ func NewCmdUHostDelete(out io.Writer) *cobra.Command {
595595 req .Zone = cmd .Flags ().String ("zone" , "" , "Optional. availability zone" )
596596 isDestroy = cmd .Flags ().Bool ("destroy" , false , "Optional. false,the uhost instance will be thrown to UHost recycle if you have permission; true,the uhost instance will be deleted directly" )
597597 req .ReleaseEIP = cmd .Flags ().Bool ("release-eip" , true , "Optional. false,Unbind EIP only; true, Unbind EIP and release it" )
598- req .ReleaseUDisk = cmd .Flags ().Bool ("delete-cloud-disk" , false , "Optional. false, detach cloud disk only; true, detach cloud disk and delete it" )
598+ req .ReleaseUDisk = cmd .Flags ().Bool ("delete-cloud-disk" , true , "Optional. false, detach cloud disk only; true, detach cloud disk and delete it" )
599599 yes = cmd .Flags ().BoolP ("yes" , "y" , false , "Optional. Do not prompt for confirmation." )
600600 cmd .Flags ().SetFlagValues ("destroy" , "true" , "false" )
601601 cmd .Flags ().SetFlagValues ("release-eip" , "true" , "false" )
@@ -679,19 +679,34 @@ func NewCmdUHostStop(out io.Writer) *cobra.Command {
679679 return cmd
680680}
681681
682- func stopUhostIns (req * uhost.StopUHostInstanceRequest , async bool , out io.Writer ) {
682+ func promptStopUhostIns (req * uhost.StopUHostInstanceRequest , yes , async bool , promptText string , out io.Writer ) bool {
683+ if ! yes {
684+ agreeClose , err := ux .Prompt (promptText )
685+ if err != nil {
686+ base .LogError (err .Error ())
687+ return false
688+ }
689+ if ! agreeClose {
690+ return false
691+ }
692+ }
693+ return stopUhostIns (req , false , out )
694+ }
695+
696+ func stopUhostIns (req * uhost.StopUHostInstanceRequest , async bool , out io.Writer ) bool {
683697 resp , err := base .BizClient .StopUHostInstance (req )
684698 if err != nil {
685699 base .HandleError (err )
686- } else {
687- text := fmt .Sprintf ("uhost[%v] is shutting down" , resp .UhostId )
688- if async {
689- fmt .Fprintln (out , text )
690- } else {
691- poller := base .NewPoller (describeUHostByID , out )
692- poller .Poll (resp .UhostId , * req .ProjectId , * req .Region , * req .Zone , text , []string {status .HOST_STOPPED , status .HOST_FAIL })
693- }
700+ return false
701+ }
702+
703+ text := fmt .Sprintf ("uhost [%v] is shutting down" , resp .UhostId )
704+ if async {
705+ fmt .Fprintln (out , text )
706+ return false
694707 }
708+ poller := base .NewPoller (describeUHostByID , out )
709+ return poller .Poll (resp .UhostId , * req .ProjectId , * req .Region , * req .Zone , text , []string {status .HOST_STOPPED , status .HOST_FAIL })
695710}
696711
697712//可并发调用版本
@@ -847,8 +862,41 @@ func NewCmdUHostPoweroff(out io.Writer) *cobra.Command {
847862 return cmd
848863}
849864
850- func resizeUhost (req * uhost.ResizeUHostInstanceRequest ) {
865+ func resizeAttachedDisk (out io.Writer , req * uhost.ResizeAttachedDiskRequest , host * uhost.UHostInstanceSet , yes , async bool , promptText string ) error {
866+ req .UHostId = & host .UHostId
867+ if host .State == status .HOST_RUNNING {
868+ err := tryStopUhost (req , host .UHostId , promptText , yes , async , out )
869+ if err != nil {
870+ return fmt .Errorf ("try to stop uhost error :%w" , err )
871+ }
872+ }
873+ req .DryRun = sdk .Bool (false )
874+ _ , err := base .BizClient .ResizeAttachedDisk (req )
875+ if err != nil {
876+ return err
877+ }
878+ text := fmt .Sprintf ("uhost [%s] disk [%s] resize" , host .UHostId , * req .DiskId )
879+ if async {
880+ fmt .Fprintln (out , text )
881+ } else {
882+ poller := base .NewPoller (describeUHostByID , out )
883+ poller .Poll (host .UHostId , * req .ProjectId , * req .Region , * req .Zone , text , []string {status .HOST_RUNNING , status .HOST_STOPPED , status .HOST_FAIL })
884+ }
885+ return nil
886+ }
851887
888+ func tryStopUhost (req * uhost.ResizeAttachedDiskRequest , uhostID , promptText string , yes , async bool , out io.Writer ) error {
889+ req .DryRun = sdk .Bool (true )
890+ resp , err := base .BizClient .ResizeAttachedDisk (req )
891+ if err != nil {
892+ return err
893+ }
894+ if resp .NeedRestart {
895+ stopReq := base .BizClient .NewStopUHostInstanceRequest ()
896+ stopReq .UHostId = & uhostID
897+ promptStopUhostIns (stopReq , yes , async , promptText , out )
898+ }
899+ return nil
852900}
853901
854902//NewCmdUHostResize ucloud uhost resize
@@ -881,34 +929,24 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
881929 return
882930 }
883931 inst := host .(* uhost.UHostInstanceSet )
884- if inst .State == "Running" {
885- if ! * yes {
886- confirmText := "Resize uhost must be done after the uhost is stopped. Do you want to stop this uhost?"
887- if len (* uhostIDs ) > 1 {
888- confirmText = "Resize uhost must be done after the uhost is stopped. Do you want to stop those uhosts?"
889- }
890- agreeClose , err := ux .Prompt (confirmText )
891- if err != nil {
892- base .Cxt .Println (err )
893- return
894- }
895- if ! agreeClose {
896- return
932+ stopReq := base .BizClient .NewStopUHostInstanceRequest ()
933+ stopReq .ProjectId = req .ProjectId
934+ stopReq .Region = req .Region
935+ stopReq .Zone = req .Zone
936+ stopReq .UHostId = & id
937+ confirmText := "Resize uhost must be done after the uhost is stopped. Do you want to stop this uhost?"
938+ if req .CPU != nil || req .Memory != nil || * req .NetCapValue != 0 {
939+ if inst .State == status .HOST_RUNNING {
940+ ret := promptStopUhostIns (stopReq , * yes , * async , confirmText , out )
941+ if ret {
942+ inst .State = status .HOST_STOPPED
897943 }
898944 }
899- _req := base .BizClient .NewStopUHostInstanceRequest ()
900- _req .ProjectId = req .ProjectId
901- _req .Region = req .Region
902- _req .Zone = req .Zone
903- _req .UHostId = & id
904- stopUhostIns (_req , false , out )
905- }
906- if req .CPU != nil || req .Memory != nil || * req .NetCapValue != 0 {
907945 resp , err := base .BizClient .ResizeUHostInstance (req )
908946 if err != nil {
909947 base .HandleError (err )
910948 } else {
911- text := fmt .Sprintf ("uhost [%v] cpu, memory resized " , resp .UhostId )
949+ text := fmt .Sprintf ("uhost [%v] cpu, memory resize " , resp .UhostId )
912950 if * async {
913951 fmt .Fprintln (out , text )
914952 } else {
@@ -937,7 +975,13 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
937975 _req .DiskSpace = & bootDiskSize
938976 _req .DiskId = & bootDisk .DiskId
939977 }
940- } else if dataDiskSize != 0 {
978+ err := resizeAttachedDisk (out , _req , inst , * yes , * async , confirmText )
979+ if err != nil {
980+ base .HandleError (err )
981+ }
982+ }
983+
984+ if dataDiskSize != 0 {
941985 var dataDisk uhost.UHostDiskSet
942986 if len (dataDisks ) > 1 {
943987 if dataDiskID == "" {
@@ -964,21 +1008,9 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
9641008 }
9651009 _req .DiskSpace = & dataDiskSize
9661010 _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 )
975- } else {
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 })
1011+ err := resizeAttachedDisk (out , _req , inst , * yes , * async , confirmText )
1012+ if err != nil {
1013+ base .HandleError (err )
9821014 }
9831015 }
9841016 }
@@ -1018,7 +1050,7 @@ func describeUHostByID(uhostID, projectID, region, zone string) (interface{}, er
10181050 return nil , err
10191051 }
10201052 if len (resp .UHostSet ) < 1 {
1021- return nil , nil
1053+ return nil , fmt . Errorf ( "uhost [%s] does not exist" , uhostID )
10221054 }
10231055
10241056 return & resp .UHostSet [0 ], nil
0 commit comments