-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
lxd_operations.go
112 lines (88 loc) · 3.02 KB
/
lxd_operations.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
package lxd
import (
"fmt"
"net/url"
"strings"
"github.com/gorilla/websocket"
"github.com/lxc/lxd/shared/api"
)
// GetOperationUUIDs returns a list of operation uuids
func (r *ProtocolLXD) GetOperationUUIDs() ([]string, error) {
urls := []string{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/operations", nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it
uuids := []string{}
for _, url := range urls {
fields := strings.Split(url, "/operations/")
uuids = append(uuids, fields[len(fields)-1])
}
return uuids, nil
}
// GetOperations returns a list of Operation struct
func (r *ProtocolLXD) GetOperations() ([]api.Operation, error) {
apiOperations := map[string][]api.Operation{}
// Fetch the raw value
_, err := r.queryStruct("GET", "/operations?recursion=1", nil, "", &apiOperations)
if err != nil {
return nil, err
}
// Turn it into just a list of operations
operations := []api.Operation{}
for _, v := range apiOperations {
for _, operation := range v {
operations = append(operations, operation)
}
}
return operations, nil
}
// GetOperation returns an Operation entry for the provided uuid
func (r *ProtocolLXD) GetOperation(uuid string) (*api.Operation, string, error) {
op := api.Operation{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/operations/%s", url.PathEscape(uuid)), nil, "", &op)
if err != nil {
return nil, "", err
}
return &op, etag, nil
}
// GetOperationWait returns an Operation entry for the provided uuid once it's complete or hits the timeout
func (r *ProtocolLXD) GetOperationWait(uuid string, timeout int) (*api.Operation, string, error) {
op := api.Operation{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/operations/%s/wait?timeout=%d", url.PathEscape(uuid), timeout), nil, "", &op)
if err != nil {
return nil, "", err
}
return &op, etag, nil
}
// GetOperationWaitSecret returns an Operation entry for the provided uuid and secret once it's complete or hits the timeout
func (r *ProtocolLXD) GetOperationWaitSecret(uuid string, secret string, timeout int) (*api.Operation, string, error) {
op := api.Operation{}
// Fetch the raw value
etag, err := r.queryStruct("GET", fmt.Sprintf("/operations/%s/wait?secret=%s&timeout=%d", url.PathEscape(uuid), url.PathEscape(secret), timeout), nil, "", &op)
if err != nil {
return nil, "", err
}
return &op, etag, nil
}
// GetOperationWebsocket returns a websocket connection for the provided operation
func (r *ProtocolLXD) GetOperationWebsocket(uuid string, secret string) (*websocket.Conn, error) {
path := fmt.Sprintf("/operations/%s/websocket", url.PathEscape(uuid))
if secret != "" {
path = fmt.Sprintf("%s?secret=%s", path, url.QueryEscape(secret))
}
return r.websocket(path)
}
// DeleteOperation deletes (cancels) a running operation
func (r *ProtocolLXD) DeleteOperation(uuid string) error {
// Send the request
_, _, err := r.query("DELETE", fmt.Sprintf("/operations/%s", url.PathEscape(uuid)), nil, "")
if err != nil {
return err
}
return nil
}