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