@@ -12,6 +12,7 @@ import (
1212)
1313
1414type VirtualMachine struct {
15+ service * Service
1516 restclient * rest.RESTClient
1617 Node string
1718 VM * api.VirtualMachine
@@ -52,11 +53,22 @@ func (s *Service) VirtualMachine(ctx context.Context, vmid int) (*VirtualMachine
5253 }
5354 return nil , err
5455 }
55- return & VirtualMachine {restclient : s .restclient , VM : vm , Node : node .Node }, nil
56+ return & VirtualMachine {service : s , restclient : s .restclient , VM : vm , Node : node .Node }, nil
5657 }
5758 return nil , rest .NotFoundErr
5859}
5960
61+ func (s * Service ) CreateVirtualMachine (ctx context.Context , node string , vmid int , option api.VirtualMachineCreateOptions ) (* VirtualMachine , error ) {
62+ taskid , err := s .restclient .CreateVirtualMachine (ctx , node , vmid , option )
63+ if err != nil {
64+ return nil , err
65+ }
66+ if err := s .EnsureTaskDone (ctx , node , * taskid ); err != nil {
67+ return nil , err
68+ }
69+ return s .VirtualMachine (ctx , vmid )
70+ }
71+
6072func (s * Service ) VirtualMachineFromUUID (ctx context.Context , uuid string ) (* VirtualMachine , error ) {
6173 nodes , err := s .Nodes (ctx )
6274 if err != nil {
@@ -72,19 +84,19 @@ func (s *Service) VirtualMachineFromUUID(ctx context.Context, uuid string) (*Vir
7284 if err != nil {
7385 return nil , err
7486 }
75- vmuuid , err := convertSMBiosToUUID (config .SMBios1 )
87+ vmuuid , err := ConvertSMBiosToUUID (config .SMBios1 )
7688 if err != nil {
7789 return nil , err
7890 }
7991 if vmuuid == uuid {
80- return & VirtualMachine {restclient : s .restclient , VM : vm , Node : node .Node , config : config }, nil
92+ return & VirtualMachine {service : s , restclient : s .restclient , VM : vm , Node : node .Node , config : config }, nil
8193 }
8294 }
8395 }
8496 return nil , rest .NotFoundErr
8597}
8698
87- func convertSMBiosToUUID (smbios string ) (string , error ) {
99+ func ConvertSMBiosToUUID (smbios string ) (string , error ) {
88100 re := regexp .MustCompile (fmt .Sprintf ("uuid=%s" , UUIDFormat ))
89101 match := re .FindString (smbios )
90102 if match == "" {
@@ -94,6 +106,15 @@ func convertSMBiosToUUID(smbios string) (string, error) {
94106 return strings .Split (match , "=" )[1 ], nil
95107}
96108
109+ func (c * VirtualMachine ) Delete (ctx context.Context ) error {
110+ path := fmt .Sprintf ("/nodes/%s/qemu/%d" , c .Node , c .VM .VMID )
111+ var upid string
112+ if err := c .restclient .Delete (ctx , path , nil , & upid ); err != nil {
113+ return err
114+ }
115+ return c .service .EnsureTaskDone (ctx , c .Node , upid )
116+ }
117+
97118func (c * VirtualMachine ) GetConfig (ctx context.Context ) (* api.VirtualMachineConfig , error ) {
98119 if c .config != nil {
99120 return c .config , nil
@@ -114,3 +135,46 @@ func (c *VirtualMachine) GetOSInfo(ctx context.Context) (*api.OSInfo, error) {
114135 }
115136 return osInfo , nil
116137}
138+
139+ // size : The new size. With the `+` sign the value is added to the actual size of the volume
140+ // and without it, the value is taken as an absolute one.
141+ // Shrinking disk size is not supported.
142+ // size format : \+?\d+(\.\d+)?[KMGT]?j
143+ func (c * VirtualMachine ) ResizeVolume (ctx context.Context , disk , size string ) error {
144+ path := fmt .Sprintf ("/nodes/%s/qemu/%d/resize" , c .Node , c .VM .VMID )
145+ request := make (map [string ]interface {})
146+ request ["disk" ] = disk
147+ request ["size" ] = size
148+ var v interface {}
149+ if err := c .restclient .Put (ctx , path , request , & v ); err != nil {
150+ return err
151+ }
152+ return nil
153+ }
154+
155+ func (c * VirtualMachine ) Start (ctx context.Context ) error {
156+ path := fmt .Sprintf ("/nodes/%s/qemu/%d/status/start" , c .Node , c .VM .VMID )
157+ var upid string
158+ if err := c .restclient .Post (ctx , path , nil , & upid ); err != nil {
159+ return err
160+ }
161+ return c .service .EnsureTaskDone (ctx , c .Node , upid )
162+ }
163+
164+ func (c * VirtualMachine ) Stop (ctx context.Context ) error {
165+ path := fmt .Sprintf ("/nodes/%s/qemu/%d/status/stop" , c .Node , c .VM .VMID )
166+ var upid string
167+ if err := c .restclient .Post (ctx , path , nil , & upid ); err != nil {
168+ return err
169+ }
170+ return c .service .EnsureTaskDone (ctx , c .Node , upid )
171+ }
172+
173+ func (c * VirtualMachine ) Resume (ctx context.Context ) error {
174+ path := fmt .Sprintf ("/nodes/%s/qemu/%d/status/resume" , c .Node , c .VM .VMID )
175+ var upid string
176+ if err := c .restclient .Post (ctx , path , nil , & upid ); err != nil {
177+ return err
178+ }
179+ return c .service .EnsureTaskDone (ctx , c .Node , upid )
180+ }
0 commit comments