-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Clean Up Snapshot Create Rest API #31779
Changes from 5 commits
ddace8f
1b903ce
c8f89d4
746cd8d
9777fd1
a0f4347
3fa1103
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,14 +21,16 @@ | |
|
||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentParser.Token; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.snapshots.SnapshotInfo; | ||
import org.elasticsearch.snapshots.SnapshotInfo.SnapshotInfoBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
@@ -38,6 +40,14 @@ | |
*/ | ||
public class CreateSnapshotResponse extends ActionResponse implements ToXContentObject { | ||
|
||
private static final ObjectParser<CreateSnapshotResponse, Void> PARSER = | ||
new ObjectParser<>(CreateSnapshotResponse.class.getName(), true, CreateSnapshotResponse::new); | ||
|
||
static { | ||
PARSER.declareObject(CreateSnapshotResponse::setSnapshotInfoFromBuilder, | ||
SnapshotInfo.SNAPSHOT_INFO_PARSER, new ParseField("snapshot")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it would be better to simplify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to leave this one for now because there are two parsers in SnapshotInfo. |
||
} | ||
|
||
@Nullable | ||
private SnapshotInfo snapshotInfo; | ||
|
||
|
@@ -48,8 +58,8 @@ public class CreateSnapshotResponse extends ActionResponse implements ToXContent | |
CreateSnapshotResponse() { | ||
} | ||
|
||
void setSnapshotInfo(SnapshotInfo snapshotInfo) { | ||
this.snapshotInfo = snapshotInfo; | ||
private void setSnapshotInfoFromBuilder(SnapshotInfoBuilder snapshotInfoBuilder) { | ||
this.snapshotInfo = snapshotInfoBuilder.build(); | ||
} | ||
|
||
/** | ||
|
@@ -101,38 +111,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
return builder; | ||
} | ||
|
||
public static CreateSnapshotResponse fromXContent(XContentParser parser) throws IOException { | ||
CreateSnapshotResponse createSnapshotResponse = new CreateSnapshotResponse(); | ||
|
||
parser.nextToken(); // move to '{' | ||
|
||
if (parser.currentToken() != Token.START_OBJECT) { | ||
throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "], expected ['{']"); | ||
} | ||
|
||
parser.nextToken(); // move to 'snapshot' || 'accepted' | ||
|
||
if ("snapshot".equals(parser.currentName())) { | ||
createSnapshotResponse.snapshotInfo = SnapshotInfo.fromXContent(parser); | ||
} else if ("accepted".equals(parser.currentName())) { | ||
parser.nextToken(); // move to 'accepted' field value | ||
|
||
if (parser.booleanValue()) { | ||
// ensure accepted is a boolean value | ||
} | ||
|
||
parser.nextToken(); // move past 'true'/'false' | ||
} else { | ||
throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] expected ['snapshot', 'accepted']"); | ||
} | ||
|
||
if (parser.currentToken() != Token.END_OBJECT) { | ||
throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "], expected ['}']"); | ||
} | ||
|
||
parser.nextToken(); // move past '}' | ||
|
||
return createSnapshotResponse; | ||
public static CreateSnapshotResponse fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.rest.RestRequest; | ||
|
@@ -316,21 +317,6 @@ public static IndicesOptions fromMap(Map<String, Object> map, IndicesOptions def | |
defaultSettings); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startArray("expand_wildcards"); | ||
for (WildcardStates expandWildcard : expandWildcards) { | ||
builder.value(expandWildcard.toString().toLowerCase(Locale.ROOT)); | ||
} | ||
builder.endArray(); | ||
builder.field("ignore_unavailable", ignoreUnavailable()); | ||
builder.field("allow_no_indices", allowNoIndices()); | ||
builder.field("forbid_aliases_to_multiple_indices", allowAliasesToMultipleIndices() == false); | ||
builder.field("forbid_closed_indices", forbidClosedIndices()); | ||
builder.field("ignore_aliases", ignoreAliases()); | ||
return builder; | ||
} | ||
|
||
/** | ||
* Returns true if the name represents a valid name for one of the indices option | ||
* false otherwise | ||
|
@@ -360,6 +346,21 @@ public static IndicesOptions fromParameters(Object wildcardsString, Object ignor | |
); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startArray("expand_wildcards"); | ||
for (WildcardStates expandWildcard : expandWildcards) { | ||
builder.value(expandWildcard.toString().toLowerCase(Locale.ROOT)); | ||
} | ||
builder.endArray(); | ||
builder.field("ignore_unavailable", ignoreUnavailable()); | ||
builder.field("allow_no_indices", allowNoIndices()); | ||
builder.field("forbid_aliases_to_multiple_indices", allowAliasesToMultipleIndices() == false); | ||
builder.field("forbid_closed_indices", forbidClosedIndices()); | ||
builder.field("ignore_aliases", ignoreAliases()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one ends up sending parameters that are getting ignored, see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. Makes sense that we only print out what can be input. |
||
return builder; | ||
} | ||
|
||
/** | ||
* @return indices options that requires every specified index to exist, expands wildcards only to open indices and | ||
* allows that no indices are resolved from wildcard expressions (not returning an error). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch , these should not be printed out, nor parsed back, they are query_string parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Credit to @vladimirdolzhenko for pointing this out.