-
Notifications
You must be signed in to change notification settings - Fork 14
/
async_jobs.go
138 lines (120 loc) · 4.58 KB
/
async_jobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package egoscale
import (
"encoding/json"
"errors"
)
// AsyncJobResult represents an asynchronous job result
type AsyncJobResult struct {
AccountID *UUID `json:"accountid,omitempty" doc:"the account that executed the async command"`
Cmd string `json:"cmd,omitempty" doc:"the async command executed"`
Created string `json:"created,omitempty" doc:"the created date of the job"`
JobID *UUID `json:"jobid" doc:"extra field for the initial async call"`
JobInstanceID *UUID `json:"jobinstanceid,omitempty" doc:"the unique ID of the instance/entity object related to the job"`
JobInstanceType string `json:"jobinstancetype,omitempty" doc:"the instance/entity object related to the job"`
JobProcStatus int `json:"jobprocstatus,omitempty" doc:"the progress information of the PENDING job"`
JobResult *json.RawMessage `json:"jobresult,omitempty" doc:"the result reason"`
JobResultCode int `json:"jobresultcode,omitempty" doc:"the result code for the job"`
JobResultType string `json:"jobresulttype,omitempty" doc:"the result type"`
JobStatus JobStatusType `json:"jobstatus,omitempty" doc:"the current job status-should be 0 for PENDING"`
UserID *UUID `json:"userid,omitempty" doc:"the user that executed the async command"`
}
// DeepCopy create a true copy of the receiver.
func (a *AsyncJobResult) DeepCopy() *AsyncJobResult {
if a == nil {
return nil
}
return &AsyncJobResult{
AccountID: a.AccountID.DeepCopy(),
Cmd: a.Cmd,
Created: a.Created,
JobID: a.JobID.DeepCopy(),
JobInstanceID: a.JobInstanceID.DeepCopy(),
JobInstanceType: a.JobInstanceType,
JobProcStatus: a.JobProcStatus,
JobResult: a.JobResult,
JobResultCode: a.JobResultCode,
JobResultType: a.JobResultType,
JobStatus: a.JobStatus,
UserID: a.UserID.DeepCopy(),
}
}
// DeepCopyInto copies the receiver into out.
//
// In (a) must be non nil. out must be non nil
func (a *AsyncJobResult) DeepCopyInto(out *AsyncJobResult) {
*out = AsyncJobResult{
AccountID: a.AccountID.DeepCopy(),
Cmd: a.Cmd,
Created: a.Created,
JobID: a.JobID.DeepCopy(),
JobInstanceID: a.JobInstanceID.DeepCopy(),
JobInstanceType: a.JobInstanceType,
JobProcStatus: a.JobProcStatus,
JobResult: a.JobResult,
JobResultCode: a.JobResultCode,
JobResultType: a.JobResultType,
JobStatus: a.JobStatus,
UserID: a.UserID.DeepCopy(),
}
}
// ListRequest buils the (empty) ListAsyncJobs request
func (a AsyncJobResult) ListRequest() (ListCommand, error) {
req := &ListAsyncJobs{
StartDate: a.Created,
}
return req, nil
}
// Error builds an error message from the result
func (a AsyncJobResult) Error() error {
r := new(ErrorResponse)
if e := json.Unmarshal(*a.JobResult, r); e != nil {
return e
}
return r
}
// QueryAsyncJobResult represents a query to fetch the status of async job
type QueryAsyncJobResult struct {
JobID *UUID `json:"jobid" doc:"the ID of the asynchronous job"`
_ bool `name:"queryAsyncJobResult" description:"Retrieves the current status of asynchronous job."`
}
// Response returns the struct to unmarshal
func (QueryAsyncJobResult) Response() interface{} {
return new(AsyncJobResult)
}
//go:generate go run generate/main.go -interface=Listable ListAsyncJobs
// ListAsyncJobs list the asynchronous jobs
type ListAsyncJobs struct {
Keyword string `json:"keyword,omitempty" doc:"List by keyword"`
Page int `json:"page,omitempty"`
PageSize int `json:"pagesize,omitempty"`
StartDate string `json:"startdate,omitempty" doc:"the start date of the async job"`
_ bool `name:"listAsyncJobs" description:"Lists all pending asynchronous jobs for the account."`
}
// ListAsyncJobsResponse represents a list of job results
type ListAsyncJobsResponse struct {
Count int `json:"count"`
AsyncJob []AsyncJobResult `json:"asyncjobs"`
}
// Result unmarshals the result of an AsyncJobResult into the given interface
func (a AsyncJobResult) Result(i interface{}) error {
if a.JobStatus == Failure {
return a.Error()
}
if a.JobStatus == Success {
m := map[string]json.RawMessage{}
err := json.Unmarshal(*(a.JobResult), &m)
if err == nil {
if len(m) >= 1 {
if _, ok := m["success"]; ok {
return json.Unmarshal(*(a.JobResult), i)
}
// otherwise, pick the first key
for k := range m {
return json.Unmarshal(m[k], i)
}
}
return errors.New("empty response")
}
}
return nil
}