github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/ecs/v1/cloudservers/results_job.go (about) 1 package cloudservers 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/huaweicloud/golangsdk" 8 ) 9 10 type JobResponse struct { 11 JobID string `json:"job_id"` 12 } 13 14 type JobStatus struct { 15 Status string `json:"status"` 16 Entities JobEntity `json:"entities"` 17 JobID string `json:"job_id"` 18 JobType string `json:"job_type"` 19 BeginTime string `json:"begin_time"` 20 EndTime string `json:"end_time"` 21 ErrorCode string `json:"error_code"` 22 FailReason string `json:"fail_reason"` 23 } 24 25 type JobEntity struct { 26 // Specifies the number of subtasks. 27 // When no subtask exists, the value of this parameter is 0. 28 SubJobsTotal int `json:"sub_jobs_total"` 29 30 // Specifies the execution information of a subtask. 31 // When no subtask exists, the value of this parameter is left blank. 32 SubJobs []SubJob `json:"sub_jobs"` 33 } 34 35 type SubJob struct { 36 // Specifies the task ID. 37 Id string `json:"job_id"` 38 39 // Task type. 40 Type string `json:"job_type"` 41 42 //Specifies the task status. 43 // SUCCESS: indicates the task is successfully executed. 44 // RUNNING: indicates that the task is in progress. 45 // FAIL: indicates that the task failed. 46 // INIT: indicates that the task is being initialized. 47 Status string `json:"status"` 48 49 // Specifies the time when the task started. 50 BeginTime string `json:"begin_time"` 51 52 // Specifies the time when the task finished. 53 EndTime string `json:"end_time"` 54 55 // Specifies the returned error code when the task execution fails. 56 ErrorCode string `json:"error_code"` 57 58 // Specifies the cause of the task execution failure. 59 FailReason string `json:"fail_reason"` 60 61 // Specifies the object of the task. 62 Entities map[string]interface{} `json:"entities"` 63 } 64 65 type JobResult struct { 66 golangsdk.Result 67 } 68 69 func (r JobResult) ExtractJobResponse() (*JobResponse, error) { 70 job := new(JobResponse) 71 err := r.ExtractInto(job) 72 return job, err 73 } 74 75 func (r JobResult) ExtractJobStatus() (*JobStatus, error) { 76 job := new(JobStatus) 77 err := r.ExtractInto(job) 78 return job, err 79 } 80 81 func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error { 82 return golangsdk.WaitFor(secs, func() (bool, error) { 83 job := new(JobStatus) 84 _, err := client.Get(jobURL(client, jobID), &job, nil) 85 time.Sleep(5 * time.Second) 86 if err != nil { 87 return false, err 88 } 89 90 if job.Status == "SUCCESS" { 91 return true, nil 92 } 93 if job.Status == "FAIL" { 94 err = fmt.Errorf("Job failed with code %s: %s", job.ErrorCode, job.FailReason) 95 return false, err 96 } 97 98 return false, nil 99 }) 100 } 101 102 func GetJobEntity(client *golangsdk.ServiceClient, jobID string, label string) (interface{}, error) { 103 job := new(JobStatus) 104 _, err := client.Get(jobURL(client, jobID), &job, nil) 105 if err != nil { 106 return nil, err 107 } 108 109 if job.Status == "SUCCESS" { 110 if e, ok := job.Entities.SubJobs[0].Entities[label]; ok { 111 return e, nil 112 } 113 } 114 115 return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity") 116 }