forked from opentelekomcloud/gophertelekomcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results_job.go
100 lines (83 loc) · 2.28 KB
/
results_job.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
package golangsdk
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"`
}
type RDSJobStatus struct {
Job Job `json:"job"`
}
type Job struct {
Id string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Created string `json:"created"`
Process string `json:"process"`
Instance RDSJobInstance `json:"instance"`
}
type RDSJobInstance struct {
Id string `json:"id"`
Name string `json:"name"`
}
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:], "/")
if n == -1 {
return endpoint
}
return endpoint[0 : n+8]
}
func WaitForJobSuccess(client *ServiceClient, uri string, secs int) error {
uri = strings.Replace(uri, "v1", "v1.0", 1)
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) {
uri = strings.Replace(uri, "v1", "v1.0", 1)
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")
}