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 fd2029f50b..b2d6c85f1f 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 @@ -45,9 +45,11 @@ import org.opensearch.security.sampleextension.actions.ListSampleResourceRestAction; import org.opensearch.security.sampleextension.actions.ListSampleResourceTransportAction; import org.opensearch.security.sampleextension.actions.SampleResource; +import org.opensearch.security.sampleextension.actions.UpdateSampleResourceAction; import org.opensearch.security.sampleextension.actions.UpdateSampleResourceSharingAction; import org.opensearch.security.sampleextension.actions.UpdateSampleResourceSharingRestAction; import org.opensearch.security.sampleextension.actions.UpdateSampleResourceSharingTransportAction; +import org.opensearch.security.sampleextension.actions.UpdateSampleResourceTransportAction; import org.opensearch.security.sampleextension.resource.SampleResourceSharingService; import org.opensearch.security.spi.DefaultResourceSharingService; import org.opensearch.security.spi.ResourceSharingService; @@ -115,7 +117,8 @@ public List getRestHandlers( return List.of( new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class), new ActionHandler<>(ListSampleResourceAction.INSTANCE, ListSampleResourceTransportAction.class), - new ActionHandler<>(UpdateSampleResourceSharingAction.INSTANCE, UpdateSampleResourceSharingTransportAction.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/UpdateSampleResourceAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceAction.java new file mode 100644 index 0000000000..c3037a03cf --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceAction.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; + +import org.opensearch.action.ActionType; + +/** + * Action to create a sample resource + */ +public class UpdateSampleResourceAction extends ActionType { + /** + * Create sample resource action instance + */ + public static final UpdateSampleResourceAction INSTANCE = new UpdateSampleResourceAction(); + /** + * Create sample resource action name + */ + public static final String NAME = "cluster:admin/sampleresource/update"; + + private UpdateSampleResourceAction() { + super(NAME, UpdateSampleResourceResponse::new); + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRequest.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRequest.java new file mode 100644 index 0000000000..8dd86933a4 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRequest.java @@ -0,0 +1,53 @@ +/* + * 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; + +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 ListSampleResource transport action + */ +public class UpdateSampleResourceRequest extends ActionRequest { + + private String resourceId; + private String name; + + public UpdateSampleResourceRequest(String resourceId, String name) { + this.resourceId = resourceId; + this.name = name; + } + + public String getResourceId() { + return resourceId; + } + + public String getName() { + return name; + } + + /** + * Constructor with stream input + * @param in the stream input + * @throws IOException IOException + */ + public UpdateSampleResourceRequest(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/UpdateSampleResourceResponse.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceResponse.java new file mode 100644 index 0000000000..aa103d1440 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceResponse.java @@ -0,0 +1,57 @@ +/* + * 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; + +import java.io.IOException; +import java.util.List; + +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.spi.AbstractResource; + +/** + * Response to a ListSampleResourceRequest + */ +public class UpdateSampleResourceResponse extends ActionResponse implements ToXContentObject { + private final List resources; + + /** + * Default constructor + * + * @param resources The resources + */ + public UpdateSampleResourceResponse(List resources) { + this.resources = resources; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeList(resources); + } + + /** + * Constructor with StreamInput + * + * @param in the stream input + */ + public UpdateSampleResourceResponse(final StreamInput in) throws IOException { + resources = in.readList(SampleResource::new); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.array("resources", (Object[]) resources.toArray(new AbstractResource[0])); + builder.endObject(); + return builder; + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRestAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRestAction.java new file mode 100644 index 0000000000..01c0dc3aa7 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRestAction.java @@ -0,0 +1,57 @@ +/* + * 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; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.opensearch.client.node.NodeClient; +import org.opensearch.core.xcontent.XContentParser; +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.PUT; + +public class UpdateSampleResourceRestAction extends BaseRestHandler { + + public UpdateSampleResourceRestAction() {} + + @Override + public List routes() { + return singletonList(new Route(PUT, "/_plugins/resource_sharing_example/resource/update/{id}")); + } + + @Override + public String getName() { + return "update_sample_resource"; + } + + @SuppressWarnings("unchecked") + @Override + public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + String resourceId = request.param("id"); + Map source; + try (XContentParser parser = request.contentParser()) { + source = parser.map(); + } + + String name = (String) source.get("name"); + + // TODO Update the request obj + final UpdateSampleResourceRequest updateSampleResourceRequest = new UpdateSampleResourceRequest(resourceId, name); + return channel -> client.executeLocally( + UpdateSampleResourceAction.INSTANCE, + updateSampleResourceRequest, + new RestToXContentListener<>(channel) + ); + } +} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceTransportAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceTransportAction.java new file mode 100644 index 0000000000..76c0a7e2c1 --- /dev/null +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceTransportAction.java @@ -0,0 +1,80 @@ +/* + * 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; + +import java.io.IOException; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import org.opensearch.action.index.IndexRequest; +import org.opensearch.action.index.IndexResponse; +import org.opensearch.action.support.ActionFilters; +import org.opensearch.action.support.HandledTransportAction; +import org.opensearch.action.support.WriteRequest; +import org.opensearch.client.Client; +import org.opensearch.common.inject.Inject; +import org.opensearch.core.action.ActionListener; +import org.opensearch.core.xcontent.ToXContent; +import org.opensearch.tasks.Task; +import org.opensearch.transport.TransportService; + +import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; + +/** + * Transport action for UpdateSampleResource. + */ +public class UpdateSampleResourceTransportAction extends HandledTransportAction { + private static final Logger log = LogManager.getLogger(UpdateSampleResourceTransportAction.class); + + private final Client nodeClient; + private final String resourceIndex; + + @Inject + public UpdateSampleResourceTransportAction( + TransportService transportService, + ActionFilters actionFilters, + Client nodeClient, + String resourceIndex + ) { + super(UpdateSampleResourceAction.NAME, transportService, actionFilters, UpdateSampleResourceRequest::new); + this.nodeClient = nodeClient; + this.resourceIndex = resourceIndex; + } + + @Override + protected void doExecute(Task task, UpdateSampleResourceRequest request, ActionListener actionListener) { + indexResource(request, actionListener); + } + + private void indexResource(UpdateSampleResourceRequest request, ActionListener listener) { + log.warn("resourceId: " + request.getResourceId()); + String name = request.getName(); + SampleResource updatedResource = new SampleResource(); + updatedResource.setName(name); + try { + IndexRequest ir = nodeClient.prepareIndex(resourceIndex) + .setId(request.getResourceId()) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setSource(updatedResource.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS)) + .request(); + + log.warn("Index Request: " + ir.toString()); + + ActionListener irListener = ActionListener.wrap(idxResponse -> { + log.info("Updated resource: " + idxResponse.toString()); + listener.onResponse(new UpdateSampleResourceResponse(List.of(updatedResource))); + }, listener::onFailure); + nodeClient.index(ir, irListener); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/spi/src/main/java/org/opensearch/security/spi/actions/UpdateResourceSharingTransportAction.java b/spi/src/main/java/org/opensearch/security/spi/actions/UpdateResourceSharingTransportAction.java index 78d9c1a827..a4b15bdf81 100644 --- a/spi/src/main/java/org/opensearch/security/spi/actions/UpdateResourceSharingTransportAction.java +++ b/spi/src/main/java/org/opensearch/security/spi/actions/UpdateResourceSharingTransportAction.java @@ -15,13 +15,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.opensearch.action.index.IndexRequest; -import org.opensearch.action.index.IndexResponse; import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; import org.opensearch.action.support.ActionFilters; import org.opensearch.action.support.HandledTransportAction; -import org.opensearch.action.support.WriteRequest; import org.opensearch.action.update.UpdateRequest; import org.opensearch.action.update.UpdateResponse; import org.opensearch.client.Client; @@ -29,7 +26,6 @@ import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.action.ActionListener; import org.opensearch.core.common.io.stream.Writeable; -import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.QueryBuilders; @@ -40,8 +36,6 @@ import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; -import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; - /** * Transport action for CreateSampleResource. */ @@ -133,31 +127,4 @@ public void onFailure(Exception e) { }); } } - - private void indexResource(CreateResourceRequest request, ActionListener listener) { - log.warn("Sample name: " + request.getResource()); - AbstractResource sample = request.getResource(); - try { - IndexRequest ir = nodeClient.prepareIndex(resourceIndex) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource(sample.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS)) - .request(); - - log.warn("Index Request: " + ir.toString()); - - // ActionListener resourceSharingListener = ActionListener.wrap(resourceSharingResponse -> { - // listener.onResponse(new CreateResourceResponse("Created resource: " + resourceSharingResponse.toString())); - // }, listener::onFailure); - - ActionListener irListener = ActionListener.wrap(idxResponse -> { - log.info("Created resource: " + idxResponse.toString()); - // ResourceSharingUtils.getInstance() - // .indexResourceSharing(idxResponse.getId(), sample, ShareWith.PUBLIC, resourceSharingListener); - listener.onResponse(new CreateResourceResponse("Created resource: " + idxResponse.toString())); - }, listener::onFailure); - nodeClient.index(ir, irListener); - } catch (IOException e) { - throw new RuntimeException(e); - } - } }