Skip to content

Commit 6a5238b

Browse files
committed
WIP on SampleResourcePluginIT
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent 1474d19 commit 6a5238b

14 files changed

+662
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource;
10+
11+
public class SampleResourcePluginIT {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample;
10+
11+
import org.opensearch.core.common.io.stream.StreamInput;
12+
import org.opensearch.core.common.io.stream.StreamOutput;
13+
import org.opensearch.core.xcontent.XContentBuilder;
14+
import org.opensearch.plugins.resource.SharableResource;
15+
16+
import java.io.IOException;
17+
import java.time.Instant;
18+
19+
public class SampleResource implements SharableResource {
20+
21+
private String name;
22+
private Instant lastUpdateTime;
23+
24+
public SampleResource() {
25+
Instant now = Instant.now();
26+
this.lastUpdateTime = now;
27+
}
28+
29+
public SampleResource(StreamInput in) throws IOException {
30+
this.name = in.readString();
31+
this.lastUpdateTime = in.readInstant();
32+
}
33+
34+
@Override
35+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
36+
return builder.startObject().field("name", name).endObject();
37+
}
38+
39+
@Override
40+
public void writeTo(StreamOutput out) throws IOException {
41+
out.writeString(name);
42+
out.writeInstant(lastUpdateTime);
43+
44+
}
45+
46+
@Override
47+
public String getWriteableName() {
48+
return "sample_resource";
49+
}
50+
51+
@Override
52+
public String getName() {
53+
return name;
54+
}
55+
56+
@Override
57+
public Instant getLastUpdateTime() {
58+
return lastUpdateTime;
59+
}
60+
61+
public void setName(String name) {
62+
this.name = name;
63+
}
64+
65+
public void setLastUpdateTime(Instant lastUpdateTime) {
66+
this.lastUpdateTime = lastUpdateTime;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample;
10+
11+
import org.opensearch.core.xcontent.XContentParser;
12+
import org.opensearch.core.xcontent.XContentParserUtils;
13+
import org.opensearch.plugins.resource.ResourceParser;
14+
15+
import java.io.IOException;
16+
import java.time.Instant;
17+
18+
public class SampleResourceParser implements ResourceParser<SampleResource> {
19+
20+
@Override
21+
public SampleResource parse(XContentParser parser, String id) throws IOException {
22+
SampleResource resource = new SampleResource();
23+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
24+
25+
while (!parser.nextToken().equals(XContentParser.Token.END_OBJECT)) {
26+
String fieldName = parser.currentName();
27+
parser.nextToken();
28+
switch (fieldName) {
29+
case "name":
30+
resource.setName(parser.text());
31+
break;
32+
case "last_update_time":
33+
resource.setLastUpdateTime(parseInstantValue(parser));
34+
break;
35+
default:
36+
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation());
37+
}
38+
}
39+
return resource;
40+
}
41+
42+
private Instant parseInstantValue(XContentParser parser) throws IOException {
43+
if (XContentParser.Token.VALUE_NULL.equals(parser.currentToken())) {
44+
return null;
45+
}
46+
if (parser.currentToken().isValue()) {
47+
return Instant.ofEpochMilli(parser.longValue());
48+
}
49+
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation());
50+
return null;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample;
10+
11+
import org.opensearch.action.ActionRequest;
12+
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
13+
import org.opensearch.cluster.node.DiscoveryNodes;
14+
import org.opensearch.common.settings.ClusterSettings;
15+
import org.opensearch.common.settings.IndexScopedSettings;
16+
import org.opensearch.common.settings.Settings;
17+
import org.opensearch.common.settings.SettingsFilter;
18+
import org.opensearch.core.action.ActionResponse;
19+
import org.opensearch.indices.SystemIndexDescriptor;
20+
import org.opensearch.plugins.ActionPlugin;
21+
import org.opensearch.plugins.Plugin;
22+
import org.opensearch.plugins.ResourcePlugin;
23+
import org.opensearch.plugins.SystemIndexPlugin;
24+
import org.opensearch.plugins.resource.SharableResourceType;
25+
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceAction;
26+
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceRestAction;
27+
import org.opensearch.plugins.resource.sample.action.create.CreateSampleResourceTransportAction;
28+
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceAction;
29+
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceRestAction;
30+
import org.opensearch.plugins.resource.sample.action.get.GetSampleResourceTransportAction;
31+
import org.opensearch.rest.RestController;
32+
import org.opensearch.rest.RestHandler;
33+
34+
import java.util.Collection;
35+
import java.util.Collections;
36+
import java.util.List;
37+
import java.util.function.Supplier;
38+
39+
public class SampleResourcePlugin extends Plugin implements ResourcePlugin, SystemIndexPlugin, ActionPlugin {
40+
41+
public static final String RESOURCE_INDEX_NAME = ".sample_resources";
42+
43+
@Override
44+
public List<RestHandler> getRestHandlers(
45+
Settings settings,
46+
RestController restController,
47+
ClusterSettings clusterSettings,
48+
IndexScopedSettings indexScopedSettings,
49+
SettingsFilter settingsFilter,
50+
IndexNameExpressionResolver indexNameExpressionResolver,
51+
Supplier<DiscoveryNodes> nodesInCluster
52+
) {
53+
return List.of(new CreateSampleResourceRestAction(), new GetSampleResourceRestAction());
54+
}
55+
56+
@Override
57+
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
58+
return List.of(
59+
new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class),
60+
new ActionHandler<>(GetSampleResourceAction.INSTANCE, GetSampleResourceTransportAction.class)
61+
);
62+
}
63+
64+
@Override
65+
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
66+
final SystemIndexDescriptor systemIndexDescriptor = new SystemIndexDescriptor(RESOURCE_INDEX_NAME, "Example index with resources");
67+
return Collections.singletonList(systemIndexDescriptor);
68+
}
69+
70+
@Override
71+
public List<SharableResourceType> getResourceTypes() {
72+
return List.of(SampleResourceType.getInstance());
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample;
10+
11+
import org.opensearch.plugins.resource.ResourceSharingService;
12+
import org.opensearch.plugins.resource.SharableResourceType;
13+
14+
import static org.opensearch.plugins.resource.sample.SampleResourcePlugin.RESOURCE_INDEX_NAME;
15+
16+
public class SampleResourceType implements SharableResourceType {
17+
private volatile ResourceSharingService resourceSharingService;
18+
19+
private static final SampleResourceType INSTANCE = new SampleResourceType();
20+
21+
private SampleResourceType() {}
22+
23+
public static SampleResourceType getInstance() {
24+
return INSTANCE;
25+
}
26+
27+
@Override
28+
public String getResourceType() {
29+
return "sample_resource";
30+
}
31+
32+
@Override
33+
public String getResourceIndex() {
34+
return RESOURCE_INDEX_NAME;
35+
}
36+
37+
@Override
38+
public void assignResourceSharingService(ResourceSharingService resourceSharingService) {
39+
this.resourceSharingService = resourceSharingService;
40+
}
41+
42+
public ResourceSharingService getResourceSharingService() {
43+
return this.resourceSharingService;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample.action.create;
10+
11+
import org.opensearch.action.ActionType;
12+
13+
/**
14+
* Action to create a sample resource
15+
*/
16+
public class CreateSampleResourceAction extends ActionType<CreateSampleResourceResponse> {
17+
/**
18+
* Create sample resource action instance
19+
*/
20+
public static final CreateSampleResourceAction INSTANCE = new CreateSampleResourceAction();
21+
/**
22+
* Create sample resource action name
23+
*/
24+
public static final String NAME = "cluster:admin/sampleresource/create";
25+
26+
private CreateSampleResourceAction() {
27+
super(NAME, CreateSampleResourceResponse::new);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample.action.create;
10+
11+
import org.opensearch.action.ActionRequest;
12+
import org.opensearch.action.ActionRequestValidationException;
13+
import org.opensearch.core.common.io.stream.StreamInput;
14+
import org.opensearch.core.common.io.stream.StreamOutput;
15+
import org.opensearch.plugins.resource.SharableResource;
16+
import org.opensearch.plugins.resource.sample.SampleResource;
17+
18+
import java.io.IOException;
19+
20+
/**
21+
* Request object for CreateSampleResource transport action
22+
*/
23+
public class CreateSampleResourceRequest extends ActionRequest {
24+
25+
private final SampleResource resource;
26+
27+
/**
28+
* Default constructor
29+
*/
30+
public CreateSampleResourceRequest(SampleResource resource) {
31+
this.resource = resource;
32+
}
33+
34+
public CreateSampleResourceRequest(StreamInput in, Reader<SampleResource> resourceReader) throws IOException {
35+
this.resource = resourceReader.read(in);
36+
}
37+
38+
@Override
39+
public void writeTo(final StreamOutput out) throws IOException {
40+
resource.writeTo(out);
41+
}
42+
43+
@Override
44+
public ActionRequestValidationException validate() {
45+
return null;
46+
}
47+
48+
public SharableResource getResource() {
49+
return this.resource;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugins.resource.sample.action.create;
10+
11+
import org.opensearch.core.action.ActionResponse;
12+
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.core.common.io.stream.StreamOutput;
14+
import org.opensearch.core.xcontent.ToXContentObject;
15+
import org.opensearch.core.xcontent.XContentBuilder;
16+
17+
import java.io.IOException;
18+
19+
/**
20+
* Response to a CreateSampleResourceRequest
21+
*/
22+
public class CreateSampleResourceResponse extends ActionResponse implements ToXContentObject {
23+
private final String resourceId;
24+
25+
/**
26+
* Default constructor
27+
*
28+
* @param resourceId The resourceId
29+
*/
30+
public CreateSampleResourceResponse(String resourceId) {
31+
this.resourceId = resourceId;
32+
}
33+
34+
@Override
35+
public void writeTo(StreamOutput out) throws IOException {
36+
out.writeString(resourceId);
37+
}
38+
39+
/**
40+
* Constructor with StreamInput
41+
*
42+
* @param in the stream input
43+
*/
44+
public CreateSampleResourceResponse(final StreamInput in) throws IOException {
45+
resourceId = in.readString();
46+
}
47+
48+
@Override
49+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
50+
builder.startObject();
51+
builder.field("resourceId", resourceId);
52+
builder.endObject();
53+
return builder;
54+
}
55+
}

0 commit comments

Comments
 (0)