github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/results_job.go (about) 1 package golangsdk 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type JobResponse struct { 9 URI string `json:"uri"` 10 JobID string `json:"job_id"` 11 } 12 13 type JobStatus struct { 14 Status string `json:"status"` 15 Entities map[string]interface{} `json:"entities"` 16 JobID string `json:"job_id"` 17 JobType string `json:"job_type"` 18 ErrorCode string `json:"error_code"` 19 FailReason string `json:"fail_reason"` 20 } 21 22 func (r Result) ExtractJobResponse() (*JobResponse, error) { 23 job := new(JobResponse) 24 err := r.ExtractInto(job) 25 return job, err 26 } 27 28 func (r Result) ExtractJobStatus() (*JobStatus, error) { 29 job := new(JobStatus) 30 err := r.ExtractInto(job) 31 return job, err 32 } 33 34 func GetJobEndpoint(endpoint string) string { 35 n := strings.Index(endpoint[8:], "/") 36 if n == -1 { 37 return endpoint 38 } 39 return endpoint[0 : n+8] 40 } 41 42 func WaitForJobSuccess(client *ServiceClient, uri string, secs int) error { 43 uri = strings.Replace(uri, "v1", "v1.0", 1) 44 45 return WaitFor(secs, func() (bool, error) { 46 job := new(JobStatus) 47 _, err := client.Get(GetJobEndpoint(client.Endpoint)+uri, &job, nil) 48 if err != nil { 49 return false, err 50 } 51 fmt.Printf("JobStatus: %+v.\n", job) 52 53 if job.Status == "SUCCESS" { 54 return true, nil 55 } 56 if job.Status == "FAIL" { 57 err = fmt.Errorf("Job failed with code %s: %s.\n", job.ErrorCode, job.FailReason) 58 return false, err 59 } 60 61 return false, nil 62 }) 63 } 64 65 func GetJobEntity(client *ServiceClient, uri string, label string) (interface{}, error) { 66 uri = strings.Replace(uri, "v1", "v1.0", 1) 67 68 job := new(JobStatus) 69 _, err := client.Get(GetJobEndpoint(client.Endpoint)+uri, &job, nil) 70 if err != nil { 71 return nil, err 72 } 73 fmt.Printf("JobStatus: %+v.\n", job) 74 75 if job.Status == "SUCCESS" { 76 if e := job.Entities[label]; e != nil { 77 return e, nil 78 } 79 } 80 81 return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity.") 82 }