From 0d8eeed39b1cda1913fe1d847172cc8f2c8fc1e1 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Thu, 12 Dec 2024 16:48:29 -0500 Subject: [PATCH] Add Get action Signed-off-by: Craig Perkins --- .../SampleExtensionPluginTests.java | 4 ++ .../SampleExtensionPlugin.java | 5 ++ .../actions/get/GetSampleResourceAction.java | 29 +++++++++ .../actions/get/GetSampleResourceRequest.java | 47 ++++++++++++++ .../get/GetSampleResourceResponse.java | 56 +++++++++++++++++ .../get/GetSampleResourceRestAction.java | 48 +++++++++++++++ .../get/GetSampleResourceTransportAction.java | 61 +++++++++++++++++++ 7 files changed, 250 insertions(+) create mode 100644 sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceAction.java create mode 100644 sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRequest.java create mode 100644 sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceResponse.java create mode 100644 sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRestAction.java create mode 100644 sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceTransportAction.java diff --git a/sample-extension-plugin/src/integrationTest/java/org/opensearch/security/sampleextension/SampleExtensionPluginTests.java b/sample-extension-plugin/src/integrationTest/java/org/opensearch/security/sampleextension/SampleExtensionPluginTests.java index 8a7b106a1c..c18e000a12 100644 --- a/sample-extension-plugin/src/integrationTest/java/org/opensearch/security/sampleextension/SampleExtensionPluginTests.java +++ b/sample-extension-plugin/src/integrationTest/java/org/opensearch/security/sampleextension/SampleExtensionPluginTests.java @@ -67,6 +67,10 @@ public void testCreateAndUpdateOwnSampleResource() throws Exception { System.out.println("resourceId: " + resourceId); + HttpResponse getResponse = client.get("_plugins/resource_sharing_example/resource/" + resourceId); + getResponse.assertStatusCode(HttpStatus.SC_OK); + System.out.println("Get Response: " + getResponse.getBody()); + String sampleResourceUpdated = "{\"name\":\"sampleUpdated\"}"; HttpResponse updateResponse = client.putJson( "_plugins/resource_sharing_example/resource/update/" + resourceId, diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java index a8cb3f084a..bd8c409689 100644 --- a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java @@ -42,6 +42,9 @@ import org.opensearch.security.sampleextension.actions.create.CreateSampleResourceAction; import org.opensearch.security.sampleextension.actions.create.CreateSampleResourceRestAction; import org.opensearch.security.sampleextension.actions.create.CreateSampleResourceTransportAction; +import org.opensearch.security.sampleextension.actions.get.GetSampleResourceAction; +import org.opensearch.security.sampleextension.actions.get.GetSampleResourceRestAction; +import org.opensearch.security.sampleextension.actions.get.GetSampleResourceTransportAction; import org.opensearch.security.sampleextension.actions.list.ListSampleResourceAction; import org.opensearch.security.sampleextension.actions.list.ListSampleResourceRestAction; import org.opensearch.security.sampleextension.actions.list.ListSampleResourceTransportAction; @@ -108,6 +111,7 @@ public List getRestHandlers( ) { return List.of( new CreateSampleResourceRestAction(), + new GetSampleResourceRestAction(), new ListSampleResourceRestAction(), new UpdateSampleResourceRestAction(), new UpdateSampleResourceSharingRestAction() @@ -118,6 +122,7 @@ public List getRestHandlers( public List> getActions() { return List.of( new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class), + new ActionHandler<>(GetSampleResourceAction.INSTANCE, GetSampleResourceTransportAction.class), new ActionHandler<>(ListSampleResourceAction.INSTANCE, ListSampleResourceTransportAction.class), new ActionHandler<>(UpdateSampleResourceSharingAction.INSTANCE, UpdateSampleResourceSharingTransportAction.class), new ActionHandler<>(UpdateSampleResourceAction.INSTANCE, UpdateSampleResourceTransportAction.class) diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceAction.java new file mode 100644 index 0000000000..a58bc0b4bd --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceAction.java @@ -0,0 +1,29 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.security.sampleextension.actions.get; + +import org.opensearch.action.ActionType; + +/** + * Action to get a sample resource + */ +public class GetSampleResourceAction extends ActionType { + /** + * Get sample resource action instance + */ + public static final GetSampleResourceAction INSTANCE = new GetSampleResourceAction(); + /** + * Get sample resource action name + */ + public static final String NAME = "cluster:admin/sampleresource/get"; + + private GetSampleResourceAction() { + super(NAME, GetSampleResourceResponse::new); + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRequest.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRequest.java new file mode 100644 index 0000000000..3dbc2e861d --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRequest.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.security.sampleextension.actions.get; + +import java.io.IOException; + +import org.opensearch.action.ActionRequest; +import org.opensearch.action.ActionRequestValidationException; +import org.opensearch.core.common.io.stream.StreamInput; +import org.opensearch.core.common.io.stream.StreamOutput; + +/** + * Request object for GetSampleResource transport action + */ +public class GetSampleResourceRequest extends ActionRequest { + + private String resourceId; + + public GetSampleResourceRequest(String resourceId) { + this.resourceId = resourceId; + } + + public String getResourceId() { + return resourceId; + } + + /** + * Constructor with stream input + * @param in the stream input + * @throws IOException IOException + */ + public GetSampleResourceRequest(final StreamInput in) throws IOException {} + + @Override + public void writeTo(final StreamOutput out) throws IOException {} + + @Override + public ActionRequestValidationException validate() { + return null; + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceResponse.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceResponse.java new file mode 100644 index 0000000000..35c9b247a0 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceResponse.java @@ -0,0 +1,56 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.security.sampleextension.actions.get; + +import java.io.IOException; + +import org.opensearch.core.action.ActionResponse; +import org.opensearch.core.common.io.stream.StreamInput; +import org.opensearch.core.common.io.stream.StreamOutput; +import org.opensearch.core.xcontent.ToXContentObject; +import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.security.sampleextension.actions.SampleResource; + +/** + * Response to a ListSampleResourceRequest + */ +public class GetSampleResourceResponse extends ActionResponse implements ToXContentObject { + private final SampleResource resource; + + /** + * Default constructor + * + * @param resource The resource + */ + public GetSampleResourceResponse(SampleResource resource) { + this.resource = resource; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + resource.writeTo(out); + } + + /** + * Constructor with StreamInput + * + * @param in the stream input + */ + public GetSampleResourceResponse(final StreamInput in) throws IOException { + resource = SampleResource.from(in); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field("resource", resource); + builder.endObject(); + return builder; + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRestAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRestAction.java new file mode 100644 index 0000000000..3db09de2e1 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRestAction.java @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.security.sampleextension.actions.get; + +import java.io.IOException; +import java.util.List; + +import org.opensearch.client.node.NodeClient; +import org.opensearch.rest.BaseRestHandler; +import org.opensearch.rest.RestRequest; +import org.opensearch.rest.action.RestToXContentListener; + +import static java.util.Collections.singletonList; +import static org.opensearch.rest.RestRequest.Method.GET; + +public class GetSampleResourceRestAction extends BaseRestHandler { + + public GetSampleResourceRestAction() {} + + @Override + public List routes() { + return singletonList(new Route(GET, "/_plugins/resource_sharing_example/resource/{id}")); + } + + @Override + public String getName() { + return "get_sample_resource"; + } + + @SuppressWarnings("unchecked") + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + String resourceId = request.param("id"); + + final GetSampleResourceRequest getSampleResourceRequest = new GetSampleResourceRequest(resourceId); + return channel -> client.executeLocally( + GetSampleResourceAction.INSTANCE, + getSampleResourceRequest, + new RestToXContentListener<>(channel) + ); + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceTransportAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceTransportAction.java new file mode 100644 index 0000000000..c3b109146b --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceTransportAction.java @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.security.sampleextension.actions.get; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import org.opensearch.action.get.GetRequest; +import org.opensearch.action.get.GetResponse; +import org.opensearch.action.support.ActionFilters; +import org.opensearch.action.support.HandledTransportAction; +import org.opensearch.client.Client; +import org.opensearch.common.inject.Inject; +import org.opensearch.core.action.ActionListener; +import org.opensearch.security.sampleextension.actions.SampleResource; +import org.opensearch.tasks.Task; +import org.opensearch.transport.TransportService; + +import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME; + +/** + * Transport action for UpdateSampleResource. + */ +public class GetSampleResourceTransportAction extends HandledTransportAction { + private static final Logger log = LogManager.getLogger(GetSampleResourceTransportAction.class); + + private final Client nodeClient; + + @Inject + public GetSampleResourceTransportAction(TransportService transportService, ActionFilters actionFilters, Client nodeClient) { + super(GetSampleResourceAction.NAME, transportService, actionFilters, GetSampleResourceRequest::new); + this.nodeClient = nodeClient; + } + + @Override + protected void doExecute(Task task, GetSampleResourceRequest request, ActionListener actionListener) { + getResource(request, actionListener); + } + + private void getResource(GetSampleResourceRequest request, ActionListener listener) { + log.warn("resourceId: " + request.getResourceId()); + GetRequest gr = nodeClient.prepareGet().setIndex(RESOURCE_INDEX_NAME).setId(request.getResourceId()).request(); + + log.warn("GET Request: " + gr.toString()); + + ActionListener grListener = ActionListener.wrap(getResponse -> { + log.info("Updated resource: " + getResponse.toString()); + getResponse.getSource(); + SampleResource resource = new SampleResource(); + resource.setName(getResponse.getSource().get("name").toString()); + listener.onResponse(new GetSampleResourceResponse(resource)); + }, listener::onFailure); + nodeClient.get(gr, grListener); + } +}