forked from flynn/flynn
/
http.go
206 lines (176 loc) · 5.51 KB
/
http.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package volumeapi
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/flynn/flynn/host/volume"
"github.com/flynn/flynn/host/volume/manager"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/httphelper"
"github.com/flynn/flynn/pkg/random"
"github.com/julienschmidt/httprouter"
)
const snapshotContentType = "application/vnd.zfs.snapshot-stream"
type HTTPAPI struct {
cluster *cluster.Client
vman *volumemanager.Manager
}
func NewHTTPAPI(cluster *cluster.Client, vman *volumemanager.Manager) *HTTPAPI {
return &HTTPAPI{
cluster: cluster,
vman: vman,
}
}
func (api *HTTPAPI) RegisterRoutes(r *httprouter.Router) {
r.POST("/storage/providers", api.CreateProvider)
r.POST("/storage/providers/:provider_id/volumes", api.Create)
r.GET("/storage/volumes", api.List)
r.GET("/storage/volumes/:volume_id", api.Inspect)
r.DELETE("/storage/volumes/:volume_id", api.Destroy)
r.PUT("/storage/volumes/:volume_id/snapshot", api.Snapshot)
// takes host and volID parameters, triggers a send on the remote host and give it a list of snaps already here, and pipes it into recv
r.POST("/storage/volumes/:volume_id/pull_snapshot", api.Pull)
// responds with a snapshot stream binary. only works on snapshots, takes 'haves' parameters, usually called by a node that's servicing a 'pull_snapshot' request
r.GET("/storage/volumes/:volume_id/send", api.Send)
}
func (api *HTTPAPI) CreateProvider(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
pspec := &volume.ProviderSpec{}
if err := httphelper.DecodeJSON(r, &pspec); err != nil {
httphelper.Error(w, err)
return
}
if pspec.ID == "" {
pspec.ID = random.UUID()
}
if pspec.Kind == "" {
httphelper.ValidationError(w, "kind", "must not be blank")
return
}
var provider volume.Provider
provider, err := volumemanager.NewProvider(pspec)
if err == volume.UnknownProviderKind {
httphelper.ValidationError(w, "kind", fmt.Sprintf("%q is not known", pspec.Kind))
return
}
if err := api.vman.AddProvider(pspec.ID, provider); err != nil {
switch err {
case volumemanager.ProviderAlreadyExists:
httphelper.ObjectExistsError(w, fmt.Sprintf("provider %q already exists", pspec.ID))
return
default:
httphelper.Error(w, err)
return
}
}
httphelper.JSON(w, 200, pspec)
}
func (api *HTTPAPI) Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
providerID := ps.ByName("provider_id")
vol, err := api.vman.NewVolumeFromProvider(providerID)
if err == volumemanager.NoSuchProvider {
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume provider with id %q", providerID))
return
}
httphelper.JSON(w, 200, vol.Info())
}
func (api *HTTPAPI) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
vols := api.vman.Volumes()
volList := make([]*volume.Info, 0, len(vols))
for _, v := range vols {
volList = append(volList, v.Info())
}
httphelper.JSON(w, 200, volList)
}
func (api *HTTPAPI) Inspect(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
vol := api.vman.GetVolume(volumeID)
if vol == nil {
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
}
httphelper.JSON(w, 200, vol.Info())
}
func (api *HTTPAPI) Destroy(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
err := api.vman.DestroyVolume(volumeID)
if err != nil {
switch err {
case volumemanager.NoSuchVolume:
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
default:
httphelper.Error(w, err)
return
}
}
w.WriteHeader(200)
}
func (api *HTTPAPI) Snapshot(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
snap, err := api.vman.CreateSnapshot(volumeID)
if err != nil {
switch err {
case volumemanager.NoSuchVolume:
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
default:
httphelper.Error(w, err)
return
}
}
httphelper.JSON(w, 200, snap.Info())
}
func (api *HTTPAPI) Pull(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
pull := &volume.PullCoordinate{}
if err := httphelper.DecodeJSON(r, &pull); err != nil {
httphelper.Error(w, err)
return
}
hostClient, err := api.cluster.Host(pull.HostID)
if err != nil {
httphelper.Error(w, err)
return
}
haves, err := api.vman.ListHaves(volumeID)
if err != nil {
httphelper.Error(w, err)
return
}
reader, err := hostClient.SendSnapshot(pull.SnapshotID, haves)
if err != nil {
httphelper.Error(w, err)
return
}
snap, err := api.vman.ReceiveSnapshot(volumeID, reader)
if err != nil {
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 200, snap.Info())
}
func (api *HTTPAPI) Send(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
if !strings.Contains(r.Header.Get("Accept"), snapshotContentType) {
httphelper.ValidationError(w, "", fmt.Sprintf("must be prepared to accept a content type of %q", snapshotContentType))
return
}
w.Header().Set("Content-Type", snapshotContentType)
var haves []json.RawMessage
if err := httphelper.DecodeJSON(r, &haves); err != nil {
httphelper.Error(w, err)
return
}
err := api.vman.SendSnapshot(volumeID, haves, w)
if err != nil {
switch err {
case volumemanager.NoSuchVolume:
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
default:
httphelper.Error(w, err)
return
}
}
}