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 index c3b109146b..714c5956af 100644 --- 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 @@ -11,19 +11,16 @@ 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.security.sampleextension.resource.SampleResourceSharingService; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; -import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME; - /** * Transport action for UpdateSampleResource. */ @@ -44,18 +41,10 @@ protected void doExecute(Task task, GetSampleResourceRequest request, ActionList } 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)); + ActionListener getResourceListener = ActionListener.wrap(sampleResource -> { + System.out.println("sampleResource: " + sampleResource); + listener.onResponse(new GetSampleResourceResponse(sampleResource)); }, listener::onFailure); - nodeClient.get(gr, grListener); + SampleResourceSharingService.getInstance().getSharingService().getResource(request.getResourceId(), getResourceListener); } } diff --git a/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java b/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java index 0396c6d7a5..c05b901b70 100644 --- a/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java +++ b/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java @@ -7,6 +7,8 @@ import java.util.List; import org.opensearch.OpenSearchException; +import org.opensearch.action.get.GetRequest; +import org.opensearch.action.get.GetResponse; import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; import org.opensearch.client.Client; @@ -70,4 +72,28 @@ public void onFailure(Exception e) { client.search(sr, searchListener); } } + + @SuppressWarnings("unchecked") + @Override + public void getResource(String resourceId, ActionListener getResourceListener) { + try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { + GetRequest gr = new GetRequest(resourceIndex); + gr.id(resourceId); + /* Index already exists, ignore and continue */ + ActionListener getListener = new ActionListener() { + @Override + public void onResponse(GetResponse getResponse) { + T resource = newResource(); + resource.fromSource(getResponse.getId(), getResponse.getSourceAsMap()); + getResourceListener.onResponse(resource); + } + + @Override + public void onFailure(Exception e) { + throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); + } + }; + client.get(gr, getListener); + } + } } diff --git a/spi/src/main/java/org/opensearch/security/spi/ResourceSharingService.java b/spi/src/main/java/org/opensearch/security/spi/ResourceSharingService.java index b2eb467573..c73e47a4c7 100644 --- a/spi/src/main/java/org/opensearch/security/spi/ResourceSharingService.java +++ b/spi/src/main/java/org/opensearch/security/spi/ResourceSharingService.java @@ -7,4 +7,6 @@ public interface ResourceSharingService { void listResources(ActionListener> listResourceListener); + + void getResource(String resourceId, ActionListener getResourceListener); } diff --git a/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java b/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java index f2f9c8520d..b0e29ad12b 100644 --- a/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java +++ b/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java @@ -15,6 +15,7 @@ import java.util.List; import org.opensearch.OpenSearchException; +import org.opensearch.action.get.GetRequest; import org.opensearch.action.get.GetResponse; import org.opensearch.action.get.MultiGetItemResponse; import org.opensearch.action.get.MultiGetRequest; @@ -160,4 +161,28 @@ public void onFailure(Exception e) { // client.search(sr, searchListener); } } + + @SuppressWarnings("unchecked") + @Override + public void getResource(String resourceId, ActionListener getResourceListener) { + try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { + GetRequest gr = new GetRequest(resourceIndex); + gr.id(resourceId); + /* Index already exists, ignore and continue */ + ActionListener getListener = new ActionListener() { + @Override + public void onResponse(GetResponse getResponse) { + T resource = newResource(); + resource.fromSource(getResponse.getId(), getResponse.getSourceAsMap()); + getResourceListener.onResponse(resource); + } + + @Override + public void onFailure(Exception e) { + throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); + } + }; + client.get(gr, getListener); + } + } }