github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/ims/v1/others/ShowJob.go (about) 1 package others 2 3 import ( 4 "fmt" 5 "net/http" 6 7 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" 9 ) 10 11 func ShowJob(client *golangsdk.ServiceClient, jobId string) (*JobResponse, error) { 12 // GET /v1/{project_id}/jobs/{job_id} 13 raw, err := client.Get(client.ServiceURL(client.ProjectID, "jobs", jobId), nil, nil) 14 if err != nil { 15 return nil, err 16 } 17 18 var res JobResponse 19 err = extract.Into(raw.Body, &res) 20 return &res, err 21 } 22 23 type JobResponse struct { 24 // Specifies the job status. The value can be: 25 // 26 // SUCCESS: The job is successfully executed. 27 // 28 // FAIL: The job failed to be executed. 29 // 30 // RUNNING: The job is in progress. 31 // 32 // INIT: The job is being initialized. 33 Status string `json:"status,omitempty"` 34 // Specifies the task ID. 35 JobId string `json:"job_id,omitempty"` 36 // Specifies the job type. 37 JobType string `json:"job_type,omitempty"` 38 // Specifies the start time of the job. The value is in UTC format. 39 BeginTime string `json:"begin_time,omitempty"` 40 // Specifies the end time of the job. The value is in UTC format. 41 EndTime string `json:"end_time,omitempty"` 42 // Specifies the error code. 43 ErrorCode string `json:"error_code,omitempty"` 44 // Specifies the cause of the failure. 45 FailReason string `json:"fail_reason,omitempty"` 46 // Specifies the custom attributes of the job. 47 // 48 // If the job status is normal, the image ID will be returned. If the status is abnormal, an error code and details will be returned. 49 Entities JobEntities `json:"entities,omitempty"` 50 } 51 52 type JobEntities struct { 53 // Specifies the image ID. 54 ImageId string `json:"image_id,omitempty"` 55 // Specifies the job name. 56 CurrentTask string `json:"current_task,omitempty"` 57 // Specifies the image name. 58 ImageName string `json:"image_name,omitempty"` 59 // Specifies the job progress. 60 ProcessPercent float64 `json:"process_percent,omitempty"` 61 // Specifies job execution results. 62 Results []JobEntitiesResult `json:"results,omitempty"` 63 // Specifies sub-job execution results. 64 SubJobsResult []SubJobResult `json:"sub_jobs_result,omitempty"` 65 // Specifies the sub-job IDs. 66 SubJobsList []string `json:"sub_jobs_list,omitempty"` 67 } 68 69 type JobEntitiesResult struct { 70 // Specifies the image ID. 71 ImageId string `json:"image_id,omitempty"` 72 // Specifies the project ID. 73 ProjectId string `json:"project_id,omitempty"` 74 // Specifies the job status. 75 Status string `json:"status,omitempty"` 76 } 77 78 type SubJobResult struct { 79 // Specifies the sub-job status. The value can be: 80 // 81 // SUCCESS: The sub-job is successfully executed. 82 // 83 // FAIL: The sub-job failed to be executed. 84 // 85 // RUNNING: The sub-job is in progress. 86 // 87 // INIT: The sub-job is being initialized. 88 Status string `json:"status,omitempty"` 89 // Specifies a sub-job ID. 90 JobId string `json:"job_id,omitempty"` 91 // Specifies the sub-job type. 92 JobType string `json:"job_type,omitempty"` 93 // Specifies the start time of the sub-job. The value is in UTC format. 94 BeginTime string `json:"begin_time,omitempty"` 95 // Specifies the end time of the sub-job. The value is in UTC format. 96 EndTime string `json:"end_time,omitempty"` 97 // Specifies the error code. 98 ErrorCode string `json:"error_code,omitempty"` 99 // Specifies the failure cause. 100 FailReason string `json:"fail_reason,omitempty"` 101 // Specifies the custom attributes of the sub-job. For details, see Table 5. 102 // 103 // If a sub-job is properly executed, an image ID is returned. 104 // 105 // If an exception occurs on the sub-job, an error code and associated information are returned. 106 Entities SubJobEntities `json:"entities,omitempty"` 107 } 108 109 type SubJobEntities struct { 110 // Specifies the image ID. 111 ImageId string `json:"image_id,omitempty"` 112 // Specifies the image name. 113 ImageName string `json:"image_name,omitempty"` 114 } 115 116 func ExtractJobId(err error, raw *http.Response) (*string, error) { 117 if err != nil { 118 return nil, err 119 } 120 121 var res struct { 122 // Specifies the asynchronous job ID. 123 JobId string `json:"job_id"` 124 } 125 err = extract.Into(raw.Body, &res) 126 return &res.JobId, err 127 } 128 129 func WaitForJob(c *golangsdk.ServiceClient, id string, secs int) error { 130 return golangsdk.WaitFor(secs, func() (bool, error) { 131 current, err := ShowJob(c, id) 132 if err != nil { 133 return false, err 134 } 135 136 if current.Status == "SUCCESS" { 137 return true, nil 138 } 139 140 if current.Status == "FAIL" { 141 return false, fmt.Errorf("job failed: %s", current.FailReason) 142 } 143 144 return false, nil 145 }) 146 }