github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/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  type RDSJobStatus struct {
    23  	Job Job `json:"job"`
    24  }
    25  
    26  type Job struct {
    27  	Id       string         `json:"id"`
    28  	Name     string         `json:"name"`
    29  	Status   string         `json:"status"`
    30  	Created  string         `json:"created"`
    31  	Process  string         `json:"process"`
    32  	Instance RDSJobInstance `json:"instance"`
    33  }
    34  
    35  type RDSJobInstance struct {
    36  	Id   string `json:"id"`
    37  	Name string `json:"name"`
    38  }
    39  
    40  func (r Result) ExtractJobResponse() (*JobResponse, error) {
    41  	job := new(JobResponse)
    42  	err := r.ExtractInto(job)
    43  	return job, err
    44  }
    45  
    46  func (r Result) ExtractJobStatus() (*JobStatus, error) {
    47  	job := new(JobStatus)
    48  	err := r.ExtractInto(job)
    49  	return job, err
    50  }
    51  
    52  func GetJobEndpoint(endpoint string) string {
    53  	n := strings.Index(endpoint[8:], "/")
    54  	if n == -1 {
    55  		return endpoint
    56  	}
    57  	return endpoint[0 : n+8]
    58  }
    59  
    60  func WaitForJobSuccess(client *ServiceClient, uri string, secs int) error {
    61  	uri = strings.Replace(uri, "v1", "v1.0", 1)
    62  
    63  	return WaitFor(secs, func() (bool, error) {
    64  		job := new(JobStatus)
    65  		_, err := client.Get(GetJobEndpoint(client.Endpoint)+uri, &job, nil)
    66  		if err != nil {
    67  			return false, err
    68  		}
    69  		fmt.Printf("JobStatus: %+v.\n", job)
    70  
    71  		if job.Status == "SUCCESS" {
    72  			return true, nil
    73  		}
    74  		if job.Status == "FAIL" {
    75  			err = fmt.Errorf("Job failed with code %s: %s.\n", job.ErrorCode, job.FailReason)
    76  			return false, err
    77  		}
    78  
    79  		return false, nil
    80  	})
    81  }
    82  
    83  func GetJobEntity(client *ServiceClient, uri string, label string) (interface{}, error) {
    84  	uri = strings.Replace(uri, "v1", "v1.0", 1)
    85  
    86  	job := new(JobStatus)
    87  	_, err := client.Get(GetJobEndpoint(client.Endpoint)+uri, &job, nil)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	fmt.Printf("JobStatus: %+v.\n", job)
    92  
    93  	if job.Status == "SUCCESS" {
    94  		if e := job.Entities[label]; e != nil {
    95  			return e, nil
    96  		}
    97  	}
    98  
    99  	return nil, fmt.Errorf("unexpected conversion error in GetJobEntity")
   100  }