|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.wlm.rest; |
| 10 | + |
| 11 | +import org.opensearch.client.node.NodeClient; |
| 12 | +import org.opensearch.core.rest.RestStatus; |
| 13 | +import org.opensearch.core.xcontent.ToXContent; |
| 14 | +import org.opensearch.core.xcontent.XContentParser; |
| 15 | +import org.opensearch.plugin.wlm.action.UpdateQueryGroupAction; |
| 16 | +import org.opensearch.plugin.wlm.action.UpdateQueryGroupRequest; |
| 17 | +import org.opensearch.plugin.wlm.action.UpdateQueryGroupResponse; |
| 18 | +import org.opensearch.rest.BaseRestHandler; |
| 19 | +import org.opensearch.rest.BytesRestResponse; |
| 20 | +import org.opensearch.rest.RestChannel; |
| 21 | +import org.opensearch.rest.RestRequest; |
| 22 | +import org.opensearch.rest.RestResponse; |
| 23 | +import org.opensearch.rest.action.RestResponseListener; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static org.opensearch.rest.RestRequest.Method.POST; |
| 29 | +import static org.opensearch.rest.RestRequest.Method.PUT; |
| 30 | + |
| 31 | +/** |
| 32 | + * Rest action to update a QueryGroup |
| 33 | + * |
| 34 | + * @opensearch.experimental |
| 35 | + */ |
| 36 | +public class RestUpdateQueryGroupAction extends BaseRestHandler { |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor for RestUpdateQueryGroupAction |
| 40 | + */ |
| 41 | + public RestUpdateQueryGroupAction() {} |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getName() { |
| 45 | + return "update_query_group"; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * The list of {@link Route}s that this RestHandler is responsible for handling. |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public List<Route> routes() { |
| 53 | + return List.of(new Route(POST, "_wlm/query_group/{name}"), new Route(PUT, "_wlm/query_group/{name}")); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { |
| 58 | + try (XContentParser parser = request.contentParser()) { |
| 59 | + UpdateQueryGroupRequest updateQueryGroupRequest = UpdateQueryGroupRequest.fromXContent(parser, request.param("name")); |
| 60 | + return channel -> client.execute(UpdateQueryGroupAction.INSTANCE, updateQueryGroupRequest, updateQueryGroupResponse(channel)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private RestResponseListener<UpdateQueryGroupResponse> updateQueryGroupResponse(final RestChannel channel) { |
| 65 | + return new RestResponseListener<>(channel) { |
| 66 | + @Override |
| 67 | + public RestResponse buildResponse(final UpdateQueryGroupResponse response) throws Exception { |
| 68 | + return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)); |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | +} |
0 commit comments