forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
273 lines (235 loc) · 6.87 KB
/
api.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"github.com/youtube/vitess/go/vt/schemamanager"
"github.com/youtube/vitess/go/vt/tabletmanager/tmclient"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"golang.org/x/net/context"
)
// This file implements a REST-style API for the vtctld web interface.
const (
apiPrefix = "/api/"
jsonContentType = "application/json; charset=utf-8"
)
func handleCollection(collection string, getFunc func(*http.Request) (interface{}, error)) {
http.HandleFunc(apiPrefix+collection+"/", func(w http.ResponseWriter, r *http.Request) {
// Get the requested object.
obj, err := getFunc(r)
if err != nil {
if err == topo.ErrNoNode {
http.NotFound(w, r)
return
}
httpErrorf(w, r, "can't get %v: %v", collection, err)
return
}
// JSON marshals a nil slice as "null", but we prefer "[]".
if val := reflect.ValueOf(obj); val.Kind() == reflect.Slice && val.IsNil() {
w.Header().Set("Content-Type", jsonContentType)
w.Write([]byte("[]"))
return
}
// JSON encode response.
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
httpErrorf(w, r, "json error: %v", err)
return
}
w.Header().Set("Content-Type", jsonContentType)
w.Write(data)
})
}
func getItemPath(url string) string {
// Strip API prefix.
if !strings.HasPrefix(url, apiPrefix) {
return ""
}
url = url[len(apiPrefix):]
// Strip collection name.
parts := strings.SplitN(url, "/", 2)
if len(parts) != 2 {
return ""
}
return parts[1]
}
func unmarshalRequest(r *http.Request, v interface{}) error {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
func initAPI(ctx context.Context, ts topo.Server, actions *ActionRepository) {
tabletHealthCache := newTabletHealthCache(ts)
// Cells
handleCollection("cells", func(r *http.Request) (interface{}, error) {
if getItemPath(r.URL.Path) != "" {
return nil, errors.New("cells can only be listed, not retrieved")
}
return ts.GetKnownCells(ctx)
})
// Keyspaces
handleCollection("keyspaces", func(r *http.Request) (interface{}, error) {
keyspace := getItemPath(r.URL.Path)
// List all keyspaces.
if keyspace == "" {
return ts.GetKeyspaces(ctx)
}
// Perform an action on a keyspace.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyKeyspaceAction(ctx, action, keyspace, r), nil
}
// Get the keyspace record.
return ts.GetKeyspace(ctx, keyspace)
})
// Shards
handleCollection("shards", func(r *http.Request) (interface{}, error) {
shardPath := getItemPath(r.URL.Path)
if !strings.Contains(shardPath, "/") {
return nil, fmt.Errorf("invalid shard path: %q", shardPath)
}
parts := strings.SplitN(shardPath, "/", 2)
keyspace := parts[0]
shard := parts[1]
// List the shards in a keyspace.
if shard == "" {
return ts.GetShardNames(ctx, keyspace)
}
// Perform an action on a shard.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyShardAction(ctx, action, keyspace, shard, r), nil
}
// Get the shard record.
return ts.GetShard(ctx, keyspace, shard)
})
// Tablets
handleCollection("tablets", func(r *http.Request) (interface{}, error) {
tabletPath := getItemPath(r.URL.Path)
// List tablets based on query params.
if tabletPath == "" {
if err := r.ParseForm(); err != nil {
return nil, err
}
shardRef := r.FormValue("shard")
cell := r.FormValue("cell")
if shardRef != "" {
// Look up by keyspace/shard, and optionally cell.
keyspace, shard, err := topoproto.ParseKeyspaceShard(shardRef)
if err != nil {
return nil, err
}
if cell != "" {
return ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, []string{cell})
}
return ts.FindAllTabletAliasesInShard(ctx, keyspace, shard)
}
// Get all tablets in a cell.
if cell == "" {
return nil, errors.New("cell param required")
}
return ts.GetTabletsByCell(ctx, cell)
}
// Get tablet health.
if parts := strings.Split(tabletPath, "/"); len(parts) == 2 && parts[1] == "health" {
tabletAlias, err := topoproto.ParseTabletAlias(parts[0])
if err != nil {
return nil, err
}
return tabletHealthCache.Get(ctx, tabletAlias)
}
tabletAlias, err := topoproto.ParseTabletAlias(tabletPath)
if err != nil {
return nil, err
}
// Perform an action on a tablet.
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
return nil, err
}
action := r.FormValue("action")
if action == "" {
return nil, errors.New("must specify action")
}
return actions.ApplyTabletAction(ctx, action, tabletAlias, r), nil
}
// Get the tablet record.
return ts.GetTablet(ctx, tabletAlias)
})
// EndPoints
handleCollection("endpoints", func(r *http.Request) (interface{}, error) {
// We expect cell/keyspace/shard/tabletType.
epPath := getItemPath(r.URL.Path)
parts := strings.Split(epPath, "/")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid cell/keyspace/shard/tabletType: %q", epPath)
}
if parts[3] == "" {
// tabletType is empty, so list the tablet types.
return ts.GetSrvTabletTypesPerShard(ctx, parts[0], parts[1], parts[2])
}
tabletType, err := topoproto.ParseTabletType(parts[3])
if err != nil {
return nil, fmt.Errorf("invalid tablet type %v: %v", parts[3], err)
}
// Get the endpoints object for a specific type.
ep, _, err := ts.GetEndPoints(ctx, parts[0], parts[1], parts[2], tabletType)
return ep, err
})
// Schema Change
http.HandleFunc(apiPrefix+"schema/apply", func(w http.ResponseWriter, r *http.Request) {
req := struct{ Keyspace, SQL string }{}
if err := unmarshalRequest(r, &req); err != nil {
httpErrorf(w, r, "can't unmarshal request: %v", err)
return
}
executor := schemamanager.NewTabletExecutor(
tmclient.NewTabletManagerClient(),
ts)
schemamanager.Run(ctx,
schemamanager.NewUIController(req.SQL, req.Keyspace, w), executor)
})
// VSchema
http.HandleFunc(apiPrefix+"vschema/", func(w http.ResponseWriter, r *http.Request) {
// Save VSchema
if r.Method == "POST" {
vschema, err := ioutil.ReadAll(r.Body)
if err != nil {
httpErrorf(w, r, "can't read request body: %v", err)
return
}
if err := ts.SaveVSchema(ctx, string(vschema)); err != nil {
httpErrorf(w, r, "can't save vschema: %v", err)
}
return
}
// Get VSchema
vschema, err := ts.GetVSchema(ctx)
if err != nil {
httpErrorf(w, r, "can't get vschema: %v", err)
return
}
w.Header().Set("Content-Type", jsonContentType)
w.Write([]byte(vschema))
})
}