Skip to content

Commit

Permalink
Create UpdateResourceAction
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 25e946d commit 586d26c
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +117,8 @@ public List<RestHandler> 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)
);
}

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;

import org.opensearch.action.ActionType;

/**
* Action to create a sample resource
*/
public class UpdateSampleResourceAction extends ActionType<UpdateSampleResourceResponse> {
/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<SampleResource> resources;

/**
* Default constructor
*
* @param resources The resources
*/
public UpdateSampleResourceResponse(List<SampleResource> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Route> 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<String, Object> 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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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<UpdateSampleResourceRequest, UpdateSampleResourceResponse> {
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<UpdateSampleResourceResponse> actionListener) {
indexResource(request, actionListener);
}

private void indexResource(UpdateSampleResourceRequest request, ActionListener<UpdateSampleResourceResponse> 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<IndexResponse> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
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;
import org.opensearch.common.util.concurrent.ThreadContext;
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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -133,31 +127,4 @@ public void onFailure(Exception e) {
});
}
}

private void indexResource(CreateResourceRequest<T> request, ActionListener<CreateResourceResponse> 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<IndexResponse> resourceSharingListener = ActionListener.wrap(resourceSharingResponse -> {
// listener.onResponse(new CreateResourceResponse("Created resource: " + resourceSharingResponse.toString()));
// }, listener::onFailure);

ActionListener<IndexResponse> 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);
}
}
}

0 comments on commit 586d26c

Please sign in to comment.