forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Craig Perkins <cwperx@amazon.com>
- Loading branch information
Showing
14 changed files
with
662 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
.../src/internalClusterTest/java/org/opensearch/plugins/resource/SampleResourcePluginIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
68 changes: 68 additions & 0 deletions
68
...r/src/internalClusterTest/java/org/opensearch/plugins/resource/sample/SampleResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...internalClusterTest/java/org/opensearch/plugins/resource/sample/SampleResourceParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...internalClusterTest/java/org/opensearch/plugins/resource/sample/SampleResourcePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...c/internalClusterTest/java/org/opensearch/plugins/resource/sample/SampleResourceType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...java/org/opensearch/plugins/resource/sample/action/create/CreateSampleResourceAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ava/org/opensearch/plugins/resource/sample/action/create/CreateSampleResourceRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...va/org/opensearch/plugins/resource/sample/action/create/CreateSampleResourceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.