-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper.go
77 lines (65 loc) · 1.47 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ovirtsdk
import (
"fmt"
"time"
)
const DefaultInterval = 10 * time.Second
const DefaultVMTimeout = 120 * time.Second
// WaitForVM waits for VM to given status
func (c *Connection) WaitForVM(vmID string, status VmStatus, timeout time.Duration) error {
if timeout <= 0 {
timeout = DefaultVMTimeout
}
if vmID == "" {
return fmt.Errorf("the VM ID must not be empty")
}
vmService := c.SystemService().VmsService().VmService(vmID)
for {
resp, err := vmService.Get().Send()
if err != nil {
return err
}
if timeout <= 0 {
return fmt.Errorf("timeout for waiting for VM to %v", status)
}
vm, ok := resp.Vm()
if !ok {
continue
}
if vm.MustStatus() == status {
break
}
timeout = timeout - DefaultInterval
time.Sleep(DefaultInterval)
}
return nil
}
const DefaultDiskTimeout = 120 * time.Second
func (c *Connection) WaitForDisk(diskID string, status DiskStatus, timeout time.Duration) error {
if timeout <= 0 {
timeout = DefaultDiskTimeout
}
if diskID == "" {
return fmt.Errorf("the Disk ID must not be empty")
}
diskService := c.SystemService().DisksService().DiskService(diskID)
for {
resp, err := diskService.Get().Send()
if err != nil {
return err
}
if timeout <= 0 {
return fmt.Errorf("timeout for waiting for Disk to %v", status)
}
disk, ok := resp.Disk()
if !ok {
continue
}
if disk.MustStatus() == status {
break
}
timeout = timeout - DefaultInterval
time.Sleep(DefaultInterval)
}
return nil
}