-
Notifications
You must be signed in to change notification settings - Fork 907
/
response.go
111 lines (89 loc) · 2.35 KB
/
response.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
package operations
import (
"fmt"
"net/http"
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/version"
)
// Operation response.
type operationResponse struct {
op *Operation
}
// OperationResponse returns an operation response.
func OperationResponse(op *Operation) response.Response {
return &operationResponse{op}
}
func (r *operationResponse) Render(w http.ResponseWriter) error {
err := r.op.Start()
if err != nil {
return err
}
url, md, err := r.op.Render()
if err != nil {
return err
}
body := api.ResponseRaw{
Type: api.AsyncResponse,
Status: api.OperationCreated.String(),
StatusCode: int(api.OperationCreated),
Operation: url,
Metadata: md,
}
w.Header().Set("Location", url)
code := 202
w.WriteHeader(code)
var debugLogger logger.Logger
if debug {
debugLogger = logger.AddContext(logger.Ctx{"http_code": code})
}
return util.WriteJSON(w, body, debugLogger)
}
func (r *operationResponse) String() string {
_, md, err := r.op.Render()
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return md.ID
}
// Forwarded operation response.
//
// Returned when the operation has been created on another node.
type forwardedOperationResponse struct {
op *api.Operation
project string
}
// ForwardedOperationResponse creates a response that forwards the metadata of
// an operation created on another node.
func ForwardedOperationResponse(project string, op *api.Operation) response.Response {
return &forwardedOperationResponse{
op: op,
project: project,
}
}
func (r *forwardedOperationResponse) Render(w http.ResponseWriter) error {
url := fmt.Sprintf("/%s/operations/%s", version.APIVersion, r.op.ID)
if r.project != "" {
url += fmt.Sprintf("?project=%s", r.project)
}
body := api.ResponseRaw{
Type: api.AsyncResponse,
Status: api.OperationCreated.String(),
StatusCode: int(api.OperationCreated),
Operation: url,
Metadata: r.op,
}
w.Header().Set("Location", url)
code := 202
w.WriteHeader(code)
var debugLogger logger.Logger
if debug {
debugLogger = logger.AddContext(logger.Ctx{"http_code": code})
}
return util.WriteJSON(w, body, debugLogger)
}
func (r *forwardedOperationResponse) String() string {
return r.op.ID
}