Skip to content

Commit

Permalink
Updates SampleResource class structure
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed Dec 13, 2024
1 parent ca377f6 commit 8845812
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.opensearch.sample;

import java.io.IOException;
import java.util.Map;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
Expand All @@ -22,11 +23,15 @@
public class SampleResource implements Resource {

private String name;
private String description;
private Map<String, String> attributes;

public SampleResource() {}

public SampleResource(StreamInput in) throws IOException {
this.name = in.readString();
this.description = in.readString();
this.attributes = in.readMap(StreamInput::readString, StreamInput::readString);
}

@Override
Expand All @@ -41,12 +46,14 @@ public String getResourceName() {

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

@Override
public void writeTo(StreamOutput streamOutput) throws IOException {
streamOutput.writeString(name);
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeString(description);
out.writeMap(attributes, StreamOutput::writeString, StreamOutput::writeString);
}

@Override
Expand All @@ -57,4 +64,12 @@ public String getWriteableName() {
public void setName(String name) {
this.name = name;
}

public void setDescription(String description) {
this.description = description;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
}

String name = (String) source.get("name");
String description = source.containsKey("description") ? (String) source.get("description") : null;
Map<String, String> attributes = source.containsKey("attributes") ? (Map<String, String>) source.get("attributes") : null;
SampleResource resource = new SampleResource();
resource.setName(name);
resource.setDescription(description);
resource.setAttributes(attributes);
final CreateResourceRequest createSampleResourceRequest = new CreateResourceRequest(resource);
return channel -> client.executeLocally(
CreateResourceAction.INSTANCE,
Expand Down

0 comments on commit 8845812

Please sign in to comment.