forked from xenserver/go-xenserver-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
101 lines (92 loc) · 2.19 KB
/
task.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
package client
import (
"fmt"
"regexp"
"strings"
)
type Task XenAPIObject
type TaskStatusType int
const (
_ TaskStatusType = iota
Pending
Success
Failure
Cancelling
Cancelled
)
func (self *Task) GetStatus() (status TaskStatusType, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.get_status", self.Ref)
if err != nil {
return
}
rawStatus := result.Value.(string)
switch strings.ToLower(rawStatus) {
case "pending":
status = Pending
case "success":
status = Success
case "failure":
status = Failure
case "cancelling":
status = Cancelling
case "cancelled":
status = Cancelled
default:
panic(fmt.Sprintf("Task.get_status: Unknown status '%s'", rawStatus))
}
return
}
func (self *Task) GetProgress() (progress float64, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.get_progress", self.Ref)
if err != nil {
return
}
progress = result.Value.(float64)
return
}
func (self *Task) GetResult() (object *XenAPIObject, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.get_result", self.Ref)
if err != nil {
return
}
switch ref := result.Value.(type) {
case string:
// @fixme: xapi currently sends us an xmlrpc-encoded string via xmlrpc.
// This seems to be a bug in xapi. Remove this workaround when it's fixed
re := regexp.MustCompile("^<value><array><data><value>([^<]*)</value>.*</data></array></value>$")
match := re.FindStringSubmatch(ref)
if match == nil {
object = nil
} else {
object = &XenAPIObject{
Ref: match[1],
Client: self.Client,
}
}
case nil:
object = nil
default:
err = fmt.Errorf("task.get_result: unknown value type %T (expected string or nil)", ref)
}
return
}
func (self *Task) GetErrorInfo() (errorInfo []string, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.get_error_info", self.Ref)
if err != nil {
return
}
errorInfo = make([]string, 0)
for _, infoRaw := range result.Value.([]interface{}) {
errorInfo = append(errorInfo, fmt.Sprintf("%v", infoRaw))
}
return
}
func (self *Task) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.destroy", self.Ref)
return
}