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