/
rest_create_index.go
229 lines (205 loc) · 6.55 KB
/
rest_create_index.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
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language
// governing permissions and limitations under the License.
package rest
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/gorilla/mux"
"github.com/couchbaselabs/cbgt"
)
// CreateIndexHandler is a REST handler that processes an index
// creation request.
type CreateIndexHandler struct {
mgr *cbgt.Manager
}
func NewCreateIndexHandler(mgr *cbgt.Manager) *CreateIndexHandler {
return &CreateIndexHandler{mgr: mgr}
}
func (h *CreateIndexHandler) RESTOpts(opts map[string]string) {
indexTypes := []string(nil)
for indexType, t := range cbgt.PIndexImplTypes {
indexTypes = append(indexTypes,
"```"+indexType+"```: "+
strings.Split(t.Description, " - ")[1])
}
sort.Strings(indexTypes)
indexParams := []string(nil)
for _, indexDesc := range indexTypes {
indexType := strings.Split(indexDesc, "```")[1]
t := cbgt.PIndexImplTypes[indexType]
if t.StartSample != nil {
indexParams = append(indexParams,
"For indexType ```"+indexType+"```"+
", an example indexParams JSON:\n\n "+
cbgt.IndentJSON(t.StartSample, " ", " "))
} else {
indexParams = append(indexParams,
"For indexType ```"+indexType+"```"+
", the indexParams can be null.")
}
}
sourceTypes := []string(nil)
for sourceType, t := range cbgt.FeedTypes {
if t.Public {
sourceTypes = append(sourceTypes,
"```"+sourceType+"```: "+
strings.Split(t.Description, " - ")[1])
}
}
sort.Strings(sourceTypes)
sourceParams := []string(nil)
for _, sourceDesc := range sourceTypes {
sourceType := strings.Split(sourceDesc, "```")[1]
t := cbgt.FeedTypes[sourceType]
if t.StartSample != nil {
sourceParams = append(sourceParams,
"For sourceType ```"+sourceType+"```"+
", an example sourceParams JSON:\n\n "+
cbgt.IndentJSON(t.StartSample, " ", " "))
} else {
sourceParams = append(sourceParams,
"For sourceType ```"+sourceType+"```"+
", the sourceParams can be null.")
}
}
opts["param: indexName"] =
"required, string, URL path parameter\n\n" +
"The name of the to-be-created/updated index definition,\n" +
"validated with the regular expression of ```" +
cbgt.INDEX_NAME_REGEXP + "```."
opts["param: indexType"] =
"required, string, form parameter\n\n" +
"Supported indexType's:\n\n* " +
strings.Join(indexTypes, "\n* ")
opts["param: indexParams"] =
"optional (depends on the value of the indexType)," +
" string (JSON), form parameter\n\n" +
strings.Join(indexParams, "\n\n")
opts["param: sourceType"] =
"required, string, form parameter\n\n" +
"Supported sourceType's:\n\n* " +
strings.Join(sourceTypes, "\n* ")
opts["param: sourceName"] =
"optional, string, form parameter"
opts["param: sourceUUID"] =
"optional, string, form parameter"
opts["param: sourceParams"] =
"optional (depends on the value of the sourceType)," +
" string (JSON), form parameter\n\n" +
strings.Join(sourceParams, "\n\n")
opts["param: planParams"] =
"optional, string (JSON), form parameter"
opts["param: prevIndexUUID"] =
"optional, string, form parameter\n\n" +
"Intended for clients that want to check that they are not " +
"overwriting the index definition updates of concurrent clients."
opts["result on error"] =
`non-200 HTTP error code`
opts["result on success"] =
`HTTP 200 with body JSON of {"status": "ok"}` // TODO: Revisit 200 code.
}
func (h *CreateIndexHandler) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
// TODO: Need more input validation (check source UUID's, etc).
indexName := mux.Vars(req)["indexName"]
if indexName == "" {
ShowError(w, req, "rest_create_index: index name is required", 400)
return
}
requestBody, err := ioutil.ReadAll(req.Body)
if err != nil {
ShowError(w, req, fmt.Sprintf("rest_create_index:"+
" could not read request body, indexName: %s, err: %v",
indexName, err), 400)
return
}
var indexDef cbgt.IndexDef
if len(requestBody) > 0 {
err := json.Unmarshal(requestBody, &indexDef)
if err != nil {
ShowError(w, req, fmt.Sprintf("rest_create_index:"+
" could not unmarshal json, indexName: %s, err: %v",
indexName, err), 400)
return
}
}
indexType := req.FormValue("indexType")
if indexType == "" {
indexType = indexDef.Type
}
if indexType == "" {
ShowError(w, req, "rest_create_index: indexType is required", 400)
return
}
indexParams := req.FormValue("indexParams")
if indexParams == "" {
indexParams = indexDef.Params
}
sourceType := req.FormValue("sourceType")
if sourceType == "" {
sourceType = indexDef.SourceType
}
if sourceType == "" {
ShowError(w, req, "rest_create_index: sourceType is required", 400)
return
}
sourceName := req.FormValue("sourceName")
if sourceName == "" {
sourceName = indexDef.SourceName
}
if sourceName == "" {
// NOTE: Some sourceTypes (like "nil") don't care if sourceName is "".
if sourceType == "couchbase" {
// TODO: Revisit default sourceName as indexName.
sourceName = indexName
}
}
sourceUUID := req.FormValue("sourceUUID") // Defaults to "".
if sourceUUID == "" {
sourceUUID = indexDef.SourceUUID
}
sourceParams := req.FormValue("sourceParams") // Defaults to "".
if sourceParams == "" {
sourceParams = indexDef.SourceParams
}
planParams := &cbgt.PlanParams{}
planParamsStr := req.FormValue("planParams")
if planParamsStr != "" {
err := json.Unmarshal([]byte(planParamsStr), planParams)
if err != nil {
ShowError(w, req, fmt.Sprintf("rest_create_index:"+
" error parsing planParams: %s, err: %v",
planParamsStr, err), 400)
return
}
} else {
planParams = &indexDef.PlanParams
}
prevIndexUUID := req.FormValue("prevIndexUUID") // Defaults to "".
err = h.mgr.CreateIndex(sourceType, sourceName,
sourceUUID, sourceParams,
indexType, indexName, string(indexParams),
*planParams, prevIndexUUID)
if err != nil {
ShowError(w, req, fmt.Sprintf("rest_create_index:"+
" error creating index: %s, err: %v",
indexName, err), 400)
return
}
MustEncode(w, struct {
// TODO: Should return created vs 200 HTTP code?
Status string `json:"status"`
}{Status: "ok"})
}