forked from arangodb/arangodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-endpoint.js
332 lines (316 loc) · 11.8 KB
/
api-endpoint.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*global require */
////////////////////////////////////////////////////////////////////////////////
/// @brief endpoint management
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 ArangoDB GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var arangodb = require("org/arangodb");
var actions = require("org/arangodb/actions");
var internal = require("internal");
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_get_api_endpoint
/// @brief returns a list of all endpoints
///
/// @RESTHEADER{GET /_api/endpoint, Return list of all endpoints}
///
/// @RESTDESCRIPTION
/// Returns a list of all configured endpoints the server is listening on. For
/// each endpoint, the list of allowed databases is returned too if set.
///
/// The result is a JSON hash which has the endpoints as keys, and the list of
/// mapped database names as values for each endpoint.
///
/// If a list of mapped databases is empty, it means that all databases can be
/// accessed via the endpoint. If a list of mapped databases contains more than
/// one database name, this means that any of the databases might be accessed
/// via the endpoint, and the first database in the list will be treated as
/// the default database for the endpoint. The default database will be used
/// when an incoming request does not specify a database name in the request
/// explicitly.
///
/// **Note**: retrieving the list of all endpoints is allowed in the system database
/// only. Calling this action in any other database will make the server return
/// an error.
///
/// @RESTRETURNCODES
///
/// @RESTRETURNCODE{200}
/// is returned when the list of endpoints can be determined successfully.
///
/// @RESTRETURNCODE{400}
/// is returned if the action is not carried out in the system database.
///
/// @RESTRETURNCODE{405}
/// The server will respond with *HTTP 405* if an unsupported HTTP method is used.
///
/// @EXAMPLES
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointGet}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8532";
/// var body = {
/// endpoint: endpoint,
/// databases: [ "mydb1", "mydb2" ]
/// };
/// curlRequest('POST', url, JSON.stringify(body));
///
/// var response = logCurlRequest('GET', url);
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// curlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
/// @END_EXAMPLE_ARANGOSH_RUN
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_post_api_endpoint
/// @brief connects a new endpoint or reconfigures an existing endpoint
///
/// @RESTHEADER{POST /_api/endpoint, Add new endpoint or reconfigures an existing endpoint}
///
/// @RESTBODYPARAM{description,json,required}
/// A JSON object describing the endpoint.
///
/// @RESTDESCRIPTION
/// The request body must be JSON hash with the following attributes:
///
/// - *endpoint*: the endpoint specification, e.g. *tcp://127.0.0.1:8530*
///
/// - *databases*: a list of database names the endpoint is responsible for.
///
/// If *databases* is an empty list, all databases present in the server will
/// become accessible via the endpoint, with the *_system* database being the
/// default database.
///
/// If *databases* is non-empty, only the specified databases will become
/// available via the endpoint. The first database name in the *databases*
/// list will also become the default database for the endpoint. The default
/// database will always be used if a request coming in on the endpoint does
/// not specify the database name explicitly.
///
/// **Note**: adding or reconfiguring endpoints is allowed in the system database
/// only. Calling this action in any other database will make the server
/// return an error.
///
/// Adding SSL endpoints at runtime is only supported if the server was started
/// with SSL properly configured (e.g. *--server.keyfile* must have been set).
///
/// @RESTRETURNCODES
///
/// @RESTRETURNCODE{200}
/// is returned when the endpoint was added or changed successfully.
///
/// @RESTRETURNCODE{400}
/// is returned if the request is malformed or if the action is not carried out
/// in the system database.
///
/// @RESTRETURNCODE{405}
/// The server will respond with *HTTP 405* if an unsupported HTTP method is used.
///
/// @EXAMPLES
/// Adding an endpoint *tcp://127.0.0.1:8532* with two mapped databases
/// (*mydb1* and *mydb2*). *mydb1* will become the default database for the
/// endpoint.
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointPostOne}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8532";
/// var body = {
/// endpoint: endpoint,
/// databases: [ "mydb1", "mydb2" ]
/// };
/// var response = logCurlRequest('POST', url, JSON.stringify(body));
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// curlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
/// @END_EXAMPLE_ARANGOSH_RUN
///
/// Adding an endpoint *tcp://127.0.0.1:8533* with no database names specified.
/// This will allow access to all databases on this endpoint. The *_system*
/// database will become the default database for requests that come in on this
/// endpoint and do not specify the database name explicitly.
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointPostTwo}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8533";
/// var body = {
/// endpoint: endpoint,
/// databases: [ ]
/// };
/// var response = logCurlRequest('POST', url, JSON.stringify(body));
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// curlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
/// @END_EXAMPLE_ARANGOSH_RUN
///
/// Adding an endpoint *tcp://127.0.0.1:8533* without any databases first,
/// and then updating the databases for the endpoint to *testdb1*, *testdb2*, and
/// *testdb3*.
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointPostChange}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8533";
/// var body = {
/// endpoint: endpoint,
/// databases: [ ]
/// };
/// var response = logCurlRequest('POST', url, JSON.stringify(body));
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
///
/// body.database = [ "testdb1", "testdb2", "testdb3" ];
/// response = logCurlRequest('POST', url, JSON.stringify(body));
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// curlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
/// @END_EXAMPLE_ARANGOSH_RUN
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_delete_api_endpoint
/// @brief disconnects an existing endpoint
///
/// @RESTHEADER{DELETE /_api/endpoint/{endpoint}, Delete and disconnects an existing endpoint}
///
/// @RESTURLPARAMETERS
///
/// @RESTURLPARAM{endpoint,string,required}
/// The endpoint to delete, e.g. *tcp://127.0.0.1:8529*.
///
/// @RESTDESCRIPTION
/// This operation deletes an existing endpoint from the list of all endpoints,
/// and makes the server stop listening on the endpoint.
///
/// **Note**: deleting and disconnecting an endpoint is allowed in the system
/// database only. Calling this action in any other database will make the server
/// return an error.
///
/// Futhermore, the last remaining endpoint cannot be deleted as this would make
/// the server kaputt.
///
/// @RESTRETURNCODES
///
/// @RESTRETURNCODE{200}
/// is returned when the endpoint was deleted and disconnected successfully.
///
/// @RESTRETURNCODE{400}
/// is returned if the request is malformed or if the action is not carried out
/// in the system database.
///
/// @RESTRETURNCODE{404}
/// is returned if the endpoint is not found.
///
/// @RESTRETURNCODE{405}
/// The server will respond with *HTTP 405* if an unsupported HTTP method is used.
///
/// @EXAMPLES
///
/// Deleting an existing endpoint
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointDelete}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8532";
/// var body = {
/// endpoint: endpoint,
/// databases: [ ]
/// };
/// curlRequest('POST', url, JSON.stringify(body));
/// var response = logCurlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// @END_EXAMPLE_ARANGOSH_RUN
///
/// Deleting a non-existing endpoint
///
/// @EXAMPLE_ARANGOSH_RUN{RestEndpointDeleteNonExisting}
/// var url = "/_api/endpoint";
/// var endpoint = "tcp://127.0.0.1:8532";
/// var response = logCurlRequest('DELETE', url + '/' + encodeURIComponent(endpoint));
///
/// assert(response.code === 404);
///
/// logJsonResponse(response);
/// @END_EXAMPLE_ARANGOSH_RUN
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////
actions.defineHttp({
url : "_api/endpoint",
prefix : true,
callback : function (req, res) {
try {
var result;
if (req.requestType === actions.GET) {
actions.resultOk(req, res, actions.HTTP_OK, internal.listEndpoints());
}
else if (req.requestType === actions.POST) {
var body = actions.getJsonBody(req, res);
if (body === undefined || typeof body.endpoint !== 'string') {
actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER,
"invalid endpoint value");
return;
}
result = internal.configureEndpoint(body.endpoint, body.databases || [ ]);
actions.resultOk(req, res, actions.HTTP_OK, { result: result });
}
else if (req.requestType === actions.DELETE) {
if (req.suffix.length !== 1) {
actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER,
"expected DELETE /" + this.url + "/<endpoint>");
return;
}
var endpoint = decodeURIComponent(req.suffix[0]);
result = internal.removeEndpoint(endpoint);
actions.resultOk(req, res, actions.HTTP_OK, { result: result });
}
else {
actions.resultUnsupported(req, res);
}
}
catch (err) {
actions.resultException(req, res, err, undefined, false);
}
}
});
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End: