github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/evs/v3/volumes/results_job.go (about) 1 package volumes 2 3 import ( 4 "fmt" 5 "strings" 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 Message string `json:"message"` 24 Code string `json:"code"` 25 SubJobs []JobStatus `json:"sub_jobs"` 26 } 27 28 type JobEntity struct { 29 VolumeID string `json:"volume_id"` 30 } 31 32 type JobResult struct { 33 golangsdk.Result 34 } 35 36 func (r JobResult) ExtractJobResponse() (*JobResponse, error) { 37 job := new(JobResponse) 38 err := r.ExtractInto(job) 39 return job, err 40 } 41 42 func (r JobResult) ExtractJobStatus() (*JobStatus, error) { 43 job := new(JobStatus) 44 err := r.ExtractInto(job) 45 return job, err 46 } 47 48 func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error { 49 50 jobClient := *client 51 jobClient.Endpoint = strings.Replace(jobClient.Endpoint, "v3", "v1", 1) 52 jobClient.ResourceBase = jobClient.Endpoint 53 return golangsdk.WaitFor(secs, func() (bool, error) { 54 job := new(JobStatus) 55 _, err := jobClient.Get(jobClient.ServiceURL("jobs", jobID), &job, nil) 56 if err != nil { 57 return false, err 58 } 59 60 if job.Status == "SUCCESS" { 61 return true, nil 62 } 63 if job.Status == "FAIL" { 64 err = fmt.Errorf("Job failed with code %s: %s.\n", job.ErrorCode, job.FailReason) 65 return false, err 66 } 67 68 return false, nil 69 }) 70 } 71 72 func GetJobEntity(client *golangsdk.ServiceClient, jobId string, label string) (interface{}, error) { 73 74 if label != "volume_id" { 75 return nil, fmt.Errorf("Unsupported label %s in GetJobEntity.", label) 76 } 77 78 jobClient := *client 79 jobClient.Endpoint = strings.Replace(jobClient.Endpoint, "v3", "v1", 1) 80 jobClient.ResourceBase = jobClient.Endpoint 81 job := new(JobStatus) 82 _, err := jobClient.Get(jobClient.ServiceURL("jobs", jobId), &job, nil) 83 if err != nil { 84 return nil, err 85 } 86 87 if job.Status == "SUCCESS" { 88 if e := job.Entities.VolumeID; e != "" { 89 return e, nil 90 } 91 } 92 93 return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity.") 94 }