Skip to content

Commit

Permalink
Add Get action
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 d015aa0 commit 0d8eeed
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +111,7 @@ public List<RestHandler> getRestHandlers(
) {
return List.of(
new CreateSampleResourceRestAction(),
new GetSampleResourceRestAction(),
new ListSampleResourceRestAction(),
new UpdateSampleResourceRestAction(),
new UpdateSampleResourceSharingRestAction()
Expand All @@ -118,6 +122,7 @@ public List<RestHandler> getRestHandlers(
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetSampleResourceResponse> {
/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Route> 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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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<GetSampleResourceRequest, GetSampleResourceResponse> {
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<GetSampleResourceResponse> actionListener) {
getResource(request, actionListener);
}

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));
}, listener::onFailure);
nodeClient.get(gr, grListener);
}
}

0 comments on commit 0d8eeed

Please sign in to comment.