/
wait.go
142 lines (124 loc) · 3.36 KB
/
wait.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// This code is based on the following material: https://github.com/mindjiver/gopherstack (MIT License)
//
// KT Cloud SDK go
//
// by ETRI, 2021.07.
package ktcloudsdk
import (
"fmt"
"log"
"time"
)
// Blocks until the the asynchronous job has executed or has timed out.
// time.Duration unit => 1 nanosecond. timeOut * 1,000,000,000 => 1 second
func (c KtCloudClient) WaitForAsyncJob(jobId string, timeOut time.Duration) error {
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
log.Printf("Checking the async job status... (attempt: %d)", attempts)
response, err := c.QueryAsyncJobResult(jobId)
if err != nil {
result <- err
return
}
// Check status of the job
// 0 - Pending / In progress, Continue job
// 1 - Succeeded
// 2 - Failed
// 3 - Cancelled //Not supports on KT Cloud
status := response.Queryasyncjobresultresponse.JobStatus
log.Printf("The job status : %d", status)
switch status {
case 1:
result <- nil
return
case 2:
err := fmt.Errorf("WaitForAsyncJob() failed")
result <- err
return
// case 3:
// err := fmt.Errorf("WaitForAsyncJob was cancelled")
// result <- err
// return
}
// Wait 3 seconds between requests
time.Sleep(3 * time.Second)
// Verify whether we shouldn't exit or ...
select {
case <-done:
// Finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
log.Printf("Waiting for up to %f seconds for async job : %s", timeOut.Seconds(), jobId)
select {
case err := <-result:
return err
case <-time.After(timeOut):
err := fmt.Errorf("Timeout while waiting to for the async job to finish")
return err
}
}
// WaitForVirtualMachineState simply blocks until the virtual machine is in the specified state.
func (c KtCloudClient) WaitForVirtualMachineState(zoneId string, vmId string, wantedState string, timeOut time.Duration) error {
vmListReqInfo := ListVMReqInfo{
ZoneId: zoneId,
VMId: vmId,
}
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
log.Printf("Checking the virtual machine state... (attempt: %d)", attempts)
response, err := c.ListVirtualMachines(vmListReqInfo)
if err != nil {
result <- err
return
}
count := response.Listvirtualmachinesresponse.Count
if count != 1 {
result <- err
return
}
currentState := response.Listvirtualmachinesresponse.Virtualmachine[0].State
// Check what the real state will be.
log.Printf("Current state: %s", currentState)
log.Printf("Wanted state: %s", wantedState)
if currentState == wantedState {
result <- nil
return
}
// Wait 3 seconds in between
time.Sleep(3 * time.Second)
// Verify whether we shouldn't exit or ...
select {
case <-done:
// Finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
log.Printf("Waiting for up to %f seconds for Virtual Machine state to converge", timeOut.Seconds())
select {
case err := <-result:
return err
case <-time.After(timeOut):
err := fmt.Errorf("Timeout while waiting to for the Virtual Machine to converge")
return err
}
}