Skip to content

Commit 0ae5e08

Browse files
authored
Merge pull request #27 from gig-tech/iso
add stop/start vm
2 parents a57250c + 888c43e commit 0ae5e08

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

ovc/machines.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ type MachineService interface {
121121
Shutdown(int) error
122122
AddExternalIP(int, int) error
123123
DeleteExternalIP(int, int, string) error
124+
Stop(int, bool) error
125+
Start(int, int) error
124126
}
125127

126128
// MachineServiceOp handles communication with the machine related methods of the
@@ -306,6 +308,42 @@ func (s *MachineServiceOp) DeleteByID(machineID int) error {
306308
return err
307309
}
308310

311+
// Stop stops a machine
312+
func (s *MachineServiceOp) Stop(machineID int, force bool) error {
313+
machineMap := make(map[string]interface{})
314+
machineMap["machineId"] = machineID
315+
machineMap["stop"] = force
316+
machineJSON, err := json.Marshal(machineMap)
317+
if err != nil {
318+
return err
319+
}
320+
req, err := http.NewRequest("POST", s.client.ServerURL+"/cloudapi/machines/stop", bytes.NewBuffer(machineJSON))
321+
if err != nil {
322+
return err
323+
}
324+
_, err = s.client.Do(req)
325+
return err
326+
}
327+
328+
// Start starts a machine, boots from ISO if diskID is given
329+
func (s *MachineServiceOp) Start(machineID int, diskID int) error {
330+
machineMap := make(map[string]interface{})
331+
machineMap["machineId"] = machineID
332+
if diskID != 0 {
333+
machineMap["diskId"] = diskID
334+
}
335+
machineJSON, err := json.Marshal(machineMap)
336+
if err != nil {
337+
return err
338+
}
339+
req, err := http.NewRequest("POST", s.client.ServerURL+"/cloudapi/machines/start", bytes.NewBuffer(machineJSON))
340+
if err != nil {
341+
return err
342+
}
343+
_, err = s.client.Do(req)
344+
return err
345+
}
346+
309347
// Template creates an image of the existing machine by ID
310348
func (s *MachineServiceOp) Template(machineID int, templateName string) error {
311349
machineMap := make(map[string]interface{})

0 commit comments

Comments
 (0)