forked from Savasw/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otc.go
80 lines (66 loc) · 1.72 KB
/
otc.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
package gophercloud
import (
"fmt"
"strings"
)
type JobResponse struct {
URI string `json:"uri"`
JobID string `json:"job_id"`
}
type JobStatus struct {
Status string `json:"status"`
Entities map[string]interface{} `json:"entities"`
JobID string `json:"job_id"`
JobType string `json:"job_type"`
ErrorCode string `json:"error_code"`
FailReason string `json:"fail_reason"`
}
func (r Result) ExtractJobResponse() (*JobResponse, error) {
job := new(JobResponse)
err := r.ExtractInto(job)
return job, err
}
func (r Result) ExtractJobStatus() (*JobStatus, error) {
job := new(JobStatus)
err := r.ExtractInto(job)
return job, err
}
func GetJobEndpoint(endpoint string) string {
n := strings.Index(endpoint[8:len(endpoint)], "/")
if n == -1 {
return endpoint
}
return endpoint[0:n+8]
}
func WaitForJobSuccess(client *ServiceClient, uri string, secs int) error {
return WaitFor(secs, func() (bool, error) {
job := new(JobStatus)
_, err := client.Get(GetJobEndpoint(client.Endpoint) + uri, &job, nil)
if err != nil {
return false, err
}
fmt.Printf("JobStatus: %+v.\n", job)
if job.Status == "SUCCESS" {
return true, nil
}
if job.Status == "FAIL" {
err = fmt.Errorf("Job failed with code %s: %s.\n", job.ErrorCode, job.FailReason)
return false, err
}
return false, nil
})
}
func GetJobEntity(client *ServiceClient, uri string, label string) (interface{}, error) {
job := new(JobStatus)
_, err := client.Get(GetJobEndpoint(client.Endpoint) + uri, &job, nil)
if err != nil {
return nil, err
}
fmt.Printf("JobStatus: %+v.\n", job)
if job.Status == "SUCCESS" {
if e := job.Entities[label]; e != nil {
return e, nil
}
}
return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity.")
}