github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/sdrs/v1/attachreplication/results_job.go (about)

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