8
8
9
9
package org .opensearch .rest .action .admin .indices ;
10
10
11
- import org .apache .logging .log4j .LogManager ;
12
- import org .apache .logging .log4j .Logger ;
13
11
import org .opensearch .action .admin .indices .view .CreateViewAction ;
14
12
import org .opensearch .action .admin .indices .view .DeleteViewAction ;
15
13
import org .opensearch .action .admin .indices .view .GetViewAction ;
41
39
@ ExperimentalApi
42
40
public class RestViewAction {
43
41
44
- private final static Logger LOG = LogManager .getLogger (RestViewAction .class );
45
-
46
- public static final String VIEW_ID = "view_id" ;
47
- public static final String VIEW_ID_PARAMETER = "{" + VIEW_ID + "}" ;
42
+ public static final String VIEW_NAME = "view_name" ;
43
+ public static final String VIEW_NAME_PARAMETER = "{" + VIEW_NAME + "}" ;
48
44
49
45
/** Handler for create view */
46
+ @ ExperimentalApi
50
47
public static class CreateViewHandler extends BaseRestHandler {
51
48
52
49
@ Override
@@ -63,18 +60,25 @@ public String getName() {
63
60
protected RestChannelConsumer prepareRequest (final RestRequest request , final NodeClient client ) throws IOException {
64
61
try (final XContentParser parser = request .contentParser ()) {
65
62
final CreateViewAction .Request createViewAction = CreateViewAction .Request .fromXContent (parser );
63
+
64
+ final ValidationException validationResult = createViewAction .validate ();
65
+ if (validationResult != null ) {
66
+ throw validationResult ;
67
+ }
68
+
66
69
return channel -> client .admin ().indices ().createView (createViewAction , new RestToXContentListener <>(channel ));
67
70
}
68
71
}
69
72
}
70
73
71
74
/** Handler for delete view */
75
+ @ ExperimentalApi
72
76
public static class DeleteViewHandler extends BaseRestHandler {
73
77
74
78
@ Override
75
79
public List <Route > routes () {
76
80
return List .of (
77
- new NamedRoute .Builder ().path ("/views/" + VIEW_ID_PARAMETER ).method (DELETE ).uniqueName (DeleteViewAction .NAME ).build ()
81
+ new NamedRoute .Builder ().path ("/views/" + VIEW_NAME_PARAMETER ).method (DELETE ).uniqueName (DeleteViewAction .NAME ).build ()
78
82
);
79
83
}
80
84
@@ -85,20 +89,27 @@ public String getName() {
85
89
86
90
@ Override
87
91
protected RestChannelConsumer prepareRequest (final RestRequest request , final NodeClient client ) throws IOException {
88
- final String viewId = request .param (VIEW_ID );
92
+ final String viewId = request .param (VIEW_NAME );
89
93
90
94
final DeleteViewAction .Request deleteRequest = new DeleteViewAction .Request (viewId );
95
+
96
+ final ValidationException validationResult = deleteRequest .validate ();
97
+ if (validationResult != null ) {
98
+ throw validationResult ;
99
+ }
100
+
91
101
return channel -> client .admin ().indices ().deleteView (deleteRequest , new RestToXContentListener <>(channel ));
92
102
}
93
103
}
94
104
95
105
/** Handler for update view */
106
+ @ ExperimentalApi
96
107
public static class UpdateViewHandler extends BaseRestHandler {
97
108
98
109
@ Override
99
110
public List <Route > routes () {
100
111
return List .of (
101
- new NamedRoute .Builder ().path ("/views/" + VIEW_ID_PARAMETER ).method (PUT ).uniqueName (UpdateViewAction .NAME ).build ()
112
+ new NamedRoute .Builder ().path ("/views/" + VIEW_NAME_PARAMETER ).method (PUT ).uniqueName (UpdateViewAction .NAME ).build ()
102
113
);
103
114
}
104
115
@@ -109,21 +120,30 @@ public String getName() {
109
120
110
121
@ Override
111
122
protected RestChannelConsumer prepareRequest (final RestRequest request , final NodeClient client ) throws IOException {
112
- final String viewId = request .param (VIEW_ID );
123
+ final String viewId = request .param (VIEW_NAME );
113
124
114
125
try (final XContentParser parser = request .contentParser ()) {
115
126
final CreateViewAction .Request updateRequest = UpdateViewAction .Request .fromXContent (parser , viewId );
127
+
128
+ final ValidationException validationResult = updateRequest .validate ();
129
+ if (validationResult != null ) {
130
+ throw validationResult ;
131
+ }
132
+
116
133
return channel -> client .admin ().indices ().updateView (updateRequest , new RestToXContentListener <>(channel ));
117
134
}
118
135
}
119
136
}
120
137
121
138
/** Handler for get view */
139
+ @ ExperimentalApi
122
140
public static class GetViewHandler extends BaseRestHandler {
123
141
124
142
@ Override
125
143
public List <Route > routes () {
126
- return List .of (new NamedRoute .Builder ().path ("/views/" + VIEW_ID_PARAMETER ).method (GET ).uniqueName (GetViewAction .NAME ).build ());
144
+ return List .of (
145
+ new NamedRoute .Builder ().path ("/views/" + VIEW_NAME_PARAMETER ).method (GET ).uniqueName (GetViewAction .NAME ).build ()
146
+ );
127
147
}
128
148
129
149
@ Override
@@ -133,14 +153,21 @@ public String getName() {
133
153
134
154
@ Override
135
155
protected RestChannelConsumer prepareRequest (final RestRequest request , final NodeClient client ) throws IOException {
136
- final String viewId = request .param (VIEW_ID );
156
+ final String viewId = request .param (VIEW_NAME );
137
157
138
158
final GetViewAction .Request getRequest = new GetViewAction .Request (viewId );
159
+
160
+ final ValidationException validationResult = getRequest .validate ();
161
+ if (validationResult != null ) {
162
+ throw validationResult ;
163
+ }
164
+
139
165
return channel -> client .admin ().indices ().getView (getRequest , new RestToXContentListener <>(channel ));
140
166
}
141
167
}
142
168
143
169
/** Handler for get view */
170
+ @ ExperimentalApi
144
171
public static class ListViewNamesHandler extends BaseRestHandler {
145
172
146
173
@ Override
@@ -160,15 +187,16 @@ protected RestChannelConsumer prepareRequest(final RestRequest request, final No
160
187
}
161
188
162
189
/** Handler for search view */
190
+ @ ExperimentalApi
163
191
public static class SearchViewHandler extends BaseRestHandler {
164
192
@ Override
165
193
public List <Route > routes () {
166
194
return List .of (
167
- new NamedRoute .Builder ().path ("/views/" + VIEW_ID_PARAMETER + "/_search" )
195
+ new NamedRoute .Builder ().path ("/views/" + VIEW_NAME_PARAMETER + "/_search" )
168
196
.method (GET )
169
197
.uniqueName (SearchViewAction .NAME )
170
198
.build (),
171
- new NamedRoute .Builder ().path ("/views/" + VIEW_ID_PARAMETER + "/_search" )
199
+ new NamedRoute .Builder ().path ("/views/" + VIEW_NAME_PARAMETER + "/_search" )
172
200
.method (POST )
173
201
.uniqueName (SearchViewAction .NAME )
174
202
.build ()
@@ -182,7 +210,7 @@ public String getName() {
182
210
183
211
@ Override
184
212
public RestChannelConsumer prepareRequest (final RestRequest request , final NodeClient client ) throws IOException {
185
- final String viewId = request .param (VIEW_ID );
213
+ final String viewId = request .param (VIEW_NAME );
186
214
187
215
final SearchViewAction .Request viewSearchRequest = new SearchViewAction .Request (viewId );
188
216
final IntConsumer setSize = size -> viewSearchRequest .source ().size (size );
@@ -208,83 +236,4 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
208
236
};
209
237
}
210
238
}
211
-
212
- // TODO: Replace and reorganize this layout
213
-
214
- // public List<Route> routes() {
215
-
216
- // return List.of(
217
- // new NamedRoute.Builder().path("/views").method(GET).uniqueName("cluster:views:list").build(),
218
- // new NamedRoute.Builder().path("/views/" + viewIdParameter).method(GET).uniqueName("cluster:views:get").build(),
219
- // new NamedRoute.Builder().path("/views/" + viewIdParameter).method(DELETE).uniqueName("cluster:views:delete").build(),
220
- // new NamedRoute.Builder().path("/views/" + viewIdParameter).method(PUT).uniqueName("cluster:views:update").build()
221
- // );
222
- // }
223
-
224
- // public RestResponse handlePost(final RestRequest r, final RestChannel channel) throws IOException {
225
- // final View inputView;
226
- // try (final XContentParser parser = r.contentParser()) {
227
- // inputView = View.fromXContent(parser);
228
- // }
229
-
230
- // final long currentTime = System.currentTimeMillis();
231
- // final View view = new View(inputView.name, inputView.description, currentTime, currentTime, inputView.targets);
232
-
233
- // clusterService.submitStateUpdateTask("create_view_task", new ClusterStateUpdateTask() {
234
- // @Override
235
- // public ClusterState execute(final ClusterState currentState) throws Exception {
236
- // return new ClusterState.Builder(clusterService.state()).metadata(Metadata.builder(currentState.metadata()).put(view))
237
- // .build();
238
- // }
239
-
240
- // @Override
241
- // public void onFailure(final String source, final Exception e) {
242
- // LOG.error("Unable to create view, due to {}", source, e);
243
- // channel.sendResponse(
244
- // new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, "Unknown error occurred, see the log for details.")
245
- // );
246
- // }
247
-
248
- // @Override
249
- // public void clusterStateProcessed(final String source, final ClusterState oldState, final ClusterState newState) {
250
- // try {
251
- // channel.sendResponse(
252
- // new BytesRestResponse(RestStatus.CREATED, channel.newBuilder().startObject().field(view.name, view).endObject())
253
- // );
254
- // } catch (final IOException e) {
255
- // // TODO?
256
- // LOG.error(e);
257
- // }
258
- // }
259
- // });
260
- // // TODO: Handle CREATED vs UPDATED
261
- // return null;
262
- // }
263
-
264
- // public RestResponse handleSingleGet(final RestRequest r, final XContentBuilder builder) throws IOException {
265
- // final String viewId = r.param(VIEW_ID);
266
-
267
- // if (Strings.isNullOrEmpty(viewId)) {
268
- // return new BytesRestResponse(RestStatus.NOT_FOUND, "");
269
- // }
270
-
271
- // final Optional<View> view = Optional.ofNullable(clusterService.state().getMetadata())
272
- // .map(m -> m.views())
273
- // .map(views -> views.get(viewId));
274
-
275
- // if (view.isEmpty()) {
276
- // return new BytesRestResponse(RestStatus.NOT_FOUND, "");
277
- // }
278
-
279
- // return new BytesRestResponse(RestStatus.OK, builder.startObject().value(view).endObject());
280
- // }
281
-
282
- // public RestResponse handleSinglePut(final RestRequest r) {
283
- // return new BytesRestResponse(RestStatus.NOT_IMPLEMENTED, "");
284
- // }
285
-
286
- // public RestResponse handleSingleDelete(final RestRequest r) {
287
- // return new BytesRestResponse(RestStatus.NOT_IMPLEMENTED, "");
288
- // }
289
-
290
239
}
0 commit comments