-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Allow specifying destination index aliases in the Transfo…
…rm's dest config (#94943)
- Loading branch information
1 parent
32c17d7
commit 2b70165
Showing
41 changed files
with
1,076 additions
and
299 deletions.
There are no files selected for viewing
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,5 @@ | ||
pr: 94943 | ||
summary: Allow specifying destination index aliases in the Transform's `dest` config | ||
area: Transform | ||
type: enhancement | ||
issues: [] |
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
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
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
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
111 changes: 111 additions & 0 deletions
111
...lugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/DestAlias.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,111 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.transform.transforms; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.xcontent.ParseField; | ||
import org.elasticsearch.xcontent.ToXContentObject; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
public class DestAlias implements Writeable, ToXContentObject { | ||
|
||
public static final ParseField ALIAS = new ParseField("alias"); | ||
public static final ParseField MOVE_ON_CREATION = new ParseField("move_on_creation"); | ||
|
||
public static final ConstructingObjectParser<DestAlias, Void> STRICT_PARSER = createParser(false); | ||
public static final ConstructingObjectParser<DestAlias, Void> LENIENT_PARSER = createParser(true); | ||
|
||
private static ConstructingObjectParser<DestAlias, Void> createParser(boolean lenient) { | ||
ConstructingObjectParser<DestAlias, Void> parser = new ConstructingObjectParser<>( | ||
"data_frame_config_dest_alias", | ||
lenient, | ||
args -> new DestAlias((String) args[0], (Boolean) args[1]) | ||
); | ||
parser.declareString(constructorArg(), ALIAS); | ||
parser.declareBoolean(optionalConstructorArg(), MOVE_ON_CREATION); | ||
return parser; | ||
} | ||
|
||
private final String alias; | ||
private final boolean moveOnCreation; | ||
|
||
public DestAlias(String alias, Boolean moveOnCreation) { | ||
this.alias = ExceptionsHelper.requireNonNull(alias, ALIAS.getPreferredName()); | ||
this.moveOnCreation = moveOnCreation != null ? moveOnCreation : false; | ||
} | ||
|
||
public DestAlias(final StreamInput in) throws IOException { | ||
alias = in.readString(); | ||
moveOnCreation = in.readBoolean(); | ||
} | ||
|
||
public String getAlias() { | ||
return alias; | ||
} | ||
|
||
public boolean isMoveOnCreation() { | ||
return moveOnCreation; | ||
} | ||
|
||
public ActionRequestValidationException validate(ActionRequestValidationException validationException) { | ||
if (alias.isEmpty()) { | ||
validationException = addValidationError("dest.aliases.alias must not be empty", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(alias); | ||
out.writeBoolean(moveOnCreation); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(ALIAS.getPreferredName(), alias); | ||
builder.field(MOVE_ON_CREATION.getPreferredName(), moveOnCreation); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
if (other == null || other.getClass() != getClass()) { | ||
return false; | ||
} | ||
|
||
DestAlias that = (DestAlias) other; | ||
return Objects.equals(alias, that.alias) && moveOnCreation == that.moveOnCreation; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(alias, moveOnCreation); | ||
} | ||
|
||
public static DestAlias fromXContent(final XContentParser parser, boolean lenient) throws IOException { | ||
return lenient ? LENIENT_PARSER.apply(parser, null) : STRICT_PARSER.apply(parser, null); | ||
} | ||
} |
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
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
73 changes: 73 additions & 0 deletions
73
.../core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/DestAliasTests.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,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.transform.transforms; | ||
|
||
import org.elasticsearch.common.ValidationException; | ||
import org.elasticsearch.common.io.stream.Writeable.Reader; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
import org.elasticsearch.xcontent.json.JsonXContent; | ||
import org.elasticsearch.xpack.core.transform.AbstractSerializingTransformTestCase; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.emptyString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class DestAliasTests extends AbstractSerializingTransformTestCase<DestAlias> { | ||
|
||
private boolean lenient; | ||
|
||
public static DestAlias randomDestAlias() { | ||
return new DestAlias(randomAlphaOfLength(10), randomBoolean()); | ||
} | ||
|
||
@Before | ||
public void setRandomFeatures() { | ||
lenient = randomBoolean(); | ||
} | ||
|
||
@Override | ||
protected DestAlias doParseInstance(XContentParser parser) throws IOException { | ||
return DestAlias.fromXContent(parser, lenient); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return lenient; | ||
} | ||
|
||
@Override | ||
protected DestAlias createTestInstance() { | ||
return randomDestAlias(); | ||
} | ||
|
||
@Override | ||
protected DestAlias mutateInstance(DestAlias instance) { | ||
return new DestAlias(instance.getAlias() + "-x", instance.isMoveOnCreation() == false); | ||
} | ||
|
||
@Override | ||
protected Reader<DestAlias> instanceReader() { | ||
return DestAlias::new; | ||
} | ||
|
||
public void testFailOnEmptyAlias() throws IOException { | ||
boolean lenient2 = randomBoolean(); | ||
String json = "{ \"alias\": \"\" }"; | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { | ||
DestAlias destAlias = DestAlias.fromXContent(parser, lenient2); | ||
assertThat(destAlias.getAlias(), is(emptyString())); | ||
ValidationException validationException = destAlias.validate(null); | ||
assertThat(validationException, is(notNullValue())); | ||
assertThat(validationException.getMessage(), containsString("dest.aliases.alias must not be empty")); | ||
} | ||
} | ||
} |
Oops, something went wrong.