Skip to content

Commit

Permalink
Implement get resource
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 12, 2024
1 parent d062a98 commit 99348d7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -44,18 +41,10 @@ protected void doExecute(Task task, GetSampleResourceRequest request, ActionList
}

private void getResource(GetSampleResourceRequest request, ActionListener<GetSampleResourceResponse> 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<GetResponse> 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<SampleResource> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,4 +72,28 @@ public void onFailure(Exception e) {
client.search(sr, searchListener);
}
}

@SuppressWarnings("unchecked")
@Override
public void getResource(String resourceId, ActionListener<T> getResourceListener) {
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) {
GetRequest gr = new GetRequest(resourceIndex);
gr.id(resourceId);
/* Index already exists, ignore and continue */
ActionListener<GetResponse> getListener = new ActionListener<GetResponse>() {
@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface ResourceSharingService<T extends AbstractResource> {

void listResources(ActionListener<List<T>> listResourceListener);

void getResource(String resourceId, ActionListener<T> getResourceListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -160,4 +161,28 @@ public void onFailure(Exception e) {
// client.search(sr, searchListener);
}
}

@SuppressWarnings("unchecked")
@Override
public void getResource(String resourceId, ActionListener<T> getResourceListener) {
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) {
GetRequest gr = new GetRequest(resourceIndex);
gr.id(resourceId);
/* Index already exists, ignore and continue */
ActionListener<GetResponse> getListener = new ActionListener<GetResponse>() {
@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);
}
}
}

0 comments on commit 99348d7

Please sign in to comment.