Skip to content

Commit

Permalink
Re-organizes and renames sample plugin files
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Oct 4, 2024
1 parent f692bc1 commit 8151697
Show file tree
Hide file tree
Showing 26 changed files with 583 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
*/
package org.opensearch.sample;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.function.Supplier;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -44,12 +41,16 @@
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.sample.actions.create.CreateSampleResourceAction;
import org.opensearch.sample.actions.create.CreateSampleResourceRestAction;
import org.opensearch.sample.actions.create.CreateSampleResourceTransportAction;
import org.opensearch.sample.actions.list.ListSampleResourceAction;
import org.opensearch.sample.actions.list.ListSampleResourceRestAction;
import org.opensearch.sample.actions.list.ListSampleResourceTransportAction;
import org.opensearch.sample.actions.create.CreateResourceAction;
import org.opensearch.sample.actions.create.CreateResourceRestAction;
import org.opensearch.sample.actions.list.ListAccessibleResourcesAction;
import org.opensearch.sample.actions.list.ListAccessibleResourcesRestAction;
import org.opensearch.sample.actions.share.ShareResourceAction;
import org.opensearch.sample.actions.verify.VerifyResourceAccessAction;
import org.opensearch.sample.transport.CreateResourceTransportAction;
import org.opensearch.sample.transport.ListAccessibleResourcesTransportAction;
import org.opensearch.sample.transport.ShareResourceTransportAction;
import org.opensearch.sample.transport.VerifyResourceAccessTransportAction;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;
Expand All @@ -62,7 +63,9 @@
public class SampleResourcePlugin extends Plugin implements ActionPlugin, SystemIndexPlugin, ResourcePlugin {
private static final Logger log = LogManager.getLogger(SampleResourcePlugin.class);

public static final String RESOURCE_INDEX_NAME = ".sample_resources";
public static final String RESOURCE_INDEX_NAME = ".sample_resource_sharing_plugin";

public final static Map<String, Object> INDEX_SETTINGS = Map.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-all");

private Client client;

Expand Down Expand Up @@ -95,14 +98,16 @@ public List<RestHandler> getRestHandlers(
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster
) {
return List.of(new CreateSampleResourceRestAction(), new ListSampleResourceRestAction());
return List.of(new CreateResourceRestAction(), new ListAccessibleResourcesRestAction());
}

@Override
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return List.of(
new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class),
new ActionHandler<>(ListSampleResourceAction.INSTANCE, ListSampleResourceTransportAction.class)
new ActionHandler<>(CreateResourceAction.INSTANCE, CreateResourceTransportAction.class),
new ActionHandler<>(ListAccessibleResourcesAction.INSTANCE, ListAccessibleResourcesTransportAction.class),
new ActionHandler<>(ShareResourceAction.INSTANCE, ShareResourceTransportAction.class),
new ActionHandler<>(VerifyResourceAccessAction.INSTANCE, VerifyResourceAccessTransportAction.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@
package org.opensearch.sample.actions.create;

import org.opensearch.action.ActionType;
import org.opensearch.sample.transport.CreateResourceResponse;

/**
* Action to create a sample resource
*/
public class CreateSampleResourceAction extends ActionType<CreateResourceResponse> {
public class CreateResourceAction extends ActionType<CreateResourceResponse> {
/**
* Create sample resource action instance
*/
public static final CreateSampleResourceAction INSTANCE = new CreateSampleResourceAction();
public static final CreateResourceAction INSTANCE = new CreateResourceAction();
/**
* Create sample resource action name
*/
public static final String NAME = "cluster:admin/sampleresource/create";

private CreateSampleResourceAction() {
private CreateResourceAction() {
super(NAME, CreateResourceResponse::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

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

import java.io.IOException;

Expand All @@ -19,19 +19,19 @@
/**
* Request object for CreateSampleResource transport action
*/
public class CreateResourceRequest<T extends Resource> extends ActionRequest {
public class CreateResourceRequest extends ActionRequest {

private final T resource;
private final Resource resource;

/**
* Default constructor
*/
public CreateResourceRequest(T resource) {
public CreateResourceRequest(Resource resource) {
this.resource = resource;
}

public CreateResourceRequest(StreamInput in, Reader<T> resourceReader) throws IOException {
this.resource = resourceReader.read(in);
public CreateResourceRequest(StreamInput in) throws IOException {
this.resource = in.readNamedWriteable(Resource.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

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

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;
import org.opensearch.sample.transport.CreateResourceRequest;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.POST;

public class CreateSampleResourceRestAction extends BaseRestHandler {
public class CreateResourceRestAction extends BaseRestHandler {

public CreateSampleResourceRestAction() {}
public CreateResourceRestAction() {}

@Override
public List<Route> routes() {
return singletonList(new Route(POST, "/_plugins/resource_sharing_example/resource"));
return singletonList(new Route(POST, "/_plugins/sample_resource_sharing/resource"));
}

@Override
Expand All @@ -46,9 +45,9 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
String name = (String) source.get("name");
SampleResource resource = new SampleResource();
resource.setName(name);
final CreateResourceRequest<SampleResource> createSampleResourceRequest = new CreateResourceRequest<>(resource);
final CreateResourceRequest createSampleResourceRequest = new CreateResourceRequest(resource);
return channel -> client.executeLocally(
CreateSampleResourceAction.INSTANCE,
CreateResourceAction.INSTANCE,
createSampleResourceRequest,
new RestToXContentListener<>(channel)
);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void writeTo(StreamOutput streamOutput) throws IOException {

@Override
public String getWriteableName() {
return "sampled_resource";
return "sample_resource";
}

public void setName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
/**
* Action to list sample resources
*/
public class ListSampleResourceAction extends ActionType<ListSampleResourceResponse> {
public class ListAccessibleResourcesAction extends ActionType<ListAccessibleResourcesResponse> {
/**
* List sample resource action instance
*/
public static final ListSampleResourceAction INSTANCE = new ListSampleResourceAction();
public static final ListAccessibleResourcesAction INSTANCE = new ListAccessibleResourcesAction();
/**
* List sample resource action name
*/
public static final String NAME = "cluster:admin/sampleresource/list";

private ListSampleResourceAction() {
super(NAME, ListSampleResourceResponse::new);
private ListAccessibleResourcesAction() {
super(NAME, ListAccessibleResourcesResponse::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
/**
* Request object for ListSampleResource transport action
*/
public class ListSampleResourceRequest extends ActionRequest {
public class ListAccessibleResourcesRequest extends ActionRequest {

public ListSampleResourceRequest() {}
public ListAccessibleResourcesRequest() {}

/**
* Constructor with stream input
* @param in the stream input
* @throws IOException IOException
*/
public ListSampleResourceRequest(final StreamInput in) throws IOException {}
public ListAccessibleResourcesRequest(final StreamInput in) throws IOException {}

@Override
public void writeTo(final StreamOutput out) throws IOException {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.sample.actions.list;

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;

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

public ListAccessibleResourcesResponse(List<String> resourceIds) {
this.resourceIds = resourceIds;
}

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

public ListAccessibleResourcesResponse(final StreamInput in) throws IOException {
resourceIds = in.readStringList();
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("resource-ids", resourceIds);
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.GET;

public class ListSampleResourceRestAction extends BaseRestHandler {
public class ListAccessibleResourcesRestAction extends BaseRestHandler {

public ListSampleResourceRestAction() {}
public ListAccessibleResourcesRestAction() {}

@Override
public List<Route> routes() {
return singletonList(new Route(GET, "/_plugins/resource_sharing_example/resource"));
return singletonList(new Route(GET, "/_plugins/sample_resource_sharing/resource"));
}

@Override
Expand All @@ -34,10 +34,10 @@ public String getName() {

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
final ListSampleResourceRequest listSampleResourceRequest = new ListSampleResourceRequest();
final ListAccessibleResourcesRequest listAccessibleResourcesRequest = new ListAccessibleResourcesRequest();
return channel -> client.executeLocally(
ListSampleResourceAction.INSTANCE,
listSampleResourceRequest,
ListAccessibleResourcesAction.INSTANCE,
listAccessibleResourcesRequest,
new RestToXContentListener<>(channel)
);
}
Expand Down
Loading

0 comments on commit 8151697

Please sign in to comment.