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