forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
149 lines (114 loc) · 3.09 KB
/
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
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
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/response"
"github.com/lxc/lxd/lxd/util"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
)
var operationCmd = APIEndpoint{
Path: "operations/{id}",
Delete: APIEndpointAction{Handler: operationDelete},
Get: APIEndpointAction{Handler: operationGet},
}
var operationsCmd = APIEndpoint{
Path: "operations",
Get: APIEndpointAction{Handler: operationsGet},
}
var operationWebsocket = APIEndpoint{
Path: "operations/{id}/websocket",
Get: APIEndpointAction{Handler: operationWebsocketGet},
}
func operationDelete(d *Daemon, r *http.Request) response.Response {
id := mux.Vars(r)["id"]
// First check if the query is for a local operation from this node
op, err := operations.OperationGetInternal(id)
if err != nil {
return response.SmartError(err)
}
_, err = op.Cancel()
if err != nil {
return response.BadRequest(err)
}
return response.EmptySyncResponse
}
func operationGet(d *Daemon, r *http.Request) response.Response {
id := mux.Vars(r)["id"]
var body *api.Operation
// First check if the query is for a local operation from this node
op, err := operations.OperationGetInternal(id)
if err != nil {
return response.SmartError(err)
}
_, body, err = op.Render()
if err != nil {
log.Println(errors.Wrap(err, "Failed to handle operations request"))
}
return response.SyncResponse(true, body)
}
func operationsGet(d *Daemon, r *http.Request) response.Response {
recursion := util.IsRecursionRequest(r)
localOperationURLs := func() (shared.Jmap, error) {
// Get all the operations
ops := operations.Clone()
// Build a list of URLs
body := shared.Jmap{}
for _, v := range ops {
status := strings.ToLower(v.Status().String())
_, ok := body[status]
if !ok {
body[status] = make([]string, 0)
}
body[status] = append(body[status].([]string), v.URL())
}
return body, nil
}
localOperations := func() (shared.Jmap, error) {
// Get all the operations
ops := operations.Clone()
// Build a list of operations
body := shared.Jmap{}
for _, v := range ops {
status := strings.ToLower(v.Status().String())
_, ok := body[status]
if !ok {
body[status] = make([]*api.Operation, 0)
}
_, op, err := v.Render()
if err != nil {
return nil, err
}
body[status] = append(body[status].([]*api.Operation), op)
}
return body, nil
}
// Start with local operations
var md shared.Jmap
var err error
if recursion {
md, err = localOperations()
if err != nil {
return response.InternalError(err)
}
} else {
md, err = localOperationURLs()
if err != nil {
return response.InternalError(err)
}
}
return response.SyncResponse(true, md)
}
func operationWebsocketGet(d *Daemon, r *http.Request) response.Response {
id := mux.Vars(r)["id"]
// First check if the query is for a local operation from this node
op, err := operations.OperationGetInternal(id)
if err != nil {
return response.SmartError(err)
}
return operations.OperationWebSocket(r, op)
}