github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/sdrs/v1/protectedinstances/results_job.go (about) 1 package protectedinstances 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/opentelekomcloud/gophertelekomcloud" 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 } 26 27 type JobEntity struct { 28 InstanceID string `json:"protected_instance_id"` 29 SubJobs []SubJob `json:"sub_jobs"` 30 } 31 32 type SubJob struct { 33 // Specifies the task ID. 34 ID string `json:"job_id"` 35 // Task type. 36 Type string `json:"job_type"` 37 // Specifies the task status. 38 Status string `json:"status"` 39 // Specifies the time when the task started. 40 BeginTime string `json:"begin_time"` 41 // Specifies the time when the task finished. 42 EndTime string `json:"end_time"` 43 // Specifies the returned error code when the task execution fails. 44 ErrorCode string `json:"error_code"` 45 // Specifies the cause of the task execution failure. 46 FailReason string `json:"fail_reason"` 47 // Specifies the object of the task. 48 Entities map[string]string `json:"entities"` 49 } 50 51 type JobResult struct { 52 golangsdk.Result 53 } 54 55 func (r JobResult) ExtractJobResponse() (*JobResponse, error) { 56 job := new(JobResponse) 57 err := r.ExtractInto(job) 58 return job, err 59 } 60 61 func (r JobResult) ExtractJobStatus() (*JobStatus, error) { 62 job := new(JobStatus) 63 err := r.ExtractInto(job) 64 return job, err 65 } 66 67 func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error { 68 return golangsdk.WaitFor(secs, func() (bool, error) { 69 job := new(JobStatus) 70 _, err := client.Get(client.ServiceURL("jobs", jobID), &job, nil) 71 if err != nil { 72 return false, err 73 } 74 time.Sleep(5 * time.Second) 75 76 if job.Status == "SUCCESS" { 77 return true, nil 78 } 79 if job.Status == "FAIL" { 80 err = fmt.Errorf("job failed with code %s: %s", job.ErrorCode, job.FailReason) 81 return false, err 82 } 83 84 return false, nil 85 }) 86 } 87 88 func GetJobEntity(client *golangsdk.ServiceClient, jobID string, label string) (interface{}, error) { 89 if label != "protected_instance_id" { 90 return nil, fmt.Errorf("unsupported label %s in GetJobEntity", label) 91 } 92 93 job := new(JobStatus) 94 _, err := client.Get(client.ServiceURL("jobs", jobID), &job, nil) 95 if err != nil { 96 return nil, err 97 } 98 99 if job.Status == "SUCCESS" { 100 if instanceID := job.Entities.InstanceID; instanceID != "" { 101 return instanceID, nil 102 } 103 } 104 105 return nil, fmt.Errorf("unexpected conversion error in GetJobEntity") 106 }