Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new extension points for ResourcePlugin and ResourceAccessControlPlugin #233

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.plugins.resource;

public class SampleResourcePluginIT {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.plugins.resource.sample;

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.plugins.resource.SharableResource;

import java.io.IOException;
import java.time.Instant;

public class SampleResource implements SharableResource {

private String name;
private Instant lastUpdateTime;

public SampleResource() {
Instant now = Instant.now();
this.lastUpdateTime = now;
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field("name", name).endObject();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeInstant(lastUpdateTime);

}

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

@Override
public String getName() {
return name;
}

@Override
public Instant getLastUpdateTime() {
return lastUpdateTime;
}

public void setName(String name) {
this.name = name;
}

public void setLastUpdateTime(Instant lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.plugins.resource.sample;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.plugins.resource.ResourceParser;

import java.io.IOException;
import java.time.Instant;

public class SampleResourceParser implements ResourceParser<SampleResource> {

@Override
public SampleResource parse(XContentParser parser, String id) throws IOException {
SampleResource resource = new SampleResource();
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);

while (!parser.nextToken().equals(XContentParser.Token.END_OBJECT)) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case "name":
resource.setName(parser.text());
break;
case "last_update_time":
resource.setLastUpdateTime(parseInstantValue(parser));
break;
default:
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation());
}
}
return resource;
}

private Instant parseInstantValue(XContentParser parser) throws IOException {
if (XContentParser.Token.VALUE_NULL.equals(parser.currentToken())) {
return null;
}
if (parser.currentToken().isValue()) {
return Instant.ofEpochMilli(parser.longValue());
}
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation());
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.plugins.resource.sample;

import org.opensearch.action.ActionRequest;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.indices.SystemIndexDescriptor;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.ResourcePlugin;
import org.opensearch.plugins.SystemIndexPlugin;
import org.opensearch.plugins.resource.SharableResourceType;
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceAction;
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceRestAction;
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceTransportAction;
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceAction;
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceRestAction;
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceTransportAction;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;

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

public class SampleResourcePlugin extends Plugin implements ResourcePlugin, SystemIndexPlugin, ActionPlugin {

public static final String RESOURCE_INDEX_NAME = ".sample_resources";

@Override
public List<RestHandler> getRestHandlers(
Settings settings,
RestController restController,
ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings,
SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster
) {
return List.of(new CreateSampleResourceRestAction(), new GetSampleResourceRestAction());
}

@Override
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return List.of(
new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class),
new ActionHandler<>(GetSampleResourceAction.INSTANCE, GetSampleResourceTransportAction.class)
);
}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
final SystemIndexDescriptor systemIndexDescriptor = new SystemIndexDescriptor(RESOURCE_INDEX_NAME, "Example index with resources");
return Collections.singletonList(systemIndexDescriptor);
}

@Override
public List<SharableResourceType> getResourceTypes() {
return List.of(SampleResourceType.getInstance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.plugins.resource.sample;

import org.opensearch.plugins.resource.ResourceSharingService;
import org.opensearch.plugins.resource.SharableResourceType;

import static org.opensearch.plugins.resource.sample.SampleResourcePlugin.RESOURCE_INDEX_NAME;

public class SampleResourceType implements SharableResourceType {
private volatile ResourceSharingService resourceSharingService;

private static final SampleResourceType INSTANCE = new SampleResourceType();

private SampleResourceType() {}

public static SampleResourceType getInstance() {
return INSTANCE;
}

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

@Override
public String getResourceIndex() {
return RESOURCE_INDEX_NAME;
}

@Override
public void assignResourceSharingService(ResourceSharingService resourceSharingService) {
this.resourceSharingService = resourceSharingService;
}

public ResourceSharingService getResourceSharingService() {
return this.resourceSharingService;
}
}
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.plugins.resource.sample.action.create;

import org.opensearch.action.ActionType;

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

private CreateSampleResourceAction() {
super(NAME, CreateSampleResourceResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.plugins.resource.sample.action.create;

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;
import org.opensearch.plugins.resource.SharableResource;
import org.opensearch.plugins.resource.sample.SampleResource;

import java.io.IOException;

/**
* Request object for CreateSampleResource transport action
*/
public class CreateSampleResourceRequest extends ActionRequest {

private final SampleResource resource;

/**
* Default constructor
*/
public CreateSampleResourceRequest(SampleResource resource) {
this.resource = resource;
}

public CreateSampleResourceRequest(StreamInput in, Reader<SampleResource> resourceReader) throws IOException {
this.resource = resourceReader.read(in);
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
resource.writeTo(out);
}

@Override
public ActionRequestValidationException validate() {
return null;
}

public SharableResource getResource() {
return this.resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.plugins.resource.sample.action.create;

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 java.io.IOException;

/**
* Response to a CreateSampleResourceRequest
*/
public class CreateSampleResourceResponse extends ActionResponse implements ToXContentObject {
private final String resourceId;

/**
* Default constructor
*
* @param resourceId The resourceId
*/
public CreateSampleResourceResponse(String resourceId) {
this.resourceId = resourceId;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(resourceId);
}

/**
* Constructor with StreamInput
*
* @param in the stream input
*/
public CreateSampleResourceResponse(final StreamInput in) throws IOException {
resourceId = in.readString();
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("resourceId", resourceId);
builder.endObject();
return builder;
}
}
Loading
Loading