Skip to content

Commit

Permalink
Returns actual resource when listing the resource
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
DarshitChanpura committed Dec 12, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
diegoferigo Diego Ferigo
1 parent 5ca5dec commit ca377f6
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -9,14 +9,13 @@
* GitHub history for details.
*/

package org.opensearch.sample.actions.resource.create;
package org.opensearch.sample;

import java.io.IOException;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.sample.Resource;

import static org.opensearch.sample.utils.Constants.RESOURCE_INDEX_NAME;

@@ -26,7 +25,7 @@ public class SampleResource implements Resource {

public SampleResource() {}

SampleResource(StreamInput in) throws IOException {
public SampleResource(StreamInput in) throws IOException {
this.name = in.readString();
}

Original file line number Diff line number Diff line change
@@ -16,30 +16,31 @@
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.sample.SampleResource;

/**
* Response to a ListAccessibleResourcesRequest
*/
public class ListAccessibleResourcesResponse extends ActionResponse implements ToXContentObject {
private final Set<String> resourceIds;
private final Set<SampleResource> resources;

public ListAccessibleResourcesResponse(Set<String> resourceIds) {
this.resourceIds = resourceIds;
public ListAccessibleResourcesResponse(Set<SampleResource> resources) {
this.resources = resources;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(resourceIds.toArray(new String[0]));
out.writeCollection(resources);
}

public ListAccessibleResourcesResponse(final StreamInput in) throws IOException {
resourceIds = in.readSet(StreamInput::readString);
this.resources = in.readSet(SampleResource::new);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("resource-ids", resourceIds);
builder.field("resources", resources);
builder.endObject();
return builder;
}
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;
import org.opensearch.sample.SampleResource;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.POST;
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.action.ActionListener;
import org.opensearch.sample.SampleResource;
import org.opensearch.sample.SampleResourcePlugin;
import org.opensearch.sample.actions.access.list.ListAccessibleResourcesAction;
import org.opensearch.sample.actions.access.list.ListAccessibleResourcesRequest;
@@ -41,9 +42,10 @@ public ListAccessibleResourcesTransportAction(TransportService transportService,
protected void doExecute(Task task, ListAccessibleResourcesRequest request, ActionListener<ListAccessibleResourcesResponse> listener) {
try {
ResourceService rs = SampleResourcePlugin.GuiceHolder.getResourceService();
Set<String> resourceIds = rs.getResourceAccessControlPlugin().getAccessibleResourcesForCurrentUser(RESOURCE_INDEX_NAME);
log.info("Successfully fetched accessible resources for current user : {}", resourceIds);
listener.onResponse(new ListAccessibleResourcesResponse(resourceIds));
Set<SampleResource> resources = rs.getResourceAccessControlPlugin()
.getAccessibleResourcesForCurrentUser(RESOURCE_INDEX_NAME, SampleResource.class);
log.info("Successfully fetched accessible resources for current user : {}", resources);
listener.onResponse(new ListAccessibleResourcesResponse(resources));
} catch (Exception e) {
log.info("Failed to list accessible resources for current user: ", e);
listener.onFailure(e);

0 comments on commit ca377f6

Please sign in to comment.