-
Notifications
You must be signed in to change notification settings - Fork 7
/
results_job.go
93 lines (79 loc) · 2.32 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
package cloudimages
import (
"fmt"
"time"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
)
type JobResponse struct {
JobID string `json:"job_id"`
}
type JobStatus struct {
Status string `json:"status"`
Entities JobEntity `json:"entities"`
JobID string `json:"job_id"`
JobType string `json:"job_type"`
BeginTime string `json:"begin_time"`
EndTime string `json:"end_time"`
ErrorCode string `json:"error_code"`
FailReason string `json:"fail_reason"`
}
type JobEntity struct {
ImageID string `json:"image_id"`
DataImageID string `json:"__data_images"`
}
type JobResult struct {
golangsdk.Result
}
func (r JobResult) ExtractJobResponse() (*JobResponse, error) {
job := new(JobResponse)
err := r.ExtractInto(job)
return job, err
}
func (r JobResult) ExtractJobStatus() (*JobStatus, error) {
job := new(JobStatus)
err := r.ExtractInto(job)
return job, err
}
func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error {
jobClient := *client
// v1/{project_id}/jobs/{job_id}
jobClient.ResourceBase = jobClient.Endpoint + "v1/" + jobClient.ProjectID + "/"
return golangsdk.WaitFor(secs, func() (bool, error) {
job := new(JobStatus)
_, err := jobClient.Get(jobClient.ServiceURL("jobs", jobID), &job, nil)
if err != nil {
return false, err
}
time.Sleep(10 * time.Second)
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 *golangsdk.ServiceClient, jobId string, label string) (interface{}, error) {
if label != "image_id" && label != "__data_images" {
return nil, fmt.Errorf("Unsupported label %s in GetJobEntity.", label)
}
jobClient := *client
// v1/{project_id}/jobs/{job_id}
jobClient.ResourceBase = jobClient.Endpoint + "v1/" + jobClient.ProjectID + "/"
job := new(JobStatus)
_, err := jobClient.Get(jobClient.ServiceURL("jobs", jobId), &job, nil)
if err != nil {
return nil, err
}
if job.Status == "SUCCESS" {
if e := job.Entities.ImageID; e != "" {
return e, nil
}
if e := job.Entities.DataImageID; e != "" {
return e, nil
}
}
return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity.")
}