|
| 1 | +package org.opensearch.timeseries.rest; |
| 2 | + |
| 3 | +import static java.util.Collections.singletonList; |
| 4 | +import static org.opensearch.rest.RestRequest.Method.POST; |
| 5 | +import static org.opensearch.timeseries.constant.CommonValue.CONFIG_ACCESS_CONTROL_BASE_URI; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.opensearch.accesscontrol.resources.ShareWith; |
| 12 | +import org.opensearch.client.node.NodeClient; |
| 13 | +import org.opensearch.common.xcontent.LoggingDeprecationHandler; |
| 14 | +import org.opensearch.common.xcontent.XContentFactory; |
| 15 | +import org.opensearch.common.xcontent.XContentType; |
| 16 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 17 | +import org.opensearch.core.xcontent.XContentParser; |
| 18 | +import org.opensearch.rest.BaseRestHandler; |
| 19 | +import org.opensearch.rest.RestRequest; |
| 20 | +import org.opensearch.rest.action.RestToXContentListener; |
| 21 | +import org.opensearch.timeseries.transport.ShareConfigAction; |
| 22 | +import org.opensearch.timeseries.transport.ShareConfigRequest; |
| 23 | + |
| 24 | +public class RestShareConfigAction extends BaseRestHandler { |
| 25 | + public RestShareConfigAction() {} |
| 26 | + |
| 27 | + @Override |
| 28 | + public List<Route> routes() { |
| 29 | + return singletonList(new Route(POST, CONFIG_ACCESS_CONTROL_BASE_URI + "/share")); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getName() { |
| 34 | + return "share_timeseries_config"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { |
| 39 | + Map<String, Object> source; |
| 40 | + try (XContentParser parser = request.contentParser()) { |
| 41 | + source = parser.map(); |
| 42 | + } |
| 43 | + |
| 44 | + String resourceId = (String) source.get("config_id"); |
| 45 | + |
| 46 | + ShareWith shareWith = parseShareWith(source); |
| 47 | + final ShareConfigRequest shareResourceRequest = new ShareConfigRequest(resourceId, shareWith); |
| 48 | + return channel -> client.executeLocally(ShareConfigAction.INSTANCE, shareResourceRequest, new RestToXContentListener<>(channel)); |
| 49 | + } |
| 50 | + |
| 51 | + private ShareWith parseShareWith(Map<String, Object> source) throws IOException { |
| 52 | + @SuppressWarnings("unchecked") |
| 53 | + Map<String, Object> shareWithMap = (Map<String, Object>) source.get("share_with"); |
| 54 | + if (shareWithMap == null || shareWithMap.isEmpty()) { |
| 55 | + throw new IllegalArgumentException("share_with is required and cannot be empty"); |
| 56 | + } |
| 57 | + |
| 58 | + String jsonString = XContentFactory.jsonBuilder().map(shareWithMap).toString(); |
| 59 | + |
| 60 | + try ( |
| 61 | + XContentParser parser = XContentType.JSON |
| 62 | + .xContent() |
| 63 | + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, jsonString) |
| 64 | + ) { |
| 65 | + return ShareWith.fromXContent(parser); |
| 66 | + } catch (IllegalArgumentException e) { |
| 67 | + throw new IllegalArgumentException("Invalid share_with structure: " + e.getMessage(), e); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments