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

Allow empty configuration for SLM policies #44465

Merged
merged 3 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -19,7 +19,7 @@

package org.elasticsearch.client.snapshotlifecycle;

import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
Expand All @@ -43,8 +43,6 @@ public class SnapshotLifecyclePolicy implements ToXContentObject {
private static final ParseField SCHEDULE = new ParseField("schedule");
private static final ParseField REPOSITORY = new ParseField("repository");
private static final ParseField CONFIG = new ParseField("config");
private static final IndexNameExpressionResolver.DateMathExpressionResolver DATE_MATH_RESOLVER =
new IndexNameExpressionResolver.DateMathExpressionResolver();

@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<SnapshotLifecyclePolicy, String> PARSER =
Expand All @@ -61,11 +59,11 @@ public class SnapshotLifecyclePolicy implements ToXContentObject {
PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME);
PARSER.declareString(ConstructingObjectParser.constructorArg(), SCHEDULE);
PARSER.declareString(ConstructingObjectParser.constructorArg(), REPOSITORY);
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> p.map(), CONFIG);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), CONFIG);
}

public SnapshotLifecyclePolicy(final String id, final String name, final String schedule,
final String repository, Map<String, Object> configuration) {
final String repository, @Nullable Map<String, Object> configuration) {
this.id = Objects.requireNonNull(id);
this.name = name;
this.schedule = schedule;
dakrone marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -89,6 +87,7 @@ public String getRepository() {
return this.repository;
}

@Nullable
public Map<String, Object> getConfig() {
return this.configuration;
}
Expand All @@ -103,7 +102,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(NAME.getPreferredName(), this.name);
builder.field(SCHEDULE.getPreferredName(), this.schedule);
builder.field(REPOSITORY.getPreferredName(), this.repository);
builder.field(CONFIG.getPreferredName(), this.configuration);
if (this.configuration != null) {
builder.field(CONFIG.getPreferredName(), this.configuration);
}
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.cluster.Diffable;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.Context;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.UUIDs;
Expand Down Expand Up @@ -76,11 +77,11 @@ public class SnapshotLifecyclePolicy extends AbstractDiffable<SnapshotLifecycleP
PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME);
PARSER.declareString(ConstructingObjectParser.constructorArg(), SCHEDULE);
PARSER.declareString(ConstructingObjectParser.constructorArg(), REPOSITORY);
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> p.map(), CONFIG);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), CONFIG);
}

public SnapshotLifecyclePolicy(final String id, final String name, final String schedule,
final String repository, Map<String, Object> configuration) {
final String repository, @Nullable Map<String, Object> configuration) {
this.id = Objects.requireNonNull(id);
this.name = name;
this.schedule = schedule;
Expand Down Expand Up @@ -112,6 +113,7 @@ public String getRepository() {
return this.repository;
}

@Nullable
public Map<String, Object> getConfig() {
return this.configuration;
}
Expand Down Expand Up @@ -172,7 +174,7 @@ public ActionRequestValidationException validate() {
}
}

if (configuration.containsKey(METADATA_FIELD_NAME)) {
if (configuration != null && configuration.containsKey(METADATA_FIELD_NAME)) {
if (configuration.get(METADATA_FIELD_NAME) instanceof Map == false) {
err.addValidationError("invalid configuration." + METADATA_FIELD_NAME + " [" + configuration.get(METADATA_FIELD_NAME) +
"]: must be an object if present");
Expand Down Expand Up @@ -265,7 +267,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(NAME.getPreferredName(), this.name);
builder.field(SCHEDULE.getPreferredName(), this.schedule);
builder.field(REPOSITORY.getPreferredName(), this.repository);
builder.field(CONFIG.getPreferredName(), this.configuration);
if (this.configuration != null) {
builder.field(CONFIG.getPreferredName(), this.configuration);
}
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static SnapshotHistoryItem parse(XContentParser parser, String name) {
this.snapshotName = Objects.requireNonNull(snapshotName);
this.operation = Objects.requireNonNull(operation);
this.success = success;
this.snapshotConfiguration = Objects.requireNonNull(snapshotConfiguration);
this.snapshotConfiguration = snapshotConfiguration;
this.errorDetails = errorDetails;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,12 @@ public void testIndexNameGeneration() {
}

public static SnapshotLifecyclePolicy randomSnapshotLifecyclePolicy(String id) {
Map<String, Object> config = new HashMap<>();
for (int i = 0; i < randomIntBetween(2, 5); i++) {
config.put(randomAlphaOfLength(4), randomAlphaOfLength(4));
Map<String, Object> config = null;
if (randomBoolean()) {
config = new HashMap<>();
for (int i = 0; i < randomIntBetween(2, 5); i++) {
config.put(randomAlphaOfLength(4), randomAlphaOfLength(4));
}
}
return new SnapshotLifecyclePolicy(id,
randomAlphaOfLength(4),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ protected SnapshotLifecyclePolicy createTestInstance() {
}

public static SnapshotLifecyclePolicy randomSnapshotLifecyclePolicy(String id) {
Map<String, Object> config = new HashMap<>();
for (int i = 0; i < randomIntBetween(2, 5); i++) {
config.put(randomAlphaOfLength(4), randomAlphaOfLength(4));
Map<String, Object> config = null;
if (randomBoolean()) {
config = new HashMap<>();
for (int i = 0; i < randomIntBetween(2, 5); i++) {
config.put(randomAlphaOfLength(4), randomAlphaOfLength(4));
}
}
return new SnapshotLifecyclePolicy(id,
randomAlphaOfLength(4),
Expand Down